org.hibernate.TransactionException: nested transactions not supported

阅读更多
使用hibernate4.3.5和spring4.0.3进行数据批量保存时出错,错误信息如下:
2014-07-25 13:44:38 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1]-[com.wasu.monitor.service.NewdcErrordataService.save(NewdcErrordataService.java:129)]-[ERROR] nested transactions not supported
org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at com.wasu.monitor.db.HibernateTemplate.batchSave(HibernateTemplate.java:122)
at com.wasu.monitor.service.NewdcErrordataService.save(NewdcErrordataService.java:121)
at com.wasu.monitor.service.NewdcErrordataService$$FastClassBySpringCGLIB$$ede15180.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.wasu.monitor.service.NewdcErrordataService$$EnhancerBySpringCGLIB$$ac233e9a.save()
at com.wasu.monitor.quartz.SpringQuartz.newdc_errordata_save(SpringQuartz.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:321)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)


提示好像是说存在事物嵌套,就是说事物至少开启了2次,于是我修改代码(即去掉事物控制代码部分)
修改前:
	@Override
	public int batchSave(List ts) {

		Session session = sessionFactory.getCurrentSession();

		Transaction tx = session.beginTransaction();

		for (int i = 0; i < ts.size(); i++) {

			Object obj = ts.get(i);

			session.save(obj);

			if (i % 20 == 0) {
				session.flush();
				session.clear();
			}

		}

		session.flush();
		session.clear();

		tx.commit();

		log.info("保存了" + ts.size() + "记录");

		return ts.size();

	}

修改后:
	@Override
	public int batchSave(List ts) {

		Session session = sessionFactory.getCurrentSession();

		for (int i = 0; i < ts.size(); i++) {

			Object obj = ts.get(i);

			session.save(obj);

			if (i % 20 == 0) {
				session.flush();
				session.clear();
			}

		}

		session.flush();
		session.clear();

		log.info("保存了" + ts.size() + "记录");

		return ts.size();

	}


修改后再次测试,数据成功插入!看来还真是事物嵌套造成!呵呵!

你可能感兴趣的:(hibernate,nested,事物嵌套,批量保存)