-->
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.  [ 3 posts ] 
Author Message
 Post subject: Exception Handling: Continue working with Session
PostPosted: Tue Jul 11, 2006 6:59 am 
Newbie

Joined: Mon Apr 18, 2005 5:31 am
Posts: 3
I would like to get an Object after an exception occurs. However the get() method throws the same exception. What am I doing wrong?

Code:
try{
session.save(anObject);
} catch (ConstraintViolationException exp) {
Object obj = session.get(aClass, aKey);
.
.
.
//exception handling
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:02 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Where is declared your transaction ? Where is your rollback after the exception is thrown ? Did you use a tx context ? (Maybe with Spring?)

try
{
session.beginTransaction();
session.save(yourObject);
}catch(HibernateException)
{

session.getTransaction().rollback();//this should also be put in a try-catch block
}

And I'm not sure, but I think it would be better to use a new session for this. Anyway, the session is designed to be very short-lived, so the right pattern would be to destroy the one that was containing the object you tried to save, I's say.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 12:39 pm 
Newbie

Joined: Mon Apr 18, 2005 5:31 am
Posts: 3
Indeed I'm using Spring and HibernateTransactionManager and the actual code is as follows:

Code:
try{
getHibernateTemplate().save(anObject);
} catch (ConstraintViolationException exp) {
Object obj = getHibernateTemplate().get(aClass, aKey);
.
.
.
//exception handling
}


If I get an exception (ConstraintViolationException specifically) with the save method, I get the same exception with the get method in the catch block.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.