Hello,
I am really confused about using transactions and MySQL when using the Hibernate MBean Service and JTA. If I execute the code below after the rollback nothing happens and the object is still present within the database. I have also tried to use Hibernate's transaction functions liek:
Transaction tx = session.beginTransaction();
//Create Object
tx.rollback();
and the debug output just adds some lines saying it got the UserTransaction and then it says rollback but still nothing actually happens. I noticed that auto commit was turned on for the connection. I saw in a couple of places where it stated that hibernate would automatically turn off autocommit on any connection that it gets except when the user passes in the connection. If I'm using a JCA bound data source is this still true or do I have to call setAutCommit(False)? However this still has no effect upon if I can rollback or not.
I can get it to work correctly if I mess around with the connection directly. Like if I do:
sess.connection.autoCommit(false);
//Create object call save
sess.connection().rollback();
then the database does rollback and the changes are reverted. However I don't think this is the proper way of doing it since I thought inside of a container I would want to use JTA to manage transactions and that it would then be responsible for calling the rollback on the individual connections.
Thanks,
Eric
Hibernate version:
Hibernate 2.1 deployed as a SAR in JBoss 3.2.1 connecting to a JCA connection to a MySQL 4.0 database with InnoDB tables.
Mapping documents:
Simple test class with an ID and value attribute. The ID is generated using the native method. There is a collection associate with the class but it is not used in the test code.
Code between sessionFactory.openSession() and session.close():
Currently testing as a member function that is a SOAP service exposed via apache axis's EJB provider. There is a Servlet Filter installed that creates a Hibernate Session per request and stores it in a thread local that we then use. The EJB is defined as a Bean Managed Transaction bean so I can use the UserTransaction object that is obtained from the session context. The actual code is:
UserTransaction Tx = ctx.getUserTransaction();
tx.begin();
sess.connection().setAutoComit(false);
Session sess = HibSessionContainer.getSession();
BaseClassA testClass = new BaseClassA();
testClass.setVale("Hello World");
sess.saveOrUpdate(testClass);
tx.rollback();
Full stack trace of any exception that occurs:
No Exceptions
Name and version of the database you are using:
MySql 4.0 with Innodb Tables
Debug level Hibernate log excerpt:
(Note I added the out put to some calls to tx.getStatus)
16:18:34,668 DEBUG [SOAPTestBean] Transaction status: 0
16:18:34,688 DEBUG [SOAPTestBean] Autocommit mode is set to: true
16:18:34,688 DEBUG [SOAPTestBean] Autocommit mode is set to: false
16:18:34,688 DEBUG [SessionImpl] saveOrUpdate() unsaved instance
16:18:34,698 DEBUG [SessionImpl] saving [edu.jhu.ccbm.pathway.data.test.BaseClas
sA#<null>]
16:18:34,698 DEBUG [Cascades] processing cascades for: edu.jhu.ccbm.pathway.data
.test.BaseClassA
16:18:34,698 DEBUG [Cascades] done processing cascades for: edu.jhu.ccbm.pathway
.data.test.BaseClassA
16:18:34,708 DEBUG [EntityPersister] Inserting entity: edu.jhu.ccbm.pathway.data
.test.BaseClassA (native id)
16:18:34,708 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 ope
n ResultSets
16:18:34,718 DEBUG [BatcherImpl] prepared statement get: insert into SktchBaseCl
assA (val) values (?)
16:18:34,718 DEBUG [BatcherImpl] preparing statement
16:18:34,718 DEBUG [EntityPersister] Dehydrating entity: [edu.jhu.ccbm.pathway.d
ata.test.BaseClassA#<null>]
16:18:34,748 DEBUG [StringType] binding 'HELLO WORLD' to parameter: 1
16:18:34,748 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open
ResultSets
16:18:34,778 DEBUG [BatcherImpl] closing statement
16:18:34,778 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 ope
n ResultSets
16:18:34,778 DEBUG [BatcherImpl] prepared statement get: SELECT LAST_INSERT_ID()
16:18:34,788 DEBUG [BatcherImpl] preparing statement
16:18:34,788 DEBUG [EntityPersister] Natively generated identity: 14
16:18:34,798 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open
ResultSets
16:18:34,798 DEBUG [BatcherImpl] closing statement
16:18:34,798 DEBUG [Cascades] processing cascades for: edu.jhu.ccbm.pathway.data
.test.BaseClassA
16:18:34,808 DEBUG [Cascades] done processing cascades for: edu.jhu.ccbm.pathway
.data.test.BaseClassA
16:18:34,808 DEBUG [SOAPTestBean] After save Transaction status: 0
16:18:34,818 DEBUG [SOAPTestBean] After rollback Transaction status: 6
|