-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate insertion failure handling
PostPosted: Sun Jun 17, 2007 12:58 am 
Newbie

Joined: Sun Apr 29, 2007 12:06 pm
Posts: 5
hello to all..

i'm using hibernate 3.2.4sp1, hibernate annotations 3.3.0 and hibernate validator 3.0.0 and spring 2.0.5

i have 2 junit test case
one inserting a 1000+ length string to a 80 column
and
one inserting "com.com" to a a property marked with
hibernate annotation's @Email

of course both will fail, throwing an exception when i do
session.flush()

ok
all is good
but i', using spring with annotation based transaction

i've notice the problem here
in hibernate's code

org.hibernate.engine.ActionQueue lines 231 - 238


Code:
   private void executeActions(List list) throws HibernateException {
      int size = list.size();
      for ( int i = 0; i < size; i++ ) {
         execute( (Executable) list.get(i) );
      }
      list.clear();
      session.getBatcher().executeBatch();
   }


the invalid email throws an com.hypercart.model.dao.hibernate.InvalidStateTranslator at
execute( (Executable) list.get(i) );

while the invalid length code
throws a HibernateException based exception
at
session.getBatcher().executeBatch();

so the list is cleared


anyways the List is a list of insert statements
when the list is not cleared, spring's aop does a commit at the end of the
transaction function resulting in a commit
and the exception is thrown again..

so my question is
shouldn't the list of insertion be cleared whenever you fail a commit?
i mean

what i want is
and that is what happens when i insert a long string
does not fail hibernate validator but fails the actual database insert
insert ....
commit
--error
commit
--ok
commit
--ok
commit
--ok


but when the entity fails the hibernate validator, this is what happens
insert ....
commit
--error
commit
--error
commit
--error
commit
--error

any information on why this behavior is different?
shouldn't a fail insert be consistent?
consistently clearing the list of pending updates?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 5:10 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
The hibernate exception is not recoverable:

11.2.3. Exception handling

If the Session throws an exception (including any SQLException), you should immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable. Ensure that the Session will be closed by calling close() in a finally block.


This simply means you are not allowed to do anything with the session after exception occurs.

Please rate if that helped.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 6:10 am 
Newbie

Joined: Sun Apr 29, 2007 12:06 pm
Posts: 5
in other words, i should call getHibernateTemplate().clear() on a finally block if an exception occurs?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 7:14 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
If exception occurs you're toast - you cannot do anything with current session because the results are simply unpredictable.


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