I'm using Hibernate 2.1 with MySQL with InnoDB tables (though I've seen the same behavior with Oracle 8.x). The problem I'm having is with transactions not rolling back. My Servlet Engine is Tomcat 4.
in hibernate.cfg.xml I've set:
Code:
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
My java code looks like:
Code:
try
{
transaction = session.beginTransaction();
session.saveOrUpdate(persistable);
session.flush();
transaction.commit();
}
catch (Throwable err)
{
convertException(err);
}
finally
{
if (null != transaction)
{
try
{
if (!transaction.wasCommitted())
{
transaction.rollback();
}
}
catch (HibernateException err2)
{
getLog().error("Failed to rollback transaction!", err2);
}
}
}
I've stepped through the code, and when there is an error on the flush, I verify that rollback is being called. However, the data from associations is not getting rolled back. I am sure of this because I start my test with an empty DB.
I've tried taking the flush out, still no roll back.
If I add 'session.connection().setAutoCommit(false);' before the call to beginTransaction, I get very strange behavior. I step through the code, and my newly created object has it's id set inside the saveOrUpdate, but, on flush (or commit if I take the explicit flush out), I get an error saying that the object already exists. Huh? However, the rollback does work in this case.
How can I have both rollback capability and be able to saveUpdate newly created objects? I know this is wanting to have my cake and eat it to, but it is rather important ;)