0 Replies Latest reply on Aug 18, 2005 7:41 AM by Marcos Fernández

    static sql connections

    Marcos Fernández Newbie

      Hi,

      I've got a stateless session EJB and a SqlSomeTask class:

      class SqlSomeTask{

      private static Connection conn;

      public static void openConnection() throws SQLException{
      Context ctx;
      DataSource ds;
      try{
      ctx = new InitialContext();
      ds = (DataSource)ctx.lookup("java:/MSSQLDS");
      if (null == connection)
      conn = ds.getConnection();
      else
      if(conn.isClosed()) {
      conn = ds.getConnection();
      }
      return conn;
      }catch(Exception e) {
      throw new Exception(e.getMessage());
      }
      }

      public static void closeConnection()throws SGException{
      try{
      if (null != connection){
      if (!conn.isClosed()) {
      conn.close();
      }
      }
      }catch(Exception e){
      throw new Exception(e.getMessage());
      }
      }
      }

      Inside my EJB I've written this:

      public class SessionFacadeBean implements SessionBean {
      ...
      public Object invoke(String sessionID, IDTO dto) throws SGException {
      ......
      SqlSomeTask.openConnection();
      SqlSome.save(dto); /*For example*/
      SqlSomeTask.closeConnection();
      ....
      }

      }

      I have a doubt regarding the behaviour of static connection: if JBOSS server is running and a user invoque the EJB and open the static connection and at the same time another user request EJB and invoke the EJB: will another static connection open for this new request or the connection is the same that the previous user opened and it will be reopened? I mean, if connection is static, then is a class variable and it's not saved in the heap but in the method area in the JVM, a new connection isn't opened for each EJB invocation because it's static, is it?
      Since every time a user invoke a stateless EJB, one instance of that EJB is created (new thread) and then destoyed, one instance of SqlSomeTask is created. Nevertheless, SqlSomeTask connection attribute is static so it's the same for every invocation.

      Thanks