jdbc connection from datasource in stateless session bean

Discussions

EJB design: jdbc connection from datasource in stateless session bean

  1. Hello team,

    Can I use an instance variable to store jdbc connection that I got from datasource.getConnection? I get this connection in ejbCreate() and put this in instance variable. Is this ok? where should I close this connection? which method? I am using stateless SB. Also I want to use instance varible to store the local home to my entity bean in this stateless SB. Do I need to nullify these db connection and local home in any methods? Please clarify.

    Thank you
  2. Hello team,Can I use an instance variable to store jdbc connection that I got from datasource.getConnection? I get this connection in ejbCreate() and put this in instance variable. Is this ok? where should I close this connection? which method? I am using stateless SB. Also I want to use instance varible to store the local home to my entity bean in this stateless SB. Do I need to nullify these db connection and local home in any methods? Please clarify.Thank you
    obtain the reference to the datasource in the setSessionContext then open the connection when used in the method and then close the connection in a finally block in the method
  3. Hi Sireesha,

    The EJB spec states that access to resource managers from a stateless SB is restricted to businessmethods only. But it doesn't specify how the container should react if you chose to use ejbCreate to obtain a connection. So, even if the container allows it, it is not portable. You could define an instance variable that holds your connection factory, and initialize that the first time a BM is called. From within a BM use this factory to create a new connection, which you should destroy at the end of the method.
  4. You have to get DataSource reference in the method setSessionContext().Open and close your connection in your business method. In this case you will have 100% garantee that connection was closed.