• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session Bean Methods Call

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as the private method invocations are within a single stateless session component method call, you should not face any problems.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic