0 Replies Latest reply on Mar 23, 2009 8:40 AM by xmedeko

    JDBC close

    xmedeko

      Hi,

      I use JDBC in a stateless session bean. (I know it's a bad practice.) I get the connection via the JBoss DataSource

      try {
      InitialContext ic = new InitialContext();
      dataSource = (DataSource) ic.lookup("java:/testDS");
      Connecion conn = dataSource.getConnection();

      PreparedStatement ps = ...
      ResultSet rs = ps.executeStatemet();

      } catch (SQLException e) {
      LOG(...);
      throw new EJBException(e);
      } finally {
      DBUtils.close(rs);
      DBUtils.close(ps);
      DBUtils.close(conn);
      }


      I am not sure, whether is it the right thing to close all JDBC resources in the finally clause? AFAIK the container makes a rollback in case of any exception. But I do not know, if the container also cleans everything up as well? E.g. would be sufficient


      try {
      InitialContext ic = new InitialContext();
      dataSource = (DataSource) ic.lookup("java:/testDS");
      Connecion conn = dataSource.getConnection();

      PreparedStatement ps = ...
      ResultSet rs = ps.executeStatemet();

      rs.close();
      ps.close();
      conn.close();
      } catch (SQLException e) {
      LOG(...);
      throw new EJBException(e);
      }

      Thank you
      Andy