开发者社区> 问答> 正文

Spring+hibernate批量添加数据添加到9000条数据的时候报错了?报错

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  
            }  
        }  
    }



展开
收起
爱吃鱼的程序员 2020-06-22 13:32:14 457 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    一百条flush一次<atarget='_blank'>@springskyhibernate.jdbc.batch_size属性可以设置,最多多少应该要根据环境来配置你好,请问hibernate一次最多可插入多少跳数据?

    2020-06-22 13:32:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多