-->
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: openSession()/closeSession() in SLSB
PostPosted: Fri Jan 16, 2004 6:15 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
I followed the guidelines in the FAQ:
http://www.hibernate.org/119.html#A14

and I now have openSession()/closeSession() in all of my business methods of my stateless session bean. I'm a bit worried about what what happens if an exception is thrown (Hibernate or other). Do I have to close the session everywhere I throw? What if it's a RuntimeException?

Ideally, I'd like to put openSession()/closeSession() somewhere more centralized, but I'm not sure where. Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 11:31 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
One of ways can be proxy object:

remote interface:

Code:
public interface Server
  extends javax.ejb.EJBObject {
     
      public Object invoke( String clsName ,
                                     String methodName,
                                     Class params[] ,
                                     Object args[]
                             ) throws java.rmi.RemoteException, Exception;
     
}

home interface :

Code:
public interface ServerHome
  extends javax.ejb.EJBHome {
     
      public Server create() throws javax.ejb.CreateException,
                                                   java.rmi.RemoteException;
     
     
}

implementation :

Code:
public Object invoke(String clsName, String methodName,
                               Class params[] ,Object args[]) throws Exception{
         
         
         //INTERCEPTS ALL CALLS FROM REMOTE CLIENT
          Object obj = lookup(clsName);     
          return  obj.getClass().
                             getMethod(methodName,params).
                                                             invoke(obj,args);
         
                   
     }


client:

Code:

public Object create(Class anyInterface){
          return Proxy.newProxyInstance(anyInterface.getClassLoader(), new Class[]{anyInterface}, new InvocationHandler(){
             
   public Object invoke( Object    obj,
                                  Method   method,
                                  Object[] args) throws java.lang.Throwable {
             //INTERCEPTS ALL CALLS CALLS TO REMOTE SERVER   
               return server.
                   invoke( method.getDeclaringClass(),
                        method.getName(),
                        method.getParameterTypes(),
                        args);
             }
             
             
          }
          );
      }


It is a simple way to use pseudo AOP with EJB for client, server or for both.
It needs to lookup single bean on client "Server", server can invoke method on Local/Remote bean or on POJO.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AOP is a good solution,
Generated EJBs works fine too.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 5:34 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
emmanuel wrote:
AOP is a good solution,


Yes it is, but I can't get it to work in Sun One appserver. I have a business delegate that I can test outside the container and I have my openSession() and closeSession() being called before and after every method. This works great outside the container. But when I deploy it and run it in the appserver, it runs as if the AspectJ stuff wasn't even there. This is my first time using AOP, so I might be doing something wrong. This is what I did:

Code:
pointcut DelegateCall(): call(public * Delegate.* (..));

before(): DelegateCall() {
  // openSession() and assign to a ThreadLocal as in Thread Local Session doc
}

after(): HSPManagerCall() {
  // closeSession()
}


Quote:
Generated EJBs works fine too.


I am already generating EJBs. It's just really annoying to have to catch all the exceptions so that I can close my session, then rethrow the exceptions. There really must be a better way to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 5:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't use AspectJ but cglib2 to develop a dynamic interceptor. It works fine on Weblogic.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2004 8:59 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
PatrickBradley wrote:
I am already generating EJBs. It's just really annoying to have to catch all the exceptions so that I can close my session, then rethrow the exceptions. There really must be a better way to do this.

Close it in "finally" block and declare all exeptions in method declaration or wrapp all exeptions in single "catch(Exception e)" block and rethrow as runtime exception.


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.