-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Cannot commit during a managed transaction
PostPosted: Sat Sep 06, 2003 10:49 am 
Newbie

Joined: Thu Sep 04, 2003 5:20 pm
Posts: 4
Location: ZA
I wish to use Hibernate with JTA on my JBoss server.
I'm build a session bean that has several methods that will communicate
with the underling Hibernate system. My main requirement is that I would like to use CMT.


I'm using a Session EJB with CMT (* / Required) on JBoss 3.2.1.
I have a method that interfaces with Hibernate via
SessionFactory reference obtained from JNDI. (Running Hibernate as MBean service)


Now the problem seems to be that Hibernate is interfering with my CMT.
I get a "java.sql.SQLException: You cannot commit during a managed transaction!"
(see below for more detailed stack).

Truly, I don't even know where to start rectifying this problem.
Any ideas are greatly appreciated.


jboss-service.xml
----------------------

<server>
<mbean code="net.sf.hibernate.jmx.HibernateService" name="jboss.jca:service=HibernateFactory,
name=HibernateFactory">
<depends>jboss.jca:service=RARDeployer</depends>
<depends>jboss.jca:service=LocalTxCM,name=jdbc/ejbPool</depends>
<!-- Make it deploy ONLY after DataSource had been started -->
<attribute name="MapResources">Person.hbm.xml</attribute>
<attribute name="JndiName">java:/hibernate/HibernateFactory</attribute>
<attribute name="Datasource">java:jdbc/ejbPool</attribute>
<attribute name="Dialect">net.sf.hibernate.dialect.MySQLDialect</attribute>
<attribute name="TransactionStrategy">net.sf.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="TransactionManagerLookupStrategy">net.sf.hibernate.transaction.JBossTransactionManagerLookup</attribute>
<attribute name="UseOuterJoin">true</attribute>
<attribute name="ShowSql">true</attribute>
<attribute name="UserTransactionName">java:/UserTransaction</attribute>
</mbean>
</server>



session bean snippet
----------------------

public void createPerson(Person p) {
Session s = null;
try {
Context ctx = new InitialContext();
SessionFactory factory =
(SessionFactory) ctx.lookup("java:/hibernate/HibernateFactory");

s = factory.openSession();

s.save(p);
s.flush();
s.close();
} catch(Exception e) {
e.printStackTrace();
}
}



During method invocation I get the following stack trace:
------------------------------------------------------------

16:08:46,847 ERROR [JDBCExceptionReporter] Could not save object
java.sql.SQLException: You cannot commit during a managed transaction!
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcComm
it(BaseWrapperManagedConnection.java:400)
at org.jboss.resource.adapter.jdbc.WrappedConnection.commit(WrappedConne
ction.java:477)
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:126)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.ja
va:59)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:614)
at com.alen.hibernatest.PersonServiceBean.createPerson(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(S
tatelessSessionContainer.java:629)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invo
ke(CachedConnectionInterceptor.java:186)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(Stat
elessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInte
rceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxIntercep
torCMT.java:243)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:1
04)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.
java:117)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFacto
ryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessi
onContainer.java:322)
at org.jboss.ejb.Container.invoke(Container.java:674)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBea
nDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:
359)
at sun.reflect.GeneratedMethodAccessor160.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:701)
at java.lang.Thread.run(Thread.java:536)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 12:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Check out the docs...

http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-4-hilo

Quote:
...you can't use hilo when supplying your own Connection to Hibernate, or when Hibernate is using an application server datasource to obtain connections enlisted with JTA...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 11:16 am 
Newbie

Joined: Thu Sep 04, 2003 5:20 pm
Posts: 4
Location: ZA
THANK YOU Steve for your reply.

I thought that my problem resolution required some small configuration change.

Funny enough, as soon as I so "hilo" and "JTA" keywords in your reply I remembered the quote when I read the doc.
Well lack of experience with Hibernate I guess. ;)

--Alen


Top
 Profile  
 
 Post subject: [ROLLBACK ISSUE] Cannot commit during a managed transaction
PostPosted: Sun Sep 07, 2003 6:04 pm 
Newbie

Joined: Thu Sep 04, 2003 5:20 pm
Posts: 4
Location: ZA
Now, I have a similar problem as to the following, unanswered, post
http://forum.hibernate.org/viewtopic.php?t=136&highlight=cmt

As said, I'm using CMT with my stateless session bean.
Now the problem is that the Container doesn't invoke the rollback on
my createPerson(...) method when an exception is thrown. (unchecked exception is thrown in my case)
So yes the DB insert statement executed via Hibernate save(...) method in my code is made permanent/committed.

This very much sounds as if my Hibernate code is not aware of the CMT at all.

How would I perhaps go about determining if Hibernate system is using the JTS on my JBoss 3.2.1 server?
Any other suggestions or ideas that I can try to help me find the solution to my problem, much appreciated.



session bean snippet
----------------------

public void createPerson(Person p) {
Session s = null;
try {
Context ctx = new InitialContext();
SessionFactory factory =
(SessionFactory) ctx.lookup("java:/hibernate/HibernateFactory");

s = factory.openSession();

s.save(p);
//s.flush();

if (1==1) { // tmp test
throw new Exception("JTS test...");
}
} catch(Exception e) {
e.printStackTrace();
try {
sessionContext.setRollbackOnly();
} catch(Exception ex) {
System.out.println("Unable to doom transaction");
ex.printStackTrace();
}
} finally {
if(s!=null) try { s.close(); } catch(Exception e) {}
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 7:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
As long as the connection that Hibernate is using is enlisted with JTA, it is the container's responsibility to roll back the work done on that connection.

So check the configuration of the datasource that Hibernate is using to obtain connections.


For this to work, Hibernate doesn't even really need to be "aware" of the JTA transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 9:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Also, when using CMT it is more than a good idea to set the transaction for rollback-only on exception conditions.


Top
 Profile  
 
 Post subject: Got it to work!
PostPosted: Tue Sep 09, 2003 3:14 pm 
Newbie

Joined: Thu Sep 04, 2003 5:20 pm
Posts: 4
Location: ZA
I got the problem resolved.

The issue was around my Database system.
I'm using MySQL 3.23.x and didn't have my transaction support in place.
Once I enabled MySQL for transaction support and modified my tables in my db to correct table types (InnoDB) it all worked.

Good idea was that I ran "BEGIN", "INSERT some data", "ROLLBACK" against my db and got following message which gave me the final clue.

"ERROR 1196: Warning: Some non-transactional changed tables couldn't be rolledack"

So I just ran "ALTER TABLE table_name TYPE = InnoDB;" against my tables and that was it.

My CMT now works with Hibernate system.


Top
 Profile  
 
 Post subject: setRollbackOnly
PostPosted: Wed Nov 12, 2003 6:05 am 
i've read that "it's good to have" a setRollbackOnly .
does this mean it would not work without? i allways thought that the container makes a commit by itself when there is no uncatched exception and a rollback when i use a throw new MyException. so is this true? (also with hibernate?)

in all the examples i've never seen someone closing a session. is this done by the container automatically when the session bean method finishes?

thanx in advance,
blip


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.