-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Session State After Exception
PostPosted: Wed May 06, 2009 12:44 am 
Newbie

Joined: Wed May 06, 2009 12:17 am
Posts: 1
Hello,
I am updating/inserting a bunch of records in loop and after that transaction is being commit. I am using session.save(...) AND session.update(...) for the same.
If a record appears in the transaction which voilates constraints AND/OR due to some other reasons it throws exception then any attempt to use that session again throws exception. Is it the default behaviour of Hiberante session OR i am missing something.
I have a try catch block at record level, this block is a part of loop . Incase this block throws exception (Constraint Voilation currently)
Code:
                           try
                           {
                              session.save(user);
                           }
                           catch (Exception e)
                           {
                              e.printStackTrace();
                           }

then any furthur attemt to use session results in following exception
Code:
[5/6/09 9:26:55:531 IST] 0000001e SystemErr     R org.hibernate.AssertionFailure: null id in com.app.portal.pojos.admin.User entry (don't flush the Session after an exception occurs)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:48)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:150)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:106)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)



I am very much aware that i should avoid exception to handle conditions prior to attempt of saving it but I want to handle it at record level without commiting after each record.

Anyone can please sugggest how can i keep session in stable state after the exception occurs.


Top
 Profile  
 
 Post subject: Re: Hibernate Session State After Exception
PostPosted: Wed May 06, 2009 1:09 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
If a exception is thrown by the session, you should not use that session at all. This is clearly mention in "Java Persistence with Hibernate" book. Business exceptions doesn't invalidate the session object, so in case if your application throw the business exception, you can still use same session object.


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