一、数据库连接池
图1、使用连接池之前
图2 使用连接池之后
如上图1所示,在使用连接池之前,需要每次都对数据库建立链接,并且需要随时进行释放,在数据量大的情况下,需要很大的连接数据库的开销,并且频繁的对数据库进行访问以及释放,也会对数据库造成很大的压力,图2为使用数据库连接池之后,将所有的链接放在池子里,不进行释放,当用的时候直接从池子里去取,用完之后放回池子里,池子保持对数据库的长链接,链接断开会进行自动重连,如果连接不够那么对应后来的用户就需要进行等待
二、使用tomcat-dbcp所使用的jar包
包含tomcat-dbcp.jar即可,剩下的都是一些基础包
三、所使用的配置
dbname.Driver=com.mysql.jdbc.Driverdbname.Url=jdbc:mysql://<your ip>/<your dbname>?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull&connectTimeout=3000dbname.Username=<your username>dbname.Password=<your password>dbname.InitialSize=15dbname.MinIdle=10dbname.MaxIdle=20dbname.MaxWait=5000dbname.MaxActive=20dbname.validationQuery=select 1
其中这些配置只需要放在<yourname>.properties里面即可,关于每一个的意义
其中driver,url,username,password为常见的数据库连接的配置
InitialSize为初始化建立的连接数
minidle为数据库连接池中保持的最少的空闲的链接数
maxidle数据库连接池中保持的最大的连接数
maxwait等待数据库连接池分配连接的最长时间,超出之后报错
maxactivite最大的活动链接数,如果是多线程可以设置为超出多线程个数个链接数
<pre name="code" class="java">validationQuery测试是否连接是有效的sql语句
三、连接池代码
public abstract class DB { private static HashMap<String, DataSource> dsTable = new HashMap<String, DataSource>();//此处记得用static private BasicDataSource ds; private PreparedStatement stmt = null; private DataSource getDataSource(String n) { if (dsTable.containsKey(n)) { <em style="color:transparent">本文来源[email protected]搞@^&代*@码网(</em> return dsTable.get(n);//如果不同的数据库,多个连接池 } else { synchronized (dsTable) { ds = new BasicDataSource(); ds.setDriverClassName(DBConfig.getString("db", n.concat(".Driver")));//将<yourname>.properties的值读进来 ds.setUrl(DBConfig.getString("db", n.concat(".Url"))); ds.setUsername(DBConfig.getString("db", n.concat(".Username"))); ds.setPassword(DBConfig.getString("db", n.concat(".Password"))); ds.setInitialSize(DBConfig.getInteger("db", n.concat(".InitialSize"))); ds.setMinIdle(DBConfig.getInteger("db", n.concat(".MinIdle"))); ds.setMaxIdle(DBConfig.getInteger("db", n.concat(".MaxIdle"))); ds.setMaxWait(DBConfig.getInteger("db", n.concat(".MaxWait"))); ds.setMaxActive(DBConfig.getInteger("db", n.concat(".MaxActive"))); ds.setValidationQuery(DBConfig.getString("db", n.concat(".validationQuery"))); dsTable.put(n, ds); return ds; } } } protected Connection conn; public boolean open() throws SQLException { BasicDataSource bds=(BasicDataSource)this.getDataSource(this.getConnectionName()); System.out.println("connection_number:"+bds.getNumActive()+"dsTable:"+dsTable); this.conn = this.getDataSource(this.getConnectionName()).getConnection(); return true; } public void close() throws SQLException { if (this.conn != null) this.conn.close(); } protected abstract String getConnectionName();//此函数可以根据自己的需求,将数据库的名字传进来即可 public void prepareStatement(String sql) throws SQLException { this.stmt = this.conn.prepareStatement(sql); } public void setObject(int index, Object value, int type) throws SQLException { this.stmt.setObject(index, value, type); } public void setObject(int index, Object value) throws SQLException { this.stmt.setObject(index, value); } public int execute() throws SQLException { return this.stmt.executeUpdate(); }}
上述是线程池使用的时候所用到的代码,只是给出了大概的写法,具体的DBDAO部分需要根据自己的需求去自己实现,比如批处理,查询,更新等函数,可以根据个人的需求去进行修改,那么如何判断你所创建的链接是你想要的呢?有两种办法可以检验
1、建立一个空的数据库,查看链接个数
2、在linux下面查看链接个数
得到processid
ps aux|grep <your java name>
查看链接数据库的链接
netstat -apn|grep <your processid>
可以看到具体的链接的个数,用来检验是否你的链接池是正确的
四、遇到的一些坑
因为使用的时候是多线程形式使用的,遇到的最主要的一个坑就是static的用法,因为不是太熟,没用static,导致了每个线程都建立了一个数据库连接池,出现了一个“too many files open”的错误,这就是因为线程池那边没用static所导致的。
以上就是tomcat-dbcp数据库连接池配置以及使用时候的一些坑的内容,更多相关内容请关注搞代码(www.gaodaima.com)!