这是我的建立连接池的做法,希望帮到楼主:
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;
}
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;
}
解决方案 »
- 菜鸟求助:window.open() 父窗口操作子窗口问题
- 关于页面嵌入mediaplayer播放器的问题,只有20分了不好意思。坐等。
- struts2 表单路径传参数 ,在action中怎么接收不到?急!!
- js添加代码后焦点消失,如何解决?
- ie显示xml文件的问题
- 继承SqlMapClientDaoSupport的DAO类,怎么测试它?
- jsp用jspsmart上传后自动生存成缩略图
- hibernate问题
- 在jsp中用Bean的问题
- java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyResultSet
- 在Jtree中添加节点和删除节点,如何把添加的节点保存.下次启动程序的时候原来添加的节点还在.
- 关于iReport的问题
在此CLASS中用循环取连接也是最多10个,但是在其他CLASS中却是调用无论打开多少个连接都可以
我这里就没有10个最大连接那些限制,
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
但是当我用循环取出连接的时候(不关闭连接)取出的连接数超过了我设定的最大可用连接数,
如果用一个连接池的话应该限制会生效。max_connection: 10