java - Spring Hibernate "You cannot commit during a managed transaction!" -
after upgrading application jsf 1.2 jsf 2.1 have following spring hibernate exception when trying login:
debug,[interceptor.exceptioninterceptor][],org.springframework.transaction.transactionsystemexception: not commit hibernate transaction; nested exception org.hibernate.transactionexception: commit failed @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:472) @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:754) @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:723) @ org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:392) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:120) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ myapp.aop.interceptor.methodexceptioninterceptor.invoke(methodexceptioninterceptor.java:21) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ myapp.aop.interceptor.sessionactivityinterceptor.invoke(sessionactivityinterceptor.java:47) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ org.springframework.aop.interceptor.exposeinvocationinterceptor.invoke(exposeinvocationinterceptor.java:90) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:204) @ com.sun.proxy.$proxy121.endsession(unknown source) @ myapp.web.action.connection.connectionbean.connect(connectionbean.java:100) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.jboss.seam.util.reflections.invoke(reflections.java:22) @ org.jboss.seam.intercept.rootinvocationcontext.proceed(rootinvocationcontext.java:32) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:56) @ myapp.web.interceptor.exceptioninterceptor.aroundinvoke(exceptioninterceptor.java:42) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:68) @ org.jboss.seam.transaction.rollbackinterceptor.aroundinvoke(rollbackinterceptor.java:28) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:68) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:68) @ org.jboss.seam.core.methodcontextinterceptor.aroundinvoke(methodcontextinterceptor.java:44) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:68) @ org.jboss.seam.core.synchronizationinterceptor.aroundinvoke(synchronizationinterceptor.java:35) @ org.jboss.seam.intercept.seaminvocationcontext.proceed(seaminvocationcontext.java:68) @ org.jboss.seam.intercept.rootinterceptor.invoke(rootinterceptor.java:107) @ org.jboss.seam.intercept.javabeaninterceptor.interceptinvocation(javabeaninterceptor.java:186) @ org.jboss.seam.intercept.javabeaninterceptor.invoke(javabeaninterceptor.java:104) @ myapp.web.action.connection.connectionbean_$$_javassist_seam_2.connect(connectionbean_$$_javassist_seam_2.java) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.jboss.el.util.reflectionutil.invokemethod(reflectionutil.java:335) @ org.jboss.el.util.reflectionutil.invokemethod(reflectionutil.java:348) @ org.jboss.el.parser.astpropertysuffix.invoke(astpropertysuffix.java:58) @ org.jboss.el.parser.astvalue.invoke(astvalue.java:96) @ org.jboss.el.methodexpressionimpl.invoke(methodexpressionimpl.java:276) @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105) @ org.apache.myfaces.trinidad.component.methodexpressionmethodbinding.invoke(methodexpressionmethodbinding.java:46) @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:101) @ org.apache.myfaces.trinidad.component.uixcommand.broadcast(uixcommand.java:190) @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:786) @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1251) @ oracle.adfinternal.view.faces.lifecycle.lifecycleimpl._invokeapplication(lifecycleimpl.java:1074) @ oracle.adfinternal.view.faces.lifecycle.lifecycleimpl._executephase(lifecycleimpl.java:402) @ oracle.adfinternal.view.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:225) @ javax.faces.webapp.facesservlet.service(facesservlet.java:593) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:295) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:214) @ oracle.adfinternal.view.faces.webapp.rich.registrationfilter.dofilter(registrationfilter.java:105) @ org.apache.myfaces.trinidadinternal.webapp.trinidadfilterimpl$filterlistchain.dofilter(trinidadfilterimpl.java:502) @ oracle.adfinternal.view.faces.activedata.adsfilter.dofilter(adsfilter.java:60) @ org.apache.myfaces.trinidadinternal.webapp.trinidadfilterimpl$filterlistchain.dofilter(trinidadfilterimpl.java:502) @ org.apache.myfaces.trinidadinternal.webapp.trinidadfilterimpl._dofilterimpl(trinidadfilterimpl.java:327) @ org.apache.myfaces.trinidadinternal.webapp.trinidadfilterimpl.dofilter(trinidadfilterimpl.java:229) @ org.apache.myfaces.trinidad.webapp.trinidadfilter.dofilter(trinidadfilter.java:92) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:246) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:214) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:83) @ org.jboss.seam.web.loggingfilter.dofilter(loggingfilter.java:60) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ myapp.web.filter.sessionactivityfilter.dofilter(sessionactivityfilter.java:67) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ org.jboss.seam.web.identityfilter.dofilter(identityfilter.java:40) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ org.jboss.seam.web.multipartfilter.dofilter(multipartfilter.java:90) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ org.jboss.seam.web.exceptionfilter.dofilter(exceptionfilter.java:64) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ org.jboss.seam.web.redirectfilter.dofilter(redirectfilter.java:45) @ org.jboss.seam.servlet.seamfilter$filterchainimpl.dofilter(seamfilter.java:69) @ org.jboss.seam.servlet.seamfilter.dofilter(seamfilter.java:158) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:246) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:214) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:230) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:149) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:389) @ org.jboss.as.web.security.securitycontextassociationvalve.invoke(securitycontextassociationvalve.java:169) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:145) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:97) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:102) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:336) @ org.apache.coyote.http11.http11processor.process(http11processor.java:856) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:653) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:920) @ java.lang.thread.run(thread.java:724) caused by: org.hibernate.transactionexception: commit failed @ org.hibernate.engine.transaction.spi.abstracttransactionimpl.commit(abstracttransactionimpl.java:185) @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:468) ... 93 more caused by: org.hibernate.transactionexception: unable commit against jdbc connection @ org.hibernate.engine.transaction.internal.jdbc.jdbctransaction.docommit(jdbctransaction.java:116) @ org.hibernate.engine.transaction.spi.abstracttransactionimpl.commit(abstracttransactionimpl.java:178) ... 94 more caused by: java.sql.sqlexception: cannot commit during managed transaction! @ org.jboss.jca.adapters.jdbc.basewrappermanagedconnection.jdbccommit(basewrappermanagedconnection.java:1052) @ org.jboss.jca.adapters.jdbc.wrappedconnection.commit(wrappedconnection.java:757) @ org.hibernate.engine.transaction.internal.jdbc.jdbctransaction.docommit(jdbctransaction.java:112) ... 95 more
here spring beans configuration :
<bean id="parentsessionfactory" abstract="true"> <property name="datasource"> <ref bean="myds" /> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.connection.driver_class"> org.h2.driver </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.ehcacheregionfactory</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_structured_cache">true</prop> </props> </property> </bean> <bean id="myds" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname" value="java:/myds" /> </bean> <bean id="sessionfactory" parent="parentsessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="configlocation"> <value> classpath:model/hibernate.cfg.xml </value> </property> <property name="hibernateproperties"> <props merge="true"> <prop key="hibernate.dialect"> org.hibernate.dialect.h2dialect </prop> </props> </property> </bean> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory"> <ref bean="sessionfactory"/> </property> </bean> <!-- transactional advice (i.e. 'happens'; see <aop:advisor/> bean below) --> <tx:advice id="txadvice" transaction-manager="transactionmanager"> <!-- transactional semantics... --> <tx:attributes> <!-- methods starting 'get' read-only --> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="read*" read-only="true" /> <tx:method name="save*" read-only="false" isolation="read_committed"/> <tx:method name="remove*" read-only="false" isolation="read_committed"/> <tx:method name="refresh*" read-only="false" isolation="read_committed"/> <tx:method name="delete*" read-only="false" isolation="read_committed"/> <!-- other methods use default transaction settings (see below) --> <tx:method name="*" /> </tx:attributes> </tx:advice> <tx:annotation-driven transaction-manager="transactionmanager"/> <!-- ensure above transactional advice runs execution of operation defined *service interface --> <aop:config> <aop:pointcut id="serviceoperation" expression="execution(* services..*service*.*(..))" /> <aop:pointcut id="daooperation" expression="execution(* dao..*dao.*(..))" /> <aop:advisor advice-ref="sessionactivityinterceptor" pointcut-ref="serviceoperation" /> <aop:advisor advice-ref="exceptioninterceptor" pointcut-ref="serviceoperation" /> --> <aop:advisor advice-ref="txadvice" pointcut-ref="serviceoperation" /> <aop:advisor advice-ref="txadvice" pointcut-ref="daooperation" /> </aop:config>
here hibernate.cfg.xml:
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.h2dialect</property> <!-- <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.ehcacheprovider</property> --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.ehcacheregionfactory</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- several mapping classes here --> </session-factory> </hibernate-configuration>
the @transactional
annotation used in 1 method in application , in unit tests, not used concerns login part of app.
investigating exception further see code throw exception in basewrappermanagedconnection
ironjacamar-jdbc-1.0.17
:
/** * jdbc commit * @exception sqlexception thrown if error occurs */ void jdbccommit() throws sqlexception { synchronized (statelock) { if (inmanagedtransaction) throw new sqlexception("you cannot commit during managed transaction!"); if (jdbcautocommit) throw new sqlexception("you cannot commit autocommit set!"); } con.commit(); if (mcf.isjta().booleanvalue()) { if (inlocaltransaction.getandset(false)) { collection<connectioneventlistener> copy = null; synchronized (cels) { copy = new arraylist<connectioneventlistener>(cels); } connectionevent ce = new connectionevent(this, connectionevent.local_transaction_committed); (iterator<connectioneventlistener> = copy.iterator(); i.hasnext();) { connectioneventlistener cel = i.next(); try { cel.localtransactioncommitted(ce); } catch (throwable t) { if (trace) getlog().trace("error notifying of connection committed listener: " + cel, t); } } } } }
the variable inmanagedtransaction
set true in localmanagedconnection
begin method ironjacamar-jdbc-1.0.17
:
/** * {@inheritdoc} */ public void begin() throws resourceexception { lock(); try { synchronized (statelock) { if (!inmanagedtransaction) { try { if (underlyingautocommit) { underlyingautocommit = false; con.setautocommit(false); } checkstate(); inmanagedtransaction = true; } catch (sqlexception e) { checkexception(e); } } else throw new resourceexception("trying begin nested local tx"); } } { unlock(); } }
and place inmanagedtransaction
set false in localmanagedconnection
commit()
or rollback()
method :
/** * {@inheritdoc} */ public void commit() throws resourceexception { lock(); try { synchronized (statelock) { if (inmanagedtransaction) inmanagedtransaction = false; } try { con.commit(); } catch (sqlexception e) { checkexception(e); } } { unlock(); } } /** * {@inheritdoc} */ public void rollback() throws resourceexception { lock(); try { synchronized (statelock) { if (inmanagedtransaction) inmanagedtransaction = false; } try { con.rollback(); } catch (sqlexception e) { try { checkexception(e); } catch (exception e2) { // ignore } } } { unlock(); } }
is normal have 2 managedconnection 1 basewrappermanagedconnection
, other localmanagedconnection
, i'm going through both of commit method ?
i have read jboss take care of transactions (container managed transactions). possible both spring , jboss try handle sessions , transactions ?
i'm using hibernate 4.2 , spring 3.2.0. of classical configuration found on many tutorials on web nothing fancy. application using configuration working using jsf 1.2. advices, hints.
you configured jta datasource in jboss, managed transactional resource , using non-jta hibernatetransactionmanager
.
to fix it, have 2 options:
- you either use resource_local datasource , provide via jndi
- you keep jta datasource , configure hibernate use spring jtatransactionmanager actual arjuna transaction manager
Comments
Post a Comment