-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using SessionSynchronization to close Hibernate Session
PostPosted: Tue Jan 04, 2005 7:19 pm 
Newbie

Joined: Tue Jan 04, 2005 6:42 pm
Posts: 2
Hibernate version: 2.1.2

Name and version of the database using: DB2 8.1 UDB

Hi,

Thsi question is a bit too long. Please bear with me. I appreciate your inputs.

I am currently looking into porting a J2EE application using Session EJB + Entity EJB (2.0) with TopLink to Session EJB + POJO with Hibernate.

With Hibernate, I will have to explicitly create Session in the begining and invoke Session.close() at the end of every transaction ( I use session per request model). That's easy.. However there is one small glitch for which I dont know how to approach.

Consider a Stateless Session EJB as follows:

public interface MySession extends EJBObject
{
public void method1()

public void method2()
}

Each of these methods are called independently by the client. In addition method2 also calls method1 during its execution. Hence I cannot put both session.open and session.close() in method1.

You might say hey... that is easy, create a method3 that invokes method1 and performs the session close. (Obtaining a session can be done by checking the ThreadLocal - as in the Caveatemptor example)

But, I have a constraint. The codebase I am porting also happens to be used for several earlier releases without Hibernate. Hence my strategy is to write a small code generation framework that will generate POJOs, and some POJO wrappers at build time from the Entity EJBs and use it with new Hibernate mapping files. This way I dont have to change any of the code containing business logic (which is continously changing).

As you can see, the code generation approach for the above scenario is going to be too complex. Instead I've been trying to go alternate route of getting my Stateless SessionEJB implement the SessionSynchronization interface. That way, I can implement the beforeCompletion() method to close the Session. I am no expert in JTA. Hence I wanted to know the opinion of experts in this forum about going down this route.

Thanks,
Srikanth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 10:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
In such a situation using some kind of interceptor-like approach is usually good - like a ServletFilter, or a JBoss interceptor.

I am no big expert of SessionSynchronisation either, but the approach sounds reasonable if you can guarantee that the session is always closed (and only once)


Top
 Profile  
 
 Post subject: Re: SessionSynchronization and Hibernate Session
PostPosted: Wed Jan 05, 2005 12:38 am 
Newbie

Joined: Tue Jan 04, 2005 6:42 pm
Posts: 2
Michael,

Given a choice I would definitely go for interceptor. It is simple and beautiful. However ServletFilter would not be possible since the execution is in the EJB container. And moreover I am using WebLogic, hence there are no interceptors like JBoss does.

Also, I think there would be more than one Session for a ThreadLocal when I have a SessionEJB with Tx Requires invokes another method with Tx Requires New. I also need to close the appropriate session.

Thanks,
Srikanth


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