您的位置:首页 > 数据库

jdbc 数据库连接池

2015-08-14 12:49 246 查看
jdbc连接池就是先从数据库中取出一定量的数据,放在池子(集合)中,然后客户端每次请求数据的时候就不用每次都关联数据库了,可以从数据库连接池中直接取数据,当连接池中数据没有了,先等一会,看有没有其他用户连接释放,如果有就直接将这个释放的连接给请求的用户,如果没有,那就从数据库中再取一些数据放入连接池中

连接池中数据量的决定,有很多影响因素,比如用户的并发量等

如果连接池中数据量过多,很多时候请求不到那些量,那程序就应该减少连接池中的量

下面给一个简单的例子(从网上下的,很简单,应该没什么问题)

import java.sql.*;

import java.util.*;

import java.util.concurrent.ConcurrentLinkedQueue;

public class DBPool {
private static DBPool pool;// 连接池
private static ConcurrentLinkedQueue<Connection> idlyConnections;// 空闲连接列表
private static List<Connection> usedConnections; // 已使用的连接
private static String driver = "";// 驱动
private static String url = "";// URL
private static String username = "";// 用户名
private static String password = "";// 密码
private static int max; // 最大连接数

// 初始化连接池配置
public static synchronized void configuration(String _driver, String _url,
String _name, String _pwd, int _max) throws Exception {
if (pool == null) {// 保证连接池只初始化一次
driver = _driver;
url = _url;
username = _name;
password = _pwd;
max = _max;
pool = new DBPool();
}
}

// singleton单例模式
private DBPool() throws Exception {
Class.forName(driver);
idlyConnections = new ConcurrentLinkedQueue<Connection>();
usedConnections = Collections
.synchronizedList(new ArrayList<Connection>());

// 预先建立连接,待用
for (int i = 0; i < max; i++) {
Connection conn = DriverManager.getConnection(url, username,
password);
idlyConnections.add(conn);
}
}

// 通过连接池取得数据库连接
public static Connection getConnection() throws java.sql.SQLException {
if (usedConnections.size() > max) {
throw new SQLException("连接数过多!");
}
Connection conn = idlyConnections.poll();//直接从池中取得建立好的连接,提高效率
if (conn == null) {
conn = DriverManager.getConnection(url, username, password);
}
usedConnections.add(conn);
return conn;
}

//释放连接
public static void releaseConnection(Connection conn)
throws java.sql.SQLException {
usedConnections.remove(conn);
idlyConnections.add(conn);
}

public static void main(String[] args) {

// DBPool.configuration(driver, url, name, pwd, max);

// Connection conn=DBPool.getConnection();
//do some thing

// DBPool.releaseConnection(conn);
}

}

还有一个应用的Demo

import java.io.File;

import java.io.IOException;

import java.sql.*;

import org.logicalcobwebs.proxool.ProxoolException;

import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public class ProxoolDemo {

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("proxool.AdventureWorks");
}

public void query() {
Connection conn=null;
try {
conn=ProxoolDemo.getConnection();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select getdate()");
if(rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
st.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (Exception e) {}
}

}

public static void main(String[] args) {

try {//初始化连接池
JAXPConfigurator.configure("proxool.xml", false);
} catch (ProxoolException e) {
e.printStackTrace();
}

new ProxoolDemo().query();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息