Avatar of jaggernat
jaggernat
 asked on

Hibernate: JDBCTransaction toggleAutoCommit Could not toggle autocommit

hi guys

when i use this code



                Session session = HibernateUtil.getSessionFactory().getCurrentSession();      
            session.beginTransaction();
            try {

                //code to call store proc

                }
            catch(Exception e)
                {
            e.printStackTrace();
                }


            session.getTransaction().commit();


i get this exception

>>>>>>>>>>>>>>>> 
org.hibernate.transaction.JDBCTransaction toggleAutoCommit Could not toggle autocommit
                  java.sql.SQLException: DSRA9350E: Operation setAutoCommit is not allowed during a global transaction.
      at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.setAutoCommit(WSJdbcConnection.java:2414)
      at org.hibernate.transaction.JDBCTransaction.toggleAutoCommit(JDBCTransaction.java:194)
      at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:142)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:115)
      at com.aoc.judiciary.framework.daoImpl.TestDaoImpl.getInfo(TestDaoImpl.java:69)
      at com.aoc.judiciary.framework.ejb.BeantestingBean.retrieveInfo(BeantestingBean.java:95)
      at com.aoc.judiciary.framework.ejb.EJSLocalStatelessBeantesting_f63227a0.retrieveInfo(EJSLocalStatelessBeantesting_f63227a0.java:29)
      at com.aoc.judiciary.framework.delegateImpl.LocalTestDelegateImpl.retrieveInfo(LocalTestDelegateImpl.java:74)
      at com.aoc.judiciary.framework.ActionClasses.Action1.retrieve(Action1.java:76)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
and intrestingly when i dont write the  "session.getTransaction().commit() "  statement , there are no exceptions on console.

any one encountered this situation in their project?

thanks
J
Java

Avatar of undefined
Last Comment
jaggernat

8/22/2022 - Mon
NHBFighter

Did you define "hibernate.transaction.factory_class" in your Hibernate config and, if so what value did you give it?
jaggernat

ASKER
HI

this is my hibernate config file

<session-factory>

        <!-- Database connection settings -->
        <property name="connection.datasource">familyDb</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.DB2390Dialect</property>
<property name="current_session_context_class">thread</property>
       
        <mapping resource="spResourceMapping/TestVO.hbm.xml"/>

    </session-factory>

do i need any thing else?

thanks very much
J
NHBFighter

What kind of env are you running in. Is this a web app running on weblogic, is it standalone?   What kind of JDBC driver are you using, is it an XA datasource.  This error usually surfaces when you are running the code with in the context of another transaction. IE a container managed transaction, like from within a EJB, and in that case the container is supposed to manage all the committing and rolling back of transactions.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
jaggernat

ASKER
ok , i see

 i am using websphere application server 6.0 with Db2

It is a web application

Driver : DB2 Legacy CLI-based Type 2 JDBC Driver

I am not using XA datasource but i am using COM.ibm.db2.jdbc.DB2ConnectionPoolDataSource  

In my ejb i am using container mananged trnasaction (the default) where
transaction-type is set to container like this : <transaction-type>Container</transaction-type>

thanks
NHBFighter

If you are calling you DAO from with in an EJB you should not be worrying about transaction management within your DAO (IE you shouldn't start or commit a transaction) that is the containers job. So you can remove the begin transaction and commit transaction code.
jaggernat

ASKER
If i remove the begin transaction code i get this error

org.hibernate.HibernateException: getNamedQuery is not valid without active transaction
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jaggernat

ASKER
although if i keep  session.beginTransaction(); and comment out just  session.getTransaction().commit();

everything works fine without any problems
NHBFighter

Ok lets see if this works:

in your config file try this has your session factory:
<session-factory>
      <property name="connection.datasource">familyDb</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.DB2390Dialect</property>        
      <property name="current_session_context_class">jta</property>
      <property name="transaction.manager_lookup_class">
            org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
      </property>
      <property name="transaction.factory_class">
            org.hibernate.transaction.JTATransactionFactory
      </property>
      <mapping resource="spResourceMapping/TestVO.hbm.xml"/>
</session-factory>


And try the code with out the beginTransaction().
jaggernat

ASKER
hmm, get this error now
>>>>>>>>>>>>>>>>>>>>>>>>>>>
Exception data: org.hibernate.TransactionException: could not register synchronization with JTA TransactionManager
      at org.hibernate.jdbc.JDBCContext.registerSynchronizationIfPossible(JDBCContext.java:181)
      at org.hibernate.jdbc.JDBCContext.<init>(JDBCContext.java:76)
      at org.hibernate.impl.SessionImpl.<init>(SessionImpl.java:213)
      at org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:525)
      at org.hibernate.context.JTASessionContext.buildOrObtainSession(JTASessionContext.java:114)
      at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:80)
      at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
      at com.aoc.judiciary.framework.daoImpl.TestDaoImpl.getInfo(TestDaoImpl.java:50)
      at com.aoc.judiciary.framework.ejb.BeantestingBean.retrieveInfo(BeantestingBean.java:95)
      at com.aoc.judiciary.framework.ejb.EJSLocalStatelessBeantesting_f63227a0.retrieveInfo(EJSLocalStatelessBeantesting_f63227a0.java:29)
      at com.aoc.judiciary.framework.delegateImpl.LocalTestDelegateImpl.retrieveInfo(LocalTestDelegateImpl.java:74)
      at com.aoc.judiciary.framework.ActionClasses.Action1.retrieve(Action1.java:76)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
      at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
      at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
      at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1282)
      at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:673)
      at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:2933)
      at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:221)
      at com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:210)
      at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1912)
      at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:84)
      at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:472)
      at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:411)
      at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:101)
      at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:566)
      at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:619)
      at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:952)
      at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1039)
      at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1462)
