shiva raaj
shiva raaj

Reputation: 41

Hibernate - Two different transaction will commit successfully

I am using two different database. I am using two different session for delete records from both database. The code is below:

try{
  Session session1 = factory.getSession();
  Transaction trn1 = session1.beginTrn();
  session1.delete(foobar);
  trn1.commit();

  Session session2 = jbomContext.getGrahpSession();
  Transaction trn2 = session2.beginTrn();
  session2.delete(box);`enter code here`
  trn2.commit();
}catch(Exception e){
     trn1.rollback();
     trn2.rollback();
}

Here, the problem is if the error is occurred in transaction2 i couldn't rollback the transaction1.I have some idea about two phase commit. but i need to rollback the transaction if exception will happen both transaction.

Upvotes: 4

Views: 883

Answers (1)

kdabir
kdabir

Reputation: 9868

You cannot rollback tr1 (if tr2's commit fails) as tr1 will be already committed by then. At least I dont see a native way of doing this in hibernate. What you can probably do is to use distributed transaction (JTA datasource) if you are on a full blown Java EE App Server or can enable it by some other means (something like this and Spring's JtaTransactionManager).

Upvotes: 3

Related Questions