Hello Hibernate Users,
I am trying to execute a transaction using Hibernate that updates two databases. Suppose that inside my UserTransaction I update my first database, then (in the same UserTransaction) while I update my second database the entry in the first database gets deleted. Ideally the second database should not be updated and the whole UserTransaction should roll back. However in my case the second database is still being updated i.e. the transaction is not rolling back.
My test code is shown below (Where utx is an instance of a UserTransaction, ctmr is an instance of my Customer class, fts is an instance of my FinancialTransaction class, sf and sf2 are instances of my Session Factories - one for each database):
Note: the purpose of putting the thread to sleep is so that I can delete the entry in the first database from outside the UserTransaction.
Code:
utx.begin();
session1 = sf.openSession();
session2 = sf2.openSession();
session1.beginTransaction();
session2.beginTransaction();
session1.save(ctmr);
session1.flush();
t.beep();
try{
Thread.sleep(10000);
}catch(InterruptedException e){}
t.beep();
session2.save(fts);
session2.flush();
session2.getTransaction().commit();
session1.getTransaction().commit();
utx.commit();
I think what is happening is that on its own Hibernate creates another session object while updating the second database. Hence the UserTransaction does not roll back
If anyone knows of a way around this that would be greatly appreciated. At the moment I only use the UserTranscation interface of JTA. I was wondering whether I need to make use of the XAResource or the TransactionManager interfaces? The other idea I had was to make changes to JTA itself.
If anyone can provide any help on the matter it would be greatly appreciated.
Outlaw