1
回答
Spring+hibernate批量添加数据添加到9000条数据的时候报错了
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
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  
            }  
        }  
    }



举报
springsky
发帖于4年前 1回/985阅
顶部