1 Reply Latest reply on Dec 9, 2003 7:49 PM by jonlee

    Where can I close connection

    diana

      Hi, I have a problem with datasource, if I don't close the connection , jboss seid me:
      org.jboss.resource.connectionmanager.CachedConnectionManager] Successfully closed a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@2fcdf5
      java.lang.Exception: Stack Trace

      But, when I try to close the connection, with next code:
      public Collection ejbFindAvailable(String sUser,String sPass) throws FinderException {
      int iID=0;
      ArrayList aUsuarios = new ArrayList();

      String sGetUsuario="{call pUsuario_ValidateUser (?,?)}";
      try {
      ResultSet rsUsuario;
      CallableStatement prstmt = con.prepareCall(sGetUsuario,
      ResultSet.TYPE_SCROLL_INSENSITIVE,
      ResultSet.CONCUR_READ_ONLY);
      prstmt.setString(1,sUser);
      prstmt.setString(2,sPass);
      rsUsuario=prstmt.executeQuery();
      while (rsUsuario.next()) {//You are supposed to return a collection with the keys
      iID =rsUsuario.getInt(1);//And findbyPrimarykey will return the instance
      aUsuarios.add(String.valueOf(iID));
      }
      this.closeConnections(rsUsuario,prstmt,con);
      } catch (Exception ex) {
      throw new EJBException("ejbFindByAvailable: " + ex.getMessage());
      }
      return aUsuarios;
      }
      public void setEntityContext(javax.ejb.EntityContext entityContext) throws javax.ejb.EJBException, java.rmi.RemoteException {
      this.context = entityContext;
      try {
      makeConnection();
      } catch (Exception ex) {
      throw new EJBException("No se puede conectar a la BD del Tren. " +
      ex.getMessage());
      }
      }
      private void makeConnection() throws NamingException, SQLException {
      try{
      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup(dbName);
      con = ds.getConnection();
      System.out.println("Se conecto a la bd");
      }catch (Exception ex) {
      System.out.println("getConnection failed. See error log for stack trace.");
      }
      }
      private void closeConnections(ResultSet res, CallableStatement cs,Connection c) {
      try {
      if (res != null){
      res.close();
      res=null;}
      if (cs != null){
      cs.close();
      cs=null;
      }
      if (c != null){
      c.close();
      c=null;
      }
      System.out.println("Se desconecto de la bd");

      } catch (Exception e) {
      System.err.println(e.getMessage());
      }
      }

      Jboss said:
      [org.jboss.resource.connectionmanager.LocalTxConnectionManager$LocalConnectionEventListener] throwable from unregister connection
      java.lang.IllegalStateException: Trying to return an unknown connection1!

      What can I do to solve this problem? I read many solutions, but I can't solve my problem, I use Jboss 3.0.7 an SQL Server 2000, and I didn't find the transaction-server.xml file

      Help me please!!
      Thanks at all!!

        • 1. Re: Where can I close connection
          jonlee

          You should probably not make your connection in setContext(). Since this is controlled by the container and you are implementing BMP, you can't guarantee when events are going to occur and when the system believes a transaction is complete. This may be leading to your problem. Try obtaining your connection in the business method ejbFindAvailable. This is standard practice - see Mastering Enterprise JavaBeans by Ed Roman.

          Hope that helps.