-->
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.  [ 5 posts ] 
Author Message
 Post subject: Why aren't exceptions being thrown?
PostPosted: Mon Jul 26, 2004 8:24 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
Hi,

I have so far been very successful with Hibernate and I'm very pleased with the results. The only thing I haven't been able to figure out is that if the generated Hibernate SQL is invalid (references a column that doesn't exist, has the wrong data type, or has invalid data, i.e. a NULL in a NOT NULL column) no exceptions are being thrown. The code I have is like this:


Code:
Connection conn = null;
Throwable anException;
try
{
   // Using my own connection pool (no servlet container)
   conn = ConnectionPool.getConnection( "myPool" );

   // superclass method to abstract session logic
   Session session = this.openSession( conn );

   // Do hibernate stuff; save, update, delete

}
catch( Throwable t )
{
   anException = t;
}
finally
{
   try { this.closeSession( ); }
   catch( Throwable t ) { }

   try { ConnectionPool.freeConnection( "myPool", conn ); }
   catch( Throwable t ) { }
}

if( anException != null )
{
    throw( new PersistenceException( t ) );
}

// return values, if any


A few notes: all of my entity beans extend BusinessObject, which has static methods for opening and closing sessions using the ThreadLocal stuff in the documentation, with a few tweaks to account for the fact that a session is opened and closed for every operation. I'm maintaining the "saved" state in a property on BusinessObject and I explicitly call save( ) or update( ) depending on its value (and then set the value accordingly). I have my reasons for this. Also, closeSession( ) does a flush( ) before closing the session. Finally, I've turned showSql on and I've seen the queries that are produced and I can verify that they contain errors (my fault, not Hibernate's).

I can fix the problems, but I would really like to know (in the form of an Exception, preferrably) when there is a problem and the database is not updated. Is it normal for Hibernate not to throw exceptions? Is there a setting in hibernate.cfg.xml or hibernate.properties that I can enable?

Thanks,

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 26, 2004 8:27 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate throws exceptions very well. But having a look at your example, you have some weird ideas how exceptions should be handled. Maybe you better start from one of the examples in the documentation and just leave it like it is. This is not a Hibernate problem.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 10:49 am 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
I never said it was a Hibernate problem. To clarify:

In my application, Hibernate does throw exceptions, but only on get( ) and find( ). The exceptions I have seen come from bean problems (like trying to set a NULL column value on a primitive accessor), and I am able to diagnose these and fix them quite easily. However, I have never seen an exception come from a save( ), update( ), or delete( ), even in situations when I know the mapping to be wrong or privileges to be insufficient. I thought that since this behavior is so consistent it might be something as simple as a configuration setting; if not, then I'll have to take a closer look.

I don't see what you have against my exception handling strategy...I try to do something, if it fails I note the exception for later, and then proceed to close down my session and free my connection (in either case) before throwing the exception (if there was one). The only thing I can think of that might cause confusion is the fact that I named one of my custom exceptions "PersistenceException", which is a name I have seen in other libraries. This is the exception I use to report and log any problems loading or saving data to the database.

Lastly, I admit that I am not using Hibernate as it was intended to be used. My main goal is platform independence using the dialect feature of Hibernate (and also not having to write redundant SQL). This is not a web app and realtime data integrity is far more important than performance, hence opening and closing the session on each operation. I realize that I am deviating from the examples and that there are some optimizations that can be performed, but I'm on a schedule and I'd like to get basic functionality in place before I proceed.

Based on your response it seems that this is an isolated issue, and it's something I'll probably have to work out myself. Sorry for wasting your time.

Thanks,

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 10:54 am 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
Found it. The exceptions are being thrown in closeSession( ) (since that's where flush( ) is called), and since they're in my finally block I'm ignoring them. 3 potential solutions: call flush( ) in the try block, handle the Throwable in the finally block; or (if possible), configure Hibernate to auto-flush (which would be preferable). Is that possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 10:56 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please read the documentation "How to end a Session". You are looking for transaction handling.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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