-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: XA rollback doesn't rollback (SQL/JDBC/Hibernate)Exception
PostPosted: Thu Mar 04, 2004 9:53 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
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]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 12:24 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Let me add to the above that putting session.clear() in the rollback block solves the problem.

However, session.clear() sounds like the wrong thing to do, and not a real solution, as clearing my session will lose everything that Hibernate knows about me/my session.

Any ideas would be much appreciated!
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 12:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should immediately discard a session after a rollback caused by an exception from a hibernate method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 1:55 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
So is calling session.clear() actually recommended in ALL cases where I catch HibernateException?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, it is not. You should immediately throw away the session, and not use it any further.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 2:23 pm 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
So what exactly do you mean by 'throw away'?

session.close(); // or session = null?
session = getNewSessionFromSomewhere();

Is that what you mean?
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 2:39 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
something like
Session ses = ServiceLocator.close();
where close() method will close the hibernate session and "clean" the threadLocal if you use thread local session


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 2:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, you have to use a completely new session after an exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.