在使用connection的method结束前将connection.close()放在try {} catch {} finally{}的finally{}模块中

解决方案 »

  1.   

    在jsp或servlet中如何释放连接?具体例子...
      

  2.   

    server.xml
        <Context path="/doc" docBase="doc" debug="0" reloadable="true" crossContext="true">
    <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/> 
    <Resource name="jdbc/MysqlDB" auth="Container" type="javax.sql.DataSource"/> 
        <ResourceParams name="jdbc/MysqlDB"> 
            <parameter> 
                <name>factory</name> 
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 
    </parameter>  <parameter> 
    <name>maxActive</name> 
    <value>100</value> 
    </parameter> 
     
    <parameter> 
    <name>maxIdle</name> 
    <value>30</value> 
    </parameter>  <parameter> 
    <name>maxWait</name> 
    <value>10000</value> 
    </parameter>  <parameter> 
    <name>username</name> 
    <value>sa</value> 
    </parameter>  <parameter> 
    <name>password</name> 
    <value>123</value> 
    </parameter> 
    <parameter> 
    <name>driverClassName</name> 
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> 
    </parameter>  <parameter> 
    <name>url</name> 
    <value>jdbc:microsoft:sqlserver://192.168.16.100:1433;DatabaseName=MyData</value> 
    </parameter> 
    </ResourceParams> 
    </Context>web.xml 中
       <web-app>
      <description>MySQL Test App</description> 
        <resource-ref> 
          <description>DB Connection</description> 
          <res-ref-name>jdbc/MysqlDB</res-ref-name> 
          <res-type>javax.sql.DataSource</res-type> 
          <res-auth>Container</res-auth> 
        </resource-ref> 
      </web-app>使用
      import java.sql.*;
    import javax.naming.*;public class DBSQL {
        public static Connection getConnection() {
            try {
                Context initCtx = new InitialContext();
                Context ctx = (Context) initCtx.lookup("java:comp/env");
                //获取连接池对象
                Object obj = (Object) ctx.lookup("jdbc/MysqlDB");
                //类型转换
                javax.sql.DataSource ds = (javax.sql.DataSource) obj;
                Connection conn = ds.getConnection();
                return conn;
                conn.close();
        conn = null;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }finally{
                if(conn != null){
            conn.close();
        }
    }
        }
    }
      

  3.   

    最后的这几句话就是关闭数据库连接的语句,当然statement也要关闭,把它放在你的调用sql的结束的地方即可         
             conn.close();
    conn = null;
             finally{
                if(conn != null){
            conn.close();
        }
    }
      

  4.   

    我想楼主想要问的是:
        在使用完连接以后如何将这个连接归还到连接池,
    而不是上面各位所说的将连接关闭掉[ connection.close() ]楼主是不是这个意思?
      

  5.   

    我想楼主是自己编写连接池代码,或用好兵的连接池吧,其实tomcat、resin等就可以配置连接池,不用手写了。 楼上RobertDeNiro()已经给出tomcat的配置了。
      

  6.   

    连接池本身就是一种动态托管
    在你代码中coon.close()
    实际上并没有关闭和数据库的连接
    而是将连接返回到了连接池中的连接管理机制,并由该机制完成对资源的释放
    并将释放之后的资源---也就是空的CONNECT返回到空池中
    所以页面或者SERVLET中调用
    conn.close();
    conn = null;
             finally{
                if(conn != null){
            conn.close();
        }
    }此段代码时,资源就可以得到释放了
      

  7.   

    yaray(雅睿) 是我所想...,感谢大家...请继续
      

  8.   

    我是这样close:
      ....;
      rs.close();
      rs=null;
    .......
    .......;
      stmt.close();
      stmt=null;
    .........
      conn.close();
      conn=null;
    ............
    这是写在一个Javabean 中的,在JSP上何处调用,JSP中javabean 的scope="page" ,jsp关闭后,连接释放吗?
      

  9.   

    根据Tomcat的帮助,也是差不多这么写的,可在JSP上何处调用,在结尾处?还是用完就放?
      

  10.   

    try{
      ...
      ...
      ...
      ...
    }catch( ...){
      ...
      ...}finally{
      if(rs!=null) rs.close();
      if(stmt!=null) stmt.close();
      if(conn!=null) conn.close();
    }
      

  11.   

    我的JAVABEAN中部份: 
    public static void getConnection()
      {
        String val="null",title="Test";
        try{
         Context initCtx = new InitialContext();
         if(initCtx == null )
                throw new Exception("No Context");
         ds = (DataSource)initCtx.lookup(
          "java:comp/env/jdbc/OracleDB");
         if (ds != null){
          conn = ds.getConnection();
         }    }
        catch(Exception ex){
         System.out.println(ex.getMessage());
        }
      }  //关闭数据库连接
      public void closeConnection()
      {
          try{
            if(rs!=null){
              rs.close();
              rs=null;}
            if(stmt!=null){
              stmt.close() ;
              stmt=null;
            }
            if(conn!=null){
              conn.close();
              conn=null;}
          }
          catch(SQLException e)
          {
             System.out.println("Conn:"+e.getMessage() ) ;
          }
          finally{
           if(conn!=null){
             try{
             conn.close();
             conn=null;
             }catch(SQLException e){
               System.out.println("Conn:"+e.getMessage() ) ;
             }
            }      }
      }
    //执行数据库查询
      public ResultSet executeQuery(String sql)
      {
          rs=null;
          try
          {
             if(conn==null)
               getConnection();
             if(conn!=null)
             {
              stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
              rs=stmt.executeQuery(sql);
             }
          }
          catch(SQLException ex)
          {
              System.out.println("executeQuery:"+ex.getMessage());
          }
          return rs;
      }
      

  12.   

    在JSP中何处调用closeConnection();
      

  13.   

    JSP一般不涉及数据库连接,普通情况都是放在Bean中作处理。
      

  14.   

    纠正一下我的错误:
    try{
      ...
      ...
      ...
      ...
    }catch( ...){
      ...
      ...}finally{
      try{
        if(rs!=null) rs.close();
        if(stmt!=null) stmt.close();
        if(conn!=null) conn.close();
      }catch(...){
        ....
      }
    }
      

  15.   

    赫赫,很多人很奇怪为什么光闭连接时调用conn.close();而不是象releaseConn(conn)这样的函数来光闭看下面的代码
    public class myConnection extends Connection
    {
        void close()
        {
               DBPool.releaseConn(this);
         }
    }明白了吗?
    调用conn.close()的时候实际上不一定调用是Connection.close这个方法
      

  16.   

    我是放在JAVABEAN中的在JSP中何处调用,结尾处?....?
      

  17.   

    releaseConn()方法是自己写的,还是类的方法