Caused by: java.lang.UnsupportedOperationException
      at org.hibernate.transaction.WebSphereExtendedJTATransactionLookup$TransactionManagerAdapter.getStatus(WebSphereExtendedJTATransactionLookup.java:78)
      at org.hibernate.transaction.JTATransactionFactory.isTransactionInProgress(JTATransactionFactory.java:94)
      at org.hibernate.jdbc.JDBCContext.isTransactionInProgress(JDBCContext.java:187)
      at org.hibernate.jdbc.JDBCContext.registerSynchronizationIfPossible(JDBCContext.java:159)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
NHBFighter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jaggernat

ASKER
prints this on the console

>>>>>>>>>>>>>

[5/31/07 15:37:36:381 EDT] 00000039 Environment   I org.hibernate.cfg.Environment <clinit> Hibernate 3.2 cr4
[5/31/07 15:37:36:396 EDT] 00000039 Environment   I org.hibernate.cfg.Environment <clinit> hibernate.properties not found
[5/31/07 15:37:36:412 EDT] 00000039 Environment   I org.hibernate.cfg.Environment buildBytecodeProvider Bytecode provider name : cglib
[5/31/07 15:37:36:428 EDT] 00000039 Environment   I org.hibernate.cfg.Environment <clinit> using JDK 1.4 java.sql.Timestamp handling
[5/31/07 15:37:36:724 EDT] 00000039 Configuration I org.hibernate.cfg.Configuration configure configuring from resource: /hibernate.cfg.xml
[5/31/07 15:37:36:740 EDT] 00000039 Configuration I org.hibernate.cfg.Configuration getConfigurationInputStream Configuration resource: /hibernate.cfg.xml
[5/31/07 15:37:36:943 EDT] 00000039 Configuration I org.hibernate.cfg.Configuration addResource Reading mappings from resource: spResourceMapping/TestVO.hbm.xml
[5/31/07 15:37:37:084 EDT] 00000039 HbmBinder     I org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues Mapping class: com.aoc.judiciary.framework.ValueObjects.TestVO -> FMT_CASEADR
[5/31/07 15:37:37:349 EDT] 00000039 Configuration I org.hibernate.cfg.Configuration doConfigure Configured SessionFactory: null
[5/31/07 15:37:37:896 EDT] 00000039 NamingHelper  I org.hibernate.util.NamingHelper getInitialContext JNDI InitialContext properties:{}
[5/31/07 15:37:37:912 EDT] 00000039 ConnectionFac W   J2CA0294W: Deprecated usage of direct JNDI lookup of resource familyAppDb.  The following default values are used: [Resource-ref settings]

      res-auth:                 1 (APPLICATION)
      res-isolation-level:      0 (TRANSACTION_NONE)
      res-sharing-scope:        true (SHAREABLE)
      loginConfigurationName:   null
      loginConfigProperties:    null
[Other attributes]

      res-resolution-control:   999 (undefined)
isCMP1_x:                 false (not CMP1.x)
isJMS:                 false (not JMS)

[5/31/07 15:37:37:912 EDT] 00000039 DatasourceCon I org.hibernate.connection.DatasourceConnectionProvider configure Using datasource: familyDb
>>>>>>>>>>>>>>>>

and then hangs, doesnt print anything after that
NHBFighter

I'm not sure this has anything to do with the new confic xml snipit I sent you. If you roll back to the old one does it work?  the only thing that should have changed was:
      <property name="transaction.factory_class">
            org.hibernate.transaction.JTATransactionFactory
      </property>
to
      <property name="transaction.factory_class">
            org.hibernate.transaction.CMTTransactionFactory
      </property>
jaggernat

ASKER
 org.hibernate.transaction.CMTTransactionFactory  works perfectly

thanks a lot,
J
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jaggernat

ASKER
so if i am using bean managed transactions(using JTA) or container managed transactions (using ejb container), i am not supposed to use   session.beginTransaction() and session.getTransaction().commit(). Is that correct?

NHBFighter

yep, that is how I understand it.
jaggernat

ASKER
thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23