本文介绍了java.lang.IllegalStateException:事务处于非活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 列表< String> memberGuids =(List< String>)newMembers.stream()。map(Member :: getGuid).collect(Collectors.toList()); 
Session session = super.getEntityManager()。unwrap(Session.class);
session.beginTransaction();
的ScrollableResults成员= session.getNamedQuery( Member.getMembersByGuid)setParameterList( memberGuids,memberGuids).setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);

int count = 0;
while(members.next()){
Member member =(Member)members.get(0);
updateMember(member,newMembers.get(count));
if(++ count%JDBC_BATCH_SIZE == 0){
session.flush();
session.clear();
}
}

session.getTransaction()。commit();
session.close();

我正在更新数据库中的值,但正在获取:

java.lang.IllegalStateException:事务处于非活动状态。



如果我注释掉以下代码,

  // session.beginTransaction(); 
//session.getTransaction()。commit();

我得到以下错误。

org .springframework.transaction.TransactionSystemException:无法回滚JPA事务;嵌套异常是javax.persistence.PersistenceException:当在org.springframework.orm.jpa.JpaTransactionManager.doRollback处回滚
时发生意外错误(JpaTransactionManager.java:548)
在org.springframework.transaction.support.AbstractPlatformTransactionManager .org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.com)上的
。 .java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291 )$ or $
在org.springframework.transaction.interceptor.TransactionI nterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( JdkDynamicAopProxy.java:208)
。在com.sun.proxy。$ Proxy37.update(来源不明)
在com.progrexion.pfmetljobs.update.UpdateJob.updateMembers(UpdateJob.java:94)
at com.progrexion.pfmetljobs.update.UpdateJob.job(UpdateJob.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(在java.lang.reflect.Method.invoke在sun.reflect.DelegatingMethodAccessorImpl.invoke来源不明)
(来源不明)
(来源不明)
。在org.springframework.util.MethodInvoker。 invoke(MethodInvoker.java:269)
at org.springframework.beans.factory.config.MethodInvokingBean.invokeWithTargetException(MethodInvokingBean.java:119)
at org.sp在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods ringframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:106)
(AbstractAutowireCapableBeanFactory.java:1637)
。在组织。 springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
。在组织。 springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)
at org .springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans。在org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(AbstractBeanFactory.java:1469)上
org.springframework.beans处的
。在org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch上的
(AbstractBeanFactory.java:544)在org.springframework.beans处
。在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:423)处
org.springframework.beans处的
。 factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:220)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBea
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency( DefaultListableBeanFactory.java:1014)
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
at org.springframework.beans.factory.annotation.InjectionMetadata。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中注入(InjectionMetadata.java:88)
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
。 populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstract AutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject (AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.Abstra ctApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext。 Java的:93)
在com.progrexion.pfmetljobs.update.UpdateJob.main(UpdateJob.java:56)
产生的原因:javax.persistence.PersistenceException:在组织rollbacking
时意外错误.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:103)
在org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:544)
... 50多个
导致:org.hibernate.ResourceClosedException:org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@bd93bc3已关闭
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java: 38)在org.hiberna
在org。org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:227)
处使用
$ te $ resource $。 hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:115)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:250)
at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:89)
at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:100)
... 51 more

解决方案

我发现了这个问题。我在我的函数上使用了@Transactional,并在事务处理已经完成的函数中使用了commit。所以我注释了下面的代码,并且我工作了。

  // session.beginTransaction(); 
//session.getTransaction()。commit();
//session.close();


I am doing batch update as shown below.

List<String> memberGuids = (List<String>) newMembers.stream().map(Member::getGuid).collect(Collectors.toList());
Session session = super.getEntityManager().unwrap(Session.class);
session.beginTransaction();
ScrollableResults members = session.getNamedQuery("Member.getMembersByGuid").setParameterList("memberGuids", memberGuids).setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);

int count = 0;
while (members.next()) {
    Member member = (Member) members.get(0);
    updateMember(member, newMembers.get(count));
    if (++count % JDBC_BATCH_SIZE == 0) {
        session.flush();
        session.clear();
    }
}

session.getTransaction().commit();
session.close();

I am getting updated values in database but getting :

`java.lang.IllegalStateException: Transaction not active.

If I comment out the following code,

//session.beginTransaction();
//session.getTransaction().commit();

I get following error.

org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:548) at org.springframework.transaction.support.AbstractPlatformTransactionManager.doRollbackOnCommitException(AbstractPlatformTransactionManager.java:900) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:789) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) at com.sun.proxy.$Proxy37.update(Unknown Source) at com.progrexion.pfmetljobs.update.UpdateJob.updateMembers(UpdateJob.java:94) at com.progrexion.pfmetljobs.update.UpdateJob.job(UpdateJob.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269) at org.springframework.beans.factory.config.MethodInvokingBean.invokeWithTargetException(MethodInvokingBean.java:119) at org.springframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(AbstractBeanFactory.java:1469) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:808) at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:544) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:447) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:423) at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:220) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1177) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at com.progrexion.pfmetljobs.update.UpdateJob.main(UpdateJob.java:56)Caused by: javax.persistence.PersistenceException: unexpected error when rollbacking at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:103) at org.springframework.orm.jpa.JpaTransactionManager.doRollback(JpaTransactionManager.java:544) ... 50 moreCaused by: org.hibernate.ResourceClosedException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@bd93bc3 is closed at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:38) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:108) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:227) at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.rollback(AbstractLogicalConnectionImplementor.java:115) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.rollback(JdbcResourceLocalTransactionCoordinatorImpl.java:250) at org.hibernate.engine.transaction.internal.TransactionImpl.rollback(TransactionImpl.java:89) at org.hibernate.jpa.internal.TransactionImpl.rollback(TransactionImpl.java:100) ... 51 more

解决方案

I found the problem. I am using @Transactional on my function and using commit inside the function which transactional is doing already. So I commented out the following code and i worked.

//session.beginTransaction();
//session.getTransaction().commit();
//session.close();

这篇关于java.lang.IllegalStateException:事务处于非活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:54
查看更多