r1 - 04 Feb 2008 - 13:14:45 - DaveMorrisYou are here: TWiki >  Main Web  >  DaveMorris > VoSpaceMySpace20080204

Notes on Hibernate Session handling.

As part of the database refactoring, I added code to keep Hibernate Session open during a data transfer, with several small Transactions to update the status.

But I kept getting this kind of error in the logs :

    [junit] 2008-02-01 16:08:11,585 VOSPACE DEBUG Transaction failed in ServiceImpl.copyURLToContent() [could not initialize proxy - the owning Session was closed]

Enabled debug logging for the Hibernate libraries and found this :

    [junit] 2008-02-01 16:08:11,152 VOSPACE DEBUG committed JDBC Connection
    [junit] 2008-02-01 16:08:11,152 VOSPACE DEBUG after transaction completion
    [junit] 2008-02-01 16:08:11,152 VOSPACE DEBUG aggressively releasing JDBC connection
    [junit] 2008-02-01 16:08:11,152 VOSPACE DEBUG releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    [junit] 2008-02-01 16:08:11,152 VOSPACE DEBUG returning connection to pool, pool size: 1

It looked like Hibernate was closing the Session and JDBC Connection after every Transaction. Which mean that my in-memory objects were stale, and no longer connected to a Hibernate Session.

I originally thought that you could use more than one Transaction within a Hibernate Session. In fact, having spent a few hours on Google it turns out that that is indeed the way you should use Hibernate.
Hibernate documentation

"Can I use two transactions in one Session?" "Yes, this is actually a better implementation of this pattern ...."

Just that it isn't configured to do that by default. The standard 'out of the box' system is configuration with a connection pool of 1, and 'aggressively' releases JDBC connections at the end of every Transaction.
Hibernate documentation

"The first two implementations provide a 'one session - one database transaction' programming model, also known and used as session-per-request. The beginning and end of a Hibernate session is defined by the duration of a database transaction."

To make it scale to an enterprise level system, you need to set the connection release mode
Hibernate documentation

    hibernate.connection.release_mode = auto (default) | on_close | after_transaction | after_statement

add a 3rd party connection pool, and handle the Session management yourself.
Hibernate documentation
Hibernate documentation

Something else that I haven't looked at in enough detail yet is adding a database cache.
Hibernate documentation

-- DaveMorris - 04 Feb 2008

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
 
AstroGrid Service Click here for the
AstroGrid Service Web
This is the AstroGrid
Development Wiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback