Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Session closure

 
Bob Ciazoratione
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

We are starting to use Hibernate in a managed environment. Our application architecure consits of a Session Facade which delegates requests to SLSB business objects. These business objects are responsible for mapping the DTOs to POJOs (and back) and calling the persistence layer. Mixing data access code with business logic, violates our emphasis on separation on concerns and therefor we use the DAO pattern to group code that relates to persistence. As documented in the 'Hibernate in action' book, we use the thread-local session pattern to ensure all our DAOs share the same Hibernate Session. After each DAO invocation, we call flush on the session to enforce real-time exception handling. All this works fine, the only problem is that the session never gets closed (the business object doesn't know if it is the last in the row and I would like to keep the close out of the session facade). Is there a way to automatically close the session when the facade returns to the client (e.g. when the transaction commits, via interceptors, ...)?

Thanx in advance...

Stijn
[ April 06, 2005: Message edited by: Stijn ]
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually man, I have the same problem and really do not know what to do. The point is that guy that create session should close it. So if in your case if any of your facades has opened the session then it is responsible to close it. Ugly way I know. But what else you can suggest.
 
Martin Podrouzek
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stijn

I think it greatly depends on the server you are using. Personally I favor JBoss 4.0.1 due to its automatic (hibernate) session handling. There is a new type of archive called HAR that simplifies mapping and configuration of hibernate. You can find further info on www.jboss.org.

I'm skating on thin ice here, but JBoss is using transactions to handle hibernate sessions if configured so, and you connect to JBoss managed session factory through JNDI. If the transaction ends so ends session.

While I was experimenting with ordinary (manual session creation) approach I also find out that if you don't close the session JBoss recogize that and close the session for you. Of course everyone's closing sessions, right?

Unfortunatelly, infos, about HARs and transactions are on a short supply ( this new JBoss managed session approach), so if you know about anything, please let me know.

Martin
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Podrouzek:
Hi Stijn

I think it greatly depends on the server you are using. Personally I favor JBoss 4.0.1 due to its automatic (hibernate) session handling. There is a new type of archive called HAR that simplifies mapping and configuration of hibernate. You can find further info on www.jboss.org.

I'm skating on thin ice here, but JBoss is using transactions to handle hibernate sessions if configured so, and you connect to JBoss managed session factory through JNDI. If the transaction ends so ends session.

While I was experimenting with ordinary (manual session creation) approach I also find out that if you don't close the session JBoss recogize that and close the session for you. Of course everyone's closing sessions, right?

Unfortunatelly, infos, about HARs and transactions are on a short supply ( this new JBoss managed session approach), so if you know about anything, please let me know.

Martin


It is not a good solution either. From the beginnin jBoss is only one of many appliacation servers that are available. Second it still not a container managed peristence. So probably it the better way to use Spring. If you integerates it with Hibernate it will open the session, close the session, flush the session, commit transaction and do whatever else is needed after business method is completed
Spring and Hibernate are friends
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second Warning.

"stijn"-
Please adjust your displayed name (Real first and real last name please) to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks!

Mark
[ April 06, 2005: Message edited by: Mark Spritzler ]
 
Bob Ciazoratione
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you can write a wrapper around the Hibernate Session which implements the JTA specific javax.transaction.Synchronization interface and registering this wrapper with the current Transaction. This way, the session can be closed (trough the beforeCompletion() callback) when the transation ends. Downsize is that this can only be used in a managed environment (due to JTA).

So probably it the better way to use Spring.


Since we're not using Spring at the time, i'm a bit worried 'bout introducing another new framework (must be justified with the architecture management). Is it possible to use Spring as an entity manager only? Is there a tutorial explaining this usecase?

Thanx...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic