Hi,
I have a doubt about how Hibernate's JDBCTransaction works, and how handles "nested" transactions.
Here's the code:
Code:
Transaction tx1 = session.beginTransaction();
// Start Block 1
User user = new User();
user.setName("blah"); // Name has unique constraint
Account account = new Account();
account.setUser(user);
Transaction tx2 = session.beginTransation();
session.save(user);
session.save(account);
tx2.commit();
// End Block 1
user.setLogin("duplicate"); // Violates unique constraint
session.update(user);
tx1.commit();
What happens is that tx1 never commits since violates the unique constraint on user.name, but nonetheless I get the user and the account inserted in the DB.
Note that the code in "Block 1" can be a call to another method that may not have knowledge that an "external" transaction has been started, and that the method can be used independently from the code shown.
I did not try, but I guess the same code in JTA environment works fine, that is no records are inserted in the DB (I'm going to try this).
I noticed that JTATransaction knows if it started the transaction or not, and behaves differently on commit/rollback, while I could not see this behavior in JDBCTransaction.
The question is: is this expected JDBCTransaction behavior ?
If so, how can I modify the code above considering that "Block 1" can be a method on its own ?
Is my last resort using a TransactionManager and JTATransaction ?
Thanks,
Simon
Hibernate version:
2.1.8