-->
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.  [ 6 posts ] 
Author Message
 Post subject: Multi Stateless EJB calls w/ same Hibernate Session failing
PostPosted: Tue Nov 18, 2003 1:16 pm 
Newbie

Joined: Tue Nov 18, 2003 1:00 pm
Posts: 6
An example of my problem.

I am calling 2 Stateless Sesion EJBs having them share a Hibernate Session. The Hibernate Session is set/disconnected thru a Servlet filter.

On the return of the first call, the Hibernate Session is committed. I need to disconnect/reconnect the session before I make the second call. This should not be the responsibility of the calling class (Struts Action Class), especially when I'm only retrieving data. I get the exception below when I try to invoke a method on the second stateless session ejb.

With Stateful Session EJBs I could use the SessionSynchronization interfact to disconnect()/reconnect() the Hibernate Session. Is there something analogous to this that will let me detect the commit or rollback.

Note
try {
beginCall():
///...
}
finally {
endCall()
}
while functional would be rather silly when dealing with hundreds of methods across numerous Session EJBs.


java.sql.SQLException: The transaction is no longer active - status: 'Committed'
. No further JDBC access is allowed within this transaction.
at weblogic.jdbc.wrapper.JTSConnection.checkIfRolledBack(JTSConnection.j
ava:118)
at weblogic.jdbc.wrapper.JTSConnection.checkConnection(JTSConnection.jav
a:127)
at weblogic.jdbc.wrapper.Connection.prepareStatement(Connection.java:316
)
at weblogic.jdbc.wrapper.JTSConnection.prepareStatement(JTSConnection.ja
va:425)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.ja
va:235)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.j
ava:61)
at net.sf.hibernate.loader.Loader.getStatement(Loader.java:529)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:566)


Top
 Profile  
 
 Post subject: Re: Multi Stateless EJB calls w/ same Hibernate Session fail
PostPosted: Tue Nov 18, 2003 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
drocklein wrote:
I am calling 2 Stateless Sesion EJBs having them share a Hibernate Session. The Hibernate Session is set/disconnected thru a Servlet filter.

Hum... are you sure those EJBs won't ever access Hibernate session in a concurrent way ? Hibernate session are not thread safe.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 1:46 pm 
Newbie

Joined: Tue Nov 18, 2003 1:00 pm
Posts: 6
I'm Storing my Hibernate Session in a ThreadLocal variable.


Excerpts of my helper class

private final static ThreadLocal _threadLocalSession = new ThreadLocal();

...

public Session getSession() {
Session session = (Session) _threadLocalSession.get();
if (session == null) {
session = _setSession();
}
return session;
}

...

private Session setSession() {
try {
Session session = _sessionFactory.openSession();
_threadLocalSession.set(session);
return session;
}
catch (Exception e) {
throw new HibernateSessionsException(e);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 2:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
different EJBs may have their own thread. so thread local variables may not be shared.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 3:02 pm 
Newbie

Joined: Tue Nov 18, 2003 1:00 pm
Posts: 6
Oops on ThreadLocal and Stateless EJBs and thanks; back to the second part of my question phrased a different way.

EJB A calls EJB B to retrieve data.
EJB C calls EJB B to retrieve data.
A Struts Action class calls EJB B to retrieve data.

The code for closing the Hibernate Session could be in all my entry point methods into the EJB layer. It could be in my Struts Action class but that is not where transaction management belongs.

A stack with push/pop each time I get a reference to the Hibernate session with a
try {
beginCall()
...
}
finally {
endCall()
}
would be cumbersome as well.

Have you found a way to resolve this without wrapping a beginCall() and endCall() around every method? I'm only about a week into Hibernate so I'm happy to throw out my initial ideas for a standard/better way.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 7:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Well, I personaly use a DAO layer dedicated to direct hibernate manipulations. I don't like EJBs except for Facade design pattern and tx mgt.

I use a similar design as Spring fwk. What I do is :
* open/close a session in my business layer so every DAO share the same session via thread local.
* dynamic proxy introspection manage calls to DAO layer, so if a session is opened (in Thread local) this one is used, otherwise a new one is opened and then closed in my proxy.
=> My dao layer never open or close session itself.

I your case I would generate EJB (via Xdoclet ?) and delegate process to a POJO. While EJB are generated, it's easy to do beginCall() and endCall()

_________________
Emmanuel


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