import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
public class JdbcPool implements DataSource{
private static final Log log = LogUtils.getLog(JdbcPool.class);
/**
* @Field: listConnections
* 使用LinkedList集合来存放数据库链接,
* 由于要频繁读写List集合,所以这里使用LinkedList存储数据库连接比较合适
*/
// private static LinkedList<Connection> listConnections = new LinkedList<Connection>();
private static ConcurrentLinkedQueue<Connection> listConnections = new ConcurrentLinkedQueue<Connection>();
private final static String filepath = Path.getCurrentPath() + "/config/db.properties";
static{
//在静态代码块中加载db.properties数据库配置文件
File file = new File(filepath);
Properties prop = new Properties();
try {
InputStream in= new FileInputStream(file);
prop.load(in);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//数据库连接池的初始化连接数大小
int jdbcPoolInitSize =Integer.parseInt(prop.getProperty("jdbcPoolInitSize"));
//加载数据库驱动
Class.forName(driver);
for (int i = 0; i < jdbcPoolInitSize; i++) {
Connection conn = DriverManager.getConnection(url, username, password);
//将获取到的数据库连接加入到listConnections集合中,listConnections集合此时就是一个存放了数据库连接的连接池
listConnections.add(conn);
}
} catch (Exception e) {
e.printStackTrace();
log.error(e);
}
}
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
/* 获取数据库连接
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() throws SQLException {
// 如果数据库连接池中的连接对象的个数大于0
if (listConnections.size() > 0) {
// if (!listConnections.isEmpty()) {
// 从listConnections集合中获取一个数据库连接
final Connection conn = listConnections.poll();
// 返回Connection对象的代理对象
return (Connection) Proxy.newProxyInstance(JdbcPool.class.getClassLoader(),
new Class[] { Connection.class }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().equals("close")) {
return method.invoke(conn, args);
} else {
// 如果调用的是Connection对象的close方法,就把conn还给数据库连接池
listConnections.add(conn);
return null;
}
}
});
} else {
return null;
}
}
public Connection getConnection(String username, String password)
throws SQLException {
return null;
}
/**
* 获取多线程的线程池大小
* @return
*/
public static int getExecutorPoolSize(){
return executorPoolSize;
}
/**
* 获取多线程中每个任务中每次查询的数据条数
* @return
*/
public static int getQuerySizeOneTime(){
return querySizeOneTime;
}
/**
* 获取redis数据存储的有效时间
* @return
*/
public static int getExpireTime(){
return expiretime;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
线程安全的jdbc连接池
共2个文件
java:2个
需积分: 21 16 下载量 62 浏览量
2015-12-08
17:59:51
上传
评论
收藏 3KB RAR 举报
温馨提示
一个简单的jdbc连接池,考虑到线程安全,这里使用了ConcurrentLinkedQueue,新手入门,简单易懂
资源推荐
资源详情
资源评论
收起资源包目录
1.rar (2个子文件)
JdbcUtil.java 2KB
JdbcPool.java 5KB
共 2 条
- 1
资源评论
m20103410
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功