最近一个项目用了数据库连接池,在网上了解后选择了阿里的Druid,整个项目已经完成,不过对Druid的使用的正确性还是很疑虑,尤其是数据库连接的回收,下面是我的代码,请大神指点:
配置文件:
#我用的是mysql数据库 url=jdbc:mysql://127.0.0.1:3306/db_name username=dba_name password=dba_pwd filters=stat #最大连接数量 maxActive=500 #初始化连接数量 initialSize=50 #超时等待时间以毫秒为单位 maxWait=60000 #最小空闲连接 minIdle=1 #校验连接池中限制时间超过minEvictableIdleTimeMillis的连接对象 timeBetweenEvictionRunsMillis=3000 #连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒 minEvictableIdleTimeMillis=300000 #SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前 validationQuery=SELECT now(); #指明连接是否被空闲连接回收器(如果有)进行检验. #如果检测失败,则连接将被从池中去除. testWhileIdle=true #指明是否在从池中取出连接前进行检验,如果检验失败, #则从池中去除连接并尝试取出另一个. testOnBorrow=false #指明是否在归还到池中前进行检验 testOnReturn=false #poolPreparedStatements=true maxPoolPreparedStatementPerConnectionSize=20初始化连接:
import java.io.InputStream; import java.util.Properties; import java.util.Set; import javax.activation.DataSource; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; public class DataBasePoolUtils { private static DruidDataSource dds = null; static{ try { InputStream in = DataBasePoolUtils.class.getClassLoader().getResourceAsStream("/dbconfig.properties"); Properties p = new Properties(); p.load(in); dds = (DruidDataSource) DruidDataSourceFactory .createDataSource(p); } catch (Exception e) { e.printStackTrace(); } } public static DruidDataSource getDruidDataSource() { System.out.println("当前正在使用的连接个数 :" + dds.getActiveCount()); System.out.println("获取的DataBaseSiurce对象" + dds); return dds; } }
在程序中的使用与回收:
//获取连接 Connection con = DataBasePoolUtils.getDruidDataSource().getConnection(); //使用后释放连接 if(con != null){ con.close(); int a = DataBasePoolUtils.getDruidDataSource().removeAbandoned(); }
我的想求证的问题:由于网上相对应解决我的问题的资料比较少,所以用的很不踏实,上来问问:
1、数据库连接的配置有没有需要优化的地方;
2、数据库连接初始化的方式有没有问题;
3、连接的释放是否存在问题。
在线等。。。。。求帮助
补充,使用时获取连接时的打印信息:
获取的DataBaseSiurce对象{ CreateTime:"2015-04-30 09:00:25", ActiveCount:0, PoolingCount:50, CreateCount:50, DestroyCount:0, CloseCount:1, ConnectCount:1, Connections:[ {ID:193852045, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:338627816, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:486038962, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1976524793, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:27660508, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1033690204, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:88554398, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:127599815, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1381410999, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:928952476, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:698150442, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:2111315133, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:2001140950, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1434550983, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:699466412, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:606502806, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1535271780, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1296787755, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:300264882, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:2112784971, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:934904452, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1948763312, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1591884387, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:1449617983, ConnectTime:"2015-04-30 09:00:26", UseCount:0, LastActiveTime:"2015-04-30 09:00:26"}, {ID:527465066, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:292869446, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:224259998, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:303985901, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:227441894, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:95020798, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1434202265, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1281013927, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1477509914, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1155714337, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:101274844, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1319254684, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:2092302360, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:281044895, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1205010512, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1840883974, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:940048689, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:361125044, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:639828086, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1140020873, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:481713977, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:153462249, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:112158896, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1711406653, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:215649053, ConnectTime:"2015-04-30 09:00:27", UseCount:0, LastActiveTime:"2015-04-30 09:00:27"}, {ID:1608721282, ConnectTime:"2015-04-30 09:00:27", UseCount:1, LastActiveTime:"2015-04-30 09:00:27"} ] }
这里面有一个CloseCount,我在想是不是每次都关闭了,这个就没有起到了数据库连接池的作用了,但是如果我不关闭的话连接很快就会被用完。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。