-->
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.  [ 8 posts ] 
Author Message
 Post subject: HibernateSession.open() and close() in nested SessionBeans
PostPosted: Wed Apr 14, 2004 4:51 am 
Newbie

Joined: Tue Apr 13, 2004 10:13 am
Posts: 4
Hi,

we are using Hibernate 2.1.2 with JBoss 3.2.1. The project uses EntityBeans as persistence layer. For some new functions we want to use Hibernate.
How we use it: The client (other SessionBean, Servlet, JUnitEJB Test) calls a method on a stateless facade-SessionBean. This facade works with stateful SessionBeans (Session for EntityBeans) and/or with hibernate objects and may also call other facades. The hibernate objects don't only have getters and setters, they also have some methods that perform some logic (for example dont get all associated objects but the objects that have a special attribute).
We use the ThreadLocal pattern and the HibernateSession. If I use the Hibernate objects directly it works perfect. I open the Session with HibernateSession.openSession(), do some work and call HibernateSession.closeSession().
But if we are using the facade Session Beans we have the same problem like in this thread http://forum.hibernate.org/viewtopic.php?t=77.

20:25:12,263 INFO [TxConnectionManager$TxConnectionEventListener] throwable from unregister connection
java.lang.IllegalStateException: Trying to return an unknown connection1! org.jboss.resource.adapter.jdbc.WrappedConnection@2b7632
at org.jboss.resource.connectionmanager.CachedConnectionManager.unregisterConnection(CachedConnectionManager.java:264)

We placed the Hibernate.openSession() and Hibernate.closeSession() calls in all Methods of the facade Bean. Do I have to open a Session in every method that works with hibernate objects (that only calls a getter)? Or is the facade the wrong place to open the Session? Should the client do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 8:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Opening and closing the session in the facade sounds pretty reasonable to me, as long as it gets opened once, and closed once :) so if your facade does not call the facade again it should not be a problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 12:12 pm 
Newbie

Joined: Tue Apr 13, 2004 10:13 am
Posts: 4
michael wrote:
Opening and closing the session in the facade sounds pretty reasonable to me, as long as it gets opened once, and closed once :) so if your facade does not call the facade again it should not be a problem.

The problem is, that there are many facades, that sometimes call other facades to do some work. After some calls over SessionBeans JBoss closes "a unknown connection" for me.
Now I am using Session.currentSession in every Method that uses Hibernate (this is bad - but it works).
The Session-per-Transaction Pattern would be the best solution, but I don't know how to implement this without Spring. I there a way to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 12:28 pm 
Newbie

Joined: Tue Apr 13, 2004 10:13 am
Posts: 4
Quote:
Now I am using Session.currentSession in every Method that uses Hibernate (this is bad - but it works).

And I am also closing the Session in this Method ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 12:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can try the reference counting version of TLS in the wiki - it is not very safe, but it might work. You can also use a JBoss interceptor for managing the session, I am currently working on this, if you want I can send you the source in advance (send me an email)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 1:10 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
I prefer to live without EJB and I do not use this way, but I hope it must be a good one:

1. Use stateful bean as facade.
2. Implement transaction synchronization callback interface.
3. Demarcate transactions on client, it can be a filter or commant dispacher (transaction per request and bean reference is stored in any thread scope attribute like threadlocal, request parameter or method parameter ).
4. Close session/connection at the end of transaction.

You do not need any transaction demarcation and threadlocals on server in this case, session can be stored in field. I think it must be a good choise for scalable transactional systems/applications.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 4:44 pm 
Newbie

Joined: Tue Apr 13, 2004 10:13 am
Posts: 4
thx for your ideas,

I will try them next week.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2004 8:10 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hi,

This may be of interest/help:

https://sourceforge.net/forum/forum.php ... _id=250340

Justin


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