Hibernate – two different transactions will be successfully submitted

I am using two different databases. I am using two different sessions to delete records from the two databases. The code is as follows:

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 that if an error occurs in transaction2 I cannot roll back the transaction. I have some ideas about two-phase commit. But if the exception will occur in two transactions, I need to roll back the transaction.

You cannot roll back tr1 (if tr2’s submission fails) because tr1 has already been submitted. At least I’m not in See the local way of doing this in hibernate. What you can do is to use distributed transactions (JTA data sources), if you are using a full Java EE App Server, or you can enable it by other means (such as this and Spring's JtaTransactionManager).

I am using two different databases. I am using two different sessions to delete records from the two databases. The code is as follows:

try{
Session session1 = factory.getSession();
Transaction trn1 = session1.beginT rn();
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 that I cannot roll back the transaction if an error occurs in transaction2. I have some thoughts on two-phase commit. But if the exception will occur in two transactions, I need to roll back the transaction.

You cannot roll back tr1 (if the commit of tr2 fails) because tr1 has been Submitted. At least I did not see a local way of doing this in hibernate. What you can do is to use distributed transactions (JTA data source), if you are using a full Java EE App Server, or you can enable it by other means It (such as this and Spring's JtaTransactionManager).

Leave a Comment

Your email address will not be published.