这是我的建立连接池的做法,希望帮到楼主:
private static DBConnectionManager dbcm = null; private static Object object = new Object(); public static DBConnectionManager getInstance() {
if (dbcm == null) {
synchronized (object) {
if (dbcm == null)
dbcm = new DBConnectionManager();
}
}
return dbcm;
} public Connection getConnection() {
DataSource ds = null;
Connection conn = null;
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/stock");
if (conn == null)
conn = ds.getConnection();
return conn;
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}

解决方案 »

  1.   

    对了,顺带提一下,我试过,DBCP是有自动管理连接池的功能,每次取出的连接池的id都是一样的,也就是并没有new过新的连接池实例
      

  2.   

    在建立的时候我明明规定max_connection=10,
    在此CLASS中用循环取连接也是最多10个,但是在其他CLASS中却是调用无论打开多少个连接都可以
      

  3.   

    啊,会这样的么?我用的时候,规定了10个,然后,我取10次连接,每次都不释放(也就是不close),然后取第11次的时候,tomcat就抱错了,说没有可用连接了。
      

  4.   

    你是用的我的这个池吗?执行getDBConnetion()然后执行getConnetion()
    我这里就没有10个最大连接那些限制,
      

  5.   

    database.yaml文件,从这里面读的数据库配置production:
      interface: mysql
      driver: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost/jpetstore
      user: root
      password: 123
      max_connection: 10
    enviroment.yaml在这里读取的connect_todatabase:
      connect_to: production
    # connect_to: development
    # connect_to: test
      

  6.   

    你把每次取到的连接打印出来,你看每次id是不是相同,如果全都一样,那就说明每次都用同一个Connection咯,那取多少次都无所谓的吧
      

  7.   

    打印出来的id是相同的,
    但是当我用循环取出连接的时候(不关闭连接)取出的连接数超过了我设定的最大可用连接数,
    如果用一个连接池的话应该限制会生效。max_connection: 10