• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate transaction questions

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have 2 questions about hibernate transactions --

1. if I write code like

try {
session.beginTransaction
// my database operation codes ... line 1
// my database codes .. line 2
// my database codes .. line 3
} catch(HibernateException e) {
// transaction rollback
} finally {
// close things
}

Does it mean that by doing this I put "line1,2,3" inside a transaction ? if any of line 1,2,3 fails the other two lines will be rolled back too ? i.e. lines 1,2,3 can only be completed together or none of them will be executed ?

2. A more complex scenario, suppose my codes are like follows. I put line 1 and line 2 in two separate methods. And in each separate method they have its own transaction. The tricky question is -- If "callmethod_1" passes but "callmethod_2" fails, does it mean what we did in "callmethod_1" will be rolled back still ? (because they are put in a bigger outer transaction)

try {
session.beginTransaction();
callMethod_1();
callMethod_2();
} catch(HibernateException e) { // rollback stuff }
finally { // close stuff }

public void callMehtod_1() {
try {
session.beginTransaction;
// line 1
} catch(HibernateException e) { ...}
}

public void callmethod_2() {
try {
session.beginTransaction();
// line 2
} catch(HibernateException e) { ..}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A session is a one to one relationship with its transaction. You cannot have two transactions running within a Session, nor start one transaction end it, then start another transaction within the same Session.

Mark
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you mark.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic