hibernate批量添加数据添加到8000条数据的时候,发现一个问题代码卡在session.beginTransaction(); // 开启事务 这行不走了, java.lang.NullPointerException at beginTransaction()
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 使用 annotation --> <context:annotation-config /> <!-- 加载数据库连接文件 --> <!-- 加载Framework模块 --> <import resource="classpath*:/config/action/applicationContext-action-service-dao.xml" /> <context:property-placeholder location="classpath:connection.properties"/> <!-- <context:property-placeholder location="classpath:config/hibernate.properties"/> --> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${connection.driverClassName}" /> <property name="url" value="${connection.url}" /> <property name="username" value="${connection.username}" /> <property name="password" value="${connection.password}" /> <!-- <property name="maxActive" value="10"></property> <property name="initialSize" value="5"></property> <property name="removeAbandoned" value="true"></property> <property name="removeAbandonedTimeout" value="3600"></property> --> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 配置SessionFactory所需的数据源,注入上面定义的dataSource --> <property name="dataSource" ref="dataSource" /> <!--<property name="annotatedClasses"> --> <property name="packagesToScan" value="com.gkwl.entity"> <!-- <list> <value>com.gkwl.excel.entity.MianDanExcelEntity</value> <value>com.gkwl.entity.OrderFormEntity</value> <value>com.gkwl.entity.MaterialEntity</value> <value>com.gkwl.entity.AuthorityEntity</value> </list> --> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect <!-- org.hibernate.dialect.Oracle10gDialect--> </prop> <prop key="hibernate.show_sql"> true </prop> <prop key="hibernate.format_sql"> true </prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.autoReconnect">true</prop> <!-- Hibernate Hbm2dll (none, validate,update,create) --> <prop key="hibernate.hbm2ddl.auto">none</prop> <prop key="hibernate.jdbc.batch_size">30</prop><!-- 每50条语句提交一次 --> <prop key="hiberante.cache.use_second_level_cache">false</prop><!--关闭二级缓存 --> <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> </props> </property> </bean> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 定义事务管理通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="daocut" expression="within(com.gkwl.service.*)" /> <!-- advisor是通知和切入点的结合体 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="daocut" /> </aop:config> </beans>
public <T> void saveBatch(List<T> entitys){ Session session = null; if (entitys != null && entitys.size() > 0) { try { session = this.getHibernateTemplate().getSessionFactory().openSession(); session.beginTransaction(); // 开启事务 T t = null; for (int i = 0; i < entitys.size(); i++) { t = entitys.get(i); session.save(t); if ((i != 0 && i % Constants.DEFAULT_BATCH_SIZE == 0) || i == entitys.size() - 1) { session.flush(); session.clear(); } } session.getTransaction().commit(); // 提交事务 } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); // 出错将回滚事务 } finally { this.getHibernateTemplate().getSessionFactory().close(); // 关闭Session } } }
一百条flush一次<atarget='_blank'>@springskyhibernate.jdbc.batch_size属性可以设置,最多多少应该要根据环境来配置你好,请问hibernate一次最多可插入多少跳数据?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。