先是关于连接池,我仅用了struts框架,采用dbcp连接池server.xml如下:
      <Context path="" docBase="ROOT" reloadable="true" crossContext="true">
          <Resource
   name="jdbc/test"
   auth="Container"
   type="javax.sql.DataSource"
   username="root"
   password="root"
   driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql://127.0.0.1:3306/test"
   maxActive="200"
   minIdle="10"
   maxIdle="30"
   maxWait="10000"
   removeAbandoned="true"
   removeAbandonedTimeout="180"
/>
      </Context>ConnFactory :package com.kuyi.util;import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;public class ConnFactory {
private static DataSource source = null; public static Connection getConnection() {
Connection conn = null;
if (source == null) {
// source = getDataSource("jdbc/kuyi");
// 取得数据库连接
// 加载驱动
try {
conn = ConnFactory.getDataSource().getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("conn:" + conn);
return conn;
}
public Connection getConn(String dsnName)
    {
Connection conn = null;
        Context ctx = null;
        DataSource ds = null;
        String jndiStr = null;
           try{             
            jndiStr = "java:comp/env/jdbc/" + dsnName.trim();
               System.out.println("jndiStr=[" + jndiStr + "]");
               ctx = new InitialContext();
               ds = (DataSource) ctx.lookup(jndiStr);                
               conn = ds.getConnection();
           }
           catch(Exception e)           {
                  System.out.println("Connect database [" + dsnName.trim() + "] Error:" + e);
           }     
           return conn;    
    }
/**
 * 关闭连接
 * 
 * @param conn
 */
public static void close(Connection conn) { try {
if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn = null;
} } private static DataSource getDataSource(String ctxName) {
DataSource ds = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(ctxName);
} catch (Exception e) {
e.printStackTrace();
} return ds; } public static void close(ResultSet rs, Statement st) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
}
} public static void close(ResultSet rs, PreparedStatement pst) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
}
} public static void close(ResultSet rs, PreparedStatement pst,
Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
pst = null;
conn = null;
}
} public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null && rs.next()) {
rs.close();
rs = null;
}
if (st != null) {
st.close();
st = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs = null;
st = null;
conn = null;
}
} public static void close(PreparedStatement pst, Connection conn) {
try { if (pst != null) {
pst.close();
pst = null;
} if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pst = null;
conn = null;
}
} public static void close(PreparedStatement pst) {
if (pst != null) {
try {
pst.close();
pst = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
pst = null;
}
}
} public static void close(Statement st) {
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
st = null;
}
}
} private static BasicDataSource ds = null; public static BasicDataSource getDataSource() {
if (ds == null) {
ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://192.168.1.2:3306/test2");
ds.setUsername("root");
ds.setPassword("root");
ds.setMaxActive(50);
}
return ds;
}
}webroot-web.xml  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/test</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
app:
public void updatePaymentMendState(int id, String state, String operator) {
new PaymentMendDAo().updateState(conn, id, state, operator);
                  System.out.println(conn);
ConnFactory.close(conn);
}
请问下我这样配置连接池还有什么问题没有?
另外我有一次在循环里System.out.println("1");的时候,服务器在循环打印过程(10秒)中一直卡住了,那在服务器上打印是不是应该都去掉来减少服务器资源消耗?
最后一个问题是tomcat服务器在运行时候到底是开有打印显示黑屏的那个tomcat[/tomcat/bin/tomcat6.exe]还是开不带显示的那个[/tomcat/bin/tomcat6w.exe],他们区别在哪里?没有部署经验,还请高手们包涵.

解决方案 »

  1.   

    1、System.out.println("")只是为了测试,不会消耗很多资源,你说的卡住了不知道是因为一直打印"1"还是因为eclipse战用CPU太多了,如果是一直打印"1"那么就是你的代码出现了死循环
    2、启tomcat应该启bin\startup.bat,你说的两个都不是
      

  2.   

    我在所有创建conn的地方都关掉了连接
    现在还是过一段时间后,就报错:连接已满200
      

  3.   

    为什么关闭连接的时候一定要 rs.next() 呢。这岂不是只要rs中有记录就关不掉?肯定连接很快就用完了。
    关的时候最好三个都关掉/**
       * 关闭连接。
       * @param conn
       * @param stmt
       * @param rs
       */
      public static void close (ResultSet rs, PreparedStatement stmt, Connection conn) {
        try {
          if(rs !=null)
             rs.close ();
        } catch (Exception e) {
          Debug.printErr (e.getMessage ());
        }
        try {
        if(stmt !=null)
          stmt.close ();
        } catch (Exception e) {
          Debug.printErr (e.getMessage ());
        }
        try {
        if(conn !=null)
          conn.close ();
        } catch (Exception e) {
          Debug.printErr (e.getMessage ());
        }
    }
      

  4.   


    明显是你的close不起作用。按7楼的方式修改下close。
      

  5.   

    链接释放有问题吧finally{
    DBCon.close(con, stm, rs);
    }public static void close(Connection con,Statement stm,ResultSet rs){
    if(con!=null){
    try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(stm!=null){
    try {
    stm.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if(rs!=null){
    try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }