I recenlty switched over from MyISAM to InnoDB tables in my MySQL 5.0.20 database to ensure transactions rolled back if they were not valid. Also, I am using Hibernate 3.1.
Performing a simple JDBC transaction, the following code works correctly i.e. my InnoDB table is updated correctly:
Code:
Customer ctmr = new Customer("John", "Doe");
ctmr.setcid(1); //Set customer ID
Fintrans fts = new Fintrans();
fts.setAmountWillingToPay(12000.05);
fts.setCustomer(ctmr);
fts.setDatetime("06/22/2006");
fts.setGrandTotalAccrued(3213.20);
fts.setGrandTotalPaid(13651.10);
fts.setReason("No reason given");
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(fts);
tx.commit();
}catch(Exception e){
if (tx!=null)
tx.rollback();
throw e;
}
However, when I try and perform the task using JTA's UserTransaction my InnoDB table is
not updated:
Code:
Customer ctmr = new Customer("John", "Doe");
ctmr.setcid(1); //Set customer ID
Fintrans fts = new Fintrans();
fts.setAmountWillingToPay(12000.05);
fts.setCustomer(ctmr);
fts.setDatetime("06/22/2006");
fts.setGrandTotalAccrued(3213.20);
fts.setGrandTotalPaid(13651.10);
fts.setReason("No reason given");
UserTransaction tx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
try{
tx.begin();
factory.getCurrentSession().save(fts);
tx.commit();
}catch(Exception e){
if (tx!=null)
tx.rollback();
throw e;
}
If I perform the above (UserTransaction) code using MyISAM tables it works perfectly. Below is my Hibernate.cfg.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">zzzzzzz</property>
<property name="connection.username">zzzzzz</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.password">zzzzz</property>
<property name="connection.pool_size">2</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
I think the problem could be something to do with the following line of code in my Hibernate.cfg.xml:
Code:
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
This is because, when I was originally trying to perform my JDBC transaction (on the InnoDB tables) with this line of code I was getting the same problem. I eventually figured out that by removing this from my Hibernate.cfg.xml the update worked fine. However, I think I need this in order to perform a UserTransaction.
If anyone can provide help on the matter that would be greatly appreciated.
Outlaw.