aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Session Bean Methods Call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Session Bean Methods Call" Watch "Session Bean Methods Call" New topic
Author

Session Bean Methods Call

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Hi All:

My session bean code looks like this:

public class TestBean implements SessionBean {
private Connection con;

private String privMethod() {
PreparedStatement pstmt = con.prepareStatement("select * from table");
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
------
}
return "1";
}

public void pubMethod() {
con = DBCon.getConnection(); // con defined as class member
String s = privMethod();
con.close();
}
}

When I call the public method, the connection created there is available to all the private methods called from there becoz the reference is declared at class level....we can access this bean only through it's public method, I mean there is no other invocation point....

So can i be sure that when the public method calls the private method, the container will call the private method on the same bean instance which is used for the public method? Here if the private method is invoked on a new instance then the connection object (con) will be null in the private method which is not desired.

Thank you,
Ashok.
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
You can achieve the functionality by using a stateful session bean. As you want the transaction to span multiple method calls, you will need to use Bean Managed Transaction.
ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Yes, stateful session bean can be an option. But i am using stateless session bean because my bean will be having only one public method which is called by the client....multiple method calls (to private methods) happens only from the public method.....

My question is that, is it possible that when i call the private method from the public method, the container may create a new bean instance and then invoke the private method on this new instance?
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
I think you are safe as long as you are invoking a single method exposed via the session bean's component interface. However if you plan to invoke more than one method, you might run into the problem that you mentioned, in which case stateful session bean would be an appropriate solution.
ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Since this is a webservice method, i have to use stateless session bean only. But is there any possibility that the container may call the private method on a new instance when invoked from the public method?
Vinay Raj
Ranch Hand

Joined: Jun 19, 2006
Posts: 81
As long as the private method invocations are within a single stateless session component method call, you should not face any problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Bean Methods Call