Hello,
Using Hibernate 2.1.1, I am having trouble rolling back a transaction, when an error occurs, such as:
Code:
Caused by: java.sql.SQLException: ERROR: Cannot insert a duplicate key into unique index ix_uus_user_subscriber
The trouble is that even though my code catches HibernateException and calls xa.rollback(), Hibernate (Session, I guess) still 'remembers' the last SQL statement, the one which caused the exception, and tries to re-execute it, even when my application tries accessing the database using different code.
I would imaging that xa.rollback() would be sufficient, that the last transaction would be rolled back, and Hibernate would completely forget the SQL statements used in that transaction.
My code is below. It includes a catch of HibernateException, which then tries to roll back the current transaction. The code that calls xa.rollback() DOES execute (verified through debugging), yet the rollback does not seem to have the correct effect.
Code:
Transaction xa = null;
try {
Session ses = ServiceLocator.currentSession();
xa = ses.beginTransaction();
UserInfo subscriber = (UserInfo) ses.load(UserInfo.class, subscriberId);
UserInfo subscribee = (UserInfo) ses.load(UserInfo.class, subscribeeId);
UserUserSubscription subscription = new UserUserSubscription();
subscription.setSubscriptionDate(new GregorianCalendar());
subscription.setUserInfo(subscribee);
subscription.setSubscriberUserInfo(subscriber);
ses.save(subscription);
xa.commit();
}
catch (HibernateException he) {
try {
if (xa != null)
xa.rollback();
}
catch (HibernateException he1) {}
throw new DAOException(he.getMessage(), he);
}
Thanks,
Otis
[/code]