java的几种连接池
连接池的管理用了了享元模式,这里对连接池进行简单设计。
一、设计思路
1.连接池配置属性DBbean:里面存放可以配置的一些属性
2.连接池接口IConnectionPool:里面定义一些基本的获取连接的一些方法
3.接口实现ConnectionPool:对上面操作进行实现,并加入一些其他方法
4.连接池管理ConnectionPoolManager:管理所有的不同的连接池,所有的连接都能通过这里进行获得连接
5.另外还有几个测试类,和连接信息模拟的类,这里就不进行xml 和配置文件信息的读取了
- package pool;
- /**
- * 这是外部可以配置的连接池属性
- * 可以允许外部配置,拥有默认值
- * @author Ran
- *
- */
- public class DBbean {
- // 连接池属性
- private String driverName;
- private String url;
- private String userName;
- private String password;
- // 连接池名字
- private String poolName;
- private int minConnections = ; // 空闲池,最小连接数
- private int maxConnections = ; // 空闲池,最大连接数
- private int initConnections = ;// 初始化连接数
- private long connTimeOut = ;// 重复获得连接的频率
- private int maxActiveConnections = ;// 最大允许的连接数,和数据库对应
- private long connectionTimeOut = **;// 连接超时时间,默认20分钟
- private boolean isCurrentConnection = true; // 是否获得当前连接,默认true
- private boolean isCheakPool = true; // 是否定时检查连接池
- private long lazyCheck = **;// 延迟多少时间后开始 检查
- private long periodCheck = **;// 检查频率
- public DBbean(String driverName, String url, String userName,
- String password, String poolName) {
- super();
- this.driverName = driverName;
- this.url = url;
- this.userName = userName;
- this.password = password;
- this.poolName = poolName;
- }
- public DBbean() {
- }
- public String getDriverName() {
- if(driverName == null){
- driverName = this.getDriverName()+"_"+this.getUrl();
- }
- return driverName;
- }
- public void setDriverName(String driverName) {
- this.driverName = driverName;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getPoolName() {
- return poolName;
- }
- public void setPoolName(String poolName) {
- this.poolName = poolName;
- }
- public int getMinConnections() {
- return minConnections;
- }
- public void setMinConnections(int minConnections) {
- this.minConnections = minConnections;
- }
- public int getMaxConnections() {
- return maxConnections;
- }
- public void setMaxConnections(int maxConnections) {
- this.maxConnections = maxConnections;
- }
- public int getInitConnections() {
- return initConnections;
- }
- public void setInitConnections(int initConnections) {
- this.initConnections = initConnections;
- }
- public int getMaxActiveConnections() {
- return maxActiveConnections;
- }
- public void setMaxActiveConnections(int maxActiveConnections) {
- this.maxActiveConnections = maxActiveConnections;
- }
- public long getConnTimeOut() {
- return connTimeOut;
- }
- public void setConnTimeOut(long connTimeOut) {
- this.connTimeOut = connTimeOut;
- }
- public long getConnectionTimeOut() {
- return connectionTimeOut;
- }
- public void setConnectionTimeOut(long connectionTimeOut) {
- this.connectionTimeOut = connectionTimeOut;
- }
- public boolean isCurrentConnection() {
- return isCurrentConnection;
- }
- public void setCurrentConnection(boolean isCurrentConnection) {
- this.isCurrentConnection = isCurrentConnection;
- }
- public long getLazyCheck() {
- return lazyCheck;
- }
- public void setLazyCheck(long lazyCheck) {
- this.lazyCheck = lazyCheck;
- }
- public long getPeriodCheck() {
- return periodCheck;
- }
- public void setPeriodCheck(long periodCheck) {
- this.periodCheck = periodCheck;
- }
- public boolean isCheakPool() {
- return isCheakPool;
- }
- public void setCheakPool(boolean isCheakPool) {
- this.isCheakPool = isCheakPool;
- }
- }
package pool;
/**
* 这是外部可以配置的连接池属性
* 可以允许外部配置,拥有默认值
* @author Ran
*
*/
public class DBbean {
// 连接池属性
private String driverName;
private String url;
private String userName;
private String password;
// 连接池名字
private String poolName;
private int minConnections = 1; // 空闲池,最小连接数
private int maxConnections = 10; // 空闲池,最大连接数 private int initConnections = 5;// 初始化连接数 private long connTimeOut = 1000;// 重复获得连接的频率 private int maxActiveConnections = 100;// 最大允许的连接数,和数据库对应 private long connectionTimeOut = 1000*60*20;// 连接超时时间,默认20分钟 private boolean isCurrentConnection = true; // 是否获得当前连接,默认true private boolean isCheakPool = true; // 是否定时检查连接池
private long lazyCheck = 1000*60*60;// 延迟多少时间后开始 检查
private long periodCheck = 1000*60*60;// 检查频率 public DBbean(String driverName, String url, String userName,
String password, String poolName) {
super();
this.driverName = driverName;
this.url = url;
this.userName = userName;
this.password = password;
this.poolName = poolName;
}
public DBbean() {
}
public String getDriverName() {
if(driverName == null){
driverName = this.getDriverName()+"_"+this.getUrl();
}
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPoolName() {
return poolName;
}
public void setPoolName(String poolName) {
this.poolName = poolName;
}
public int getMinConnections() {
return minConnections;
}
public void setMinConnections(int minConnections) {
this.minConnections = minConnections;
}
public int getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
public int getInitConnections() {
return initConnections;
}
public void setInitConnections(int initConnections) {
this.initConnections = initConnections;
} public int getMaxActiveConnections() {
return maxActiveConnections;
}
public void setMaxActiveConnections(int maxActiveConnections) {
this.maxActiveConnections = maxActiveConnections;
}
public long getConnTimeOut() {
return connTimeOut;
}
public void setConnTimeOut(long connTimeOut) {
this.connTimeOut = connTimeOut;
}
public long getConnectionTimeOut() {
return connectionTimeOut;
}
public void setConnectionTimeOut(long connectionTimeOut) {
this.connectionTimeOut = connectionTimeOut;
}
public boolean isCurrentConnection() {
return isCurrentConnection;
}
public void setCurrentConnection(boolean isCurrentConnection) {
this.isCurrentConnection = isCurrentConnection;
}
public long getLazyCheck() {
return lazyCheck;
}
public void setLazyCheck(long lazyCheck) {
this.lazyCheck = lazyCheck;
}
public long getPeriodCheck() {
return periodCheck;
}
public void setPeriodCheck(long periodCheck) {
this.periodCheck = periodCheck;
}
public boolean isCheakPool() {
return isCheakPool;
}
public void setCheakPool(boolean isCheakPool) {
this.isCheakPool = isCheakPool;
} }
- package pool;
- import java.sql.Connection;
- import java.sql.SQLException;
- public interface IConnectionPool {
- // 获得连接
- public Connection getConnection();
- // 获得当前连接
- public Connection getCurrentConnecton();
- // 回收连接
- public void releaseConn(Connection conn) throws SQLException;
- // 销毁清空
- public void destroy();
- // 连接池是活动状态
- public boolean isActive();
- // 定时器,检查连接池
- public void cheackPool();
- }
package pool; import java.sql.Connection;
import java.sql.SQLException; public interface IConnectionPool {
// 获得连接
public Connection getConnection();
// 获得当前连接
public Connection getCurrentConnecton();
// 回收连接
public void releaseConn(Connection conn) throws SQLException;
// 销毁清空
public void destroy();
// 连接池是活动状态
public boolean isActive();
// 定时器,检查连接池
public void cheackPool();
}
- package pool;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.Vector;
- public class ConnectionPool implements IConnectionPool {
- // 连接池配置属性
- private DBbean dbBean;
- private boolean isActive = false; // 连接池活动状态
- private int contActive = ;// 记录创建的总的连接数
- // 空闲连接
- private List<Connection> freeConnection = new Vector<Connection>();
- // 活动连接
- private List<Connection> activeConnection = new Vector<Connection>();
- // 将线程和连接绑定,保证事务能统一执行
- private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
- public ConnectionPool(DBbean dbBean) {
- super();
- this.dbBean = dbBean;
- init();
- cheackPool();
- }
- // 初始化
- public void init() {
- try {
- Class.forName(dbBean.getDriverName());
- for (int i = ; i < dbBean.getInitConnections(); i++) {
- Connection conn;
- conn = newConnection();
- // 初始化最小连接数
- if (conn != null) {
- freeConnection.add(conn);
- contActive++;
- }
- }
- isActive = true;
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // 获得当前连接
- public Connection getCurrentConnecton(){
- // 默认线程里面取
- Connection conn = threadLocal.get();
- if(!isValid(conn)){
- conn = getConnection();
- }
- return conn;
- }
- // 获得连接
- public synchronized Connection getConnection() {
- Connection conn = null;
- try {
- // 判断是否超过最大连接数限制
- if(contActive < this.dbBean.getMaxActiveConnections()){
- if (freeConnection.size() > ) {
- conn = freeConnection.get();
- if (conn != null) {
- threadLocal.set(conn);
- }
- freeConnection.remove();
- } else {
- conn = newConnection();
- }
- }else{
- // 继续获得连接,直到从新获得连接
- wait(this.dbBean.getConnTimeOut());
- conn = getConnection();
- }
- if (isValid(conn)) {
- activeConnection.add(conn);
- contActive ++;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return conn;
- }
- // 获得新连接
- private synchronized Connection newConnection()
- throws ClassNotFoundException, SQLException {
- Connection conn = null;
- if (dbBean != null) {
- Class.forName(dbBean.getDriverName());
- conn = DriverManager.getConnection(dbBean.getUrl(),
- dbBean.getUserName(), dbBean.getPassword());
- }
- return conn;
- }
- // 释放连接
- public synchronized void releaseConn(Connection conn) throws SQLException {
- if (isValid(conn)&& !(freeConnection.size() > dbBean.getMaxConnections())) {
- freeConnection.add(conn);
- activeConnection.remove(conn);
- contActive --;
- threadLocal.remove();
- // 唤醒所有正待等待的线程,去抢连接
- notifyAll();
- }
- }
- // 判断连接是否可用
- private boolean isValid(Connection conn) {
- try {
- if (conn == null || conn.isClosed()) {
- return false;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return true;
- }
- // 销毁连接池
- public synchronized void destroy() {
- for (Connection conn : freeConnection) {
- try {
- if (isValid(conn)) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- for (Connection conn : activeConnection) {
- try {
- if (isValid(conn)) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- isActive = false;
- contActive = ;
- }
- // 连接池状态
- @Override
- public boolean isActive() {
- return isActive;
- }
- // 定时检查连接池情况
- @Override
- public void cheackPool() {
- if(dbBean.isCheakPool()){
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- // 1.对线程里面的连接状态
- // 2.连接池最小 最大连接数
- // 3.其他状态进行检查,因为这里还需要写几个线程管理的类,暂时就不添加了
- System.out.println("空线池连接数:"+freeConnection.size());
- System.out.println("活动连接数::"+activeConnection.size());
- System.out.println("总的连接数:"+contActive);
- }
- },dbBean.getLazyCheck(),dbBean.getPeriodCheck());
- }
- }
- }
package pool; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector; public class ConnectionPool implements IConnectionPool {
// 连接池配置属性
private DBbean dbBean;
private boolean isActive = false; // 连接池活动状态
private int contActive = 0;// 记录创建的总的连接数 // 空闲连接
private List<Connection> freeConnection = new Vector<Connection>();
// 活动连接
private List<Connection> activeConnection = new Vector<Connection>();
// 将线程和连接绑定,保证事务能统一执行
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); public ConnectionPool(DBbean dbBean) {
super();
this.dbBean = dbBean;
init();
cheackPool();
} // 初始化
public void init() {
try {
Class.forName(dbBean.getDriverName());
for (int i = 0; i < dbBean.getInitConnections(); i++) {
Connection conn;
conn = newConnection();
// 初始化最小连接数
if (conn != null) {
freeConnection.add(conn);
contActive++;
}
}
isActive = true;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} // 获得当前连接
public Connection getCurrentConnecton(){
// 默认线程里面取
Connection conn = threadLocal.get();
if(!isValid(conn)){
conn = getConnection();
}
return conn;
} // 获得连接
public synchronized Connection getConnection() {
Connection conn = null;
try {
// 判断是否超过最大连接数限制
if(contActive < this.dbBean.getMaxActiveConnections()){
if (freeConnection.size() > 0) {
conn = freeConnection.get(0);
if (conn != null) {
threadLocal.set(conn);
}
freeConnection.remove(0);
} else {
conn = newConnection();
} }else{
// 继续获得连接,直到从新获得连接
wait(this.dbBean.getConnTimeOut());
conn = getConnection();
}
if (isValid(conn)) {
activeConnection.add(conn);
contActive ++;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return conn;
} // 获得新连接
private synchronized Connection newConnection()
throws ClassNotFoundException, SQLException {
Connection conn = null;
if (dbBean != null) {
Class.forName(dbBean.getDriverName());
conn = DriverManager.getConnection(dbBean.getUrl(),
dbBean.getUserName(), dbBean.getPassword());
}
return conn;
} // 释放连接
public synchronized void releaseConn(Connection conn) throws SQLException {
if (isValid(conn)&& !(freeConnection.size() > dbBean.getMaxConnections())) {
freeConnection.add(conn);
activeConnection.remove(conn);
contActive --;
threadLocal.remove();
// 唤醒所有正待等待的线程,去抢连接
notifyAll();
}
} // 判断连接是否可用
private boolean isValid(Connection conn) {
try {
if (conn == null || conn.isClosed()) {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return true;
} // 销毁连接池
public synchronized void destroy() {
for (Connection conn : freeConnection) {
try {
if (isValid(conn)) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
for (Connection conn : activeConnection) {
try {
if (isValid(conn)) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
isActive = false;
contActive = 0;
} // 连接池状态
@Override
public boolean isActive() {
return isActive;
} // 定时检查连接池情况
@Override
public void cheackPool() {
if(dbBean.isCheakPool()){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// 1.对线程里面的连接状态
// 2.连接池最小 最大连接数
// 3.其他状态进行检查,因为这里还需要写几个线程管理的类,暂时就不添加了
System.out.println("空线池连接数:"+freeConnection.size());
System.out.println("活动连接数::"+activeConnection.size());
System.out.println("总的连接数:"+contActive);
}
},dbBean.getLazyCheck(),dbBean.getPeriodCheck());
}
}
}
- package pool;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Hashtable;
- /**
- * 连接管理类
- * @author Ran
- *
- */
- public class ConnectionPoolManager {
- // 连接池存放
- public Hashtable<String,IConnectionPool> pools = new Hashtable<String, IConnectionPool>();
- // 初始化
- private ConnectionPoolManager(){
- init();
- }
- // 单例实现
- public static ConnectionPoolManager getInstance(){
- return Singtonle.instance;
- }
- private static class Singtonle {
- private static ConnectionPoolManager instance = new ConnectionPoolManager();
- }
- // 初始化所有的连接池
- public void init(){
- for(int i =;i<DBInitInfo.beans.size();i++){
- DBbean bean = DBInitInfo.beans.get(i);
- ConnectionPool pool = new ConnectionPool(bean);
- if(pool != null){
- pools.put(bean.getPoolName(), pool);
- System.out.println("Info:Init connection successed ->" +bean.getPoolName());
- }
- }
- }
- // 获得连接,根据连接池名字 获得连接
- public Connection getConnection(String poolName){
- Connection conn = null;
- if(pools.size()> && pools.containsKey(poolName)){
- conn = getPool(poolName).getConnection();
- }else{
- System.out.println("Error:Can't find this connecion pool ->"+poolName);
- }
- return conn;
- }
- // 关闭,回收连接
- public void close(String poolName,Connection conn){
- IConnectionPool pool = getPool(poolName);
- try {
- if(pool != null){
- pool.releaseConn(conn);
- }
- } catch (SQLException e) {
- System.out.println("连接池已经销毁");
- e.printStackTrace();
- }
- }
- // 清空连接池
- public void destroy(String poolName){
- IConnectionPool pool = getPool(poolName);
- if(pool != null){
- pool.destroy();
- }
- }
- // 获得连接池
- public IConnectionPool getPool(String poolName){
- IConnectionPool pool = null;
- if(pools.size() > ){
- pool = pools.get(poolName);
- }
- return pool;
- }
- }
package pool; import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
/**
* 连接管理类
* @author Ran
*
*/
public class ConnectionPoolManager { // 连接池存放
public Hashtable<String,IConnectionPool> pools = new Hashtable<String, IConnectionPool>(); // 初始化
private ConnectionPoolManager(){
init();
}
// 单例实现
public static ConnectionPoolManager getInstance(){
return Singtonle.instance;
}
private static class Singtonle {
private static ConnectionPoolManager instance = new ConnectionPoolManager();
} // 初始化所有的连接池
public void init(){
for(int i =0;i<DBInitInfo.beans.size();i++){
DBbean bean = DBInitInfo.beans.get(i);
ConnectionPool pool = new ConnectionPool(bean);
if(pool != null){
pools.put(bean.getPoolName(), pool);
System.out.println("Info:Init connection successed ->" +bean.getPoolName());
}
}
} // 获得连接,根据连接池名字 获得连接
public Connection getConnection(String poolName){
Connection conn = null;
if(pools.size()>0 && pools.containsKey(poolName)){
conn = getPool(poolName).getConnection();
}else{
System.out.println("Error:Can't find this connecion pool ->"+poolName);
}
return conn;
} // 关闭,回收连接
public void close(String poolName,Connection conn){
IConnectionPool pool = getPool(poolName);
try {
if(pool != null){
pool.releaseConn(conn);
}
} catch (SQLException e) {
System.out.println("连接池已经销毁");
e.printStackTrace();
}
} // 清空连接池
public void destroy(String poolName){
IConnectionPool pool = getPool(poolName);
if(pool != null){
pool.destroy();
}
} // 获得连接池
public IConnectionPool getPool(String poolName){
IConnectionPool pool = null;
if(pools.size() > 0){
pool = pools.get(poolName);
}
return pool;
}
}
- package pool;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 初始化,模拟加载所有的配置文件
- * @author Ran
- *
- */
- public class DBInitInfo {
- public static List<DBbean> beans = null;
- static{
- beans = new ArrayList<DBbean>();
- // 这里数据 可以从xml 等配置文件进行获取
- // 为了测试,这里我直接写死
- DBbean beanOracle = new DBbean();
- beanOracle.setDriverName("oracle.jdbc.driver.OracleDriver");
- beanOracle.setUrl("jdbc:oracle:thin:@7MEXGLUY95W1Y56:1521:orcl");
- beanOracle.setUserName("mmsoa");
- beanOracle.setPassword("password1234");
- beanOracle.setMinConnections();
- beanOracle.setMaxConnections();
- beanOracle.setPoolName("testPool");
- beans.add(beanOracle);
- }
- }
package pool; import java.util.ArrayList;
import java.util.List;
/**
* 初始化,模拟加载所有的配置文件
* @author Ran
*
*/
public class DBInitInfo {
public static List<DBbean> beans = null;
static{
beans = new ArrayList<DBbean>();
// 这里数据 可以从xml 等配置文件进行获取
// 为了测试,这里我直接写死
DBbean beanOracle = new DBbean();
beanOracle.setDriverName("oracle.jdbc.driver.OracleDriver");
beanOracle.setUrl("jdbc:oracle:thin:@7MEXGLUY95W1Y56:1521:orcl");
beanOracle.setUserName("mmsoa");
beanOracle.setPassword("password1234"); beanOracle.setMinConnections(5);
beanOracle.setMaxConnections(100); beanOracle.setPoolName("testPool");
beans.add(beanOracle);
}
}
测试:
- package pool;
- import java.sql.Connection;
- /**
- * 模拟线程启动,去获得连接
- * @author Ran
- *
- */
- public class ThreadConnection implements Runnable{
- private IConnectionPool pool;
- @Override
- public void run() {
- pool = ConnectionPoolManager.getInstance().getPool("testPool");
- }
- public Connection getConnection(){
- Connection conn = null;
- if(pool != null && pool.isActive()){
- conn = pool.getConnection();
- }
- return conn;
- }
- public Connection getCurrentConnection(){
- Connection conn = null;
- if(pool != null && pool.isActive()){
- conn = pool.getCurrentConnecton();
- }
- return conn;
- }
- }
package pool; import java.sql.Connection;
/**
* 模拟线程启动,去获得连接
* @author Ran
*
*/
public class ThreadConnection implements Runnable{
private IConnectionPool pool;
@Override
public void run() {
pool = ConnectionPoolManager.getInstance().getPool("testPool");
} public Connection getConnection(){
Connection conn = null;
if(pool != null && pool.isActive()){
conn = pool.getConnection();
}
return conn;
} public Connection getCurrentConnection(){
Connection conn = null;
if(pool != null && pool.isActive()){
conn = pool.getCurrentConnecton();
}
return conn;
}
}
- package pool;
- public class Client {
- public static void main(String[] args) throws InterruptedException {
- // 初始化连接池
- Thread t = init();
- t.start();
- t.join();
- ThreadConnection a = new ThreadConnection();
- ThreadConnection b = new ThreadConnection();
- ThreadConnection c = new ThreadConnection();
- Thread t1 = new Thread(a);
- Thread t2 = new Thread(b);
- Thread t3 = new Thread(c);
- // 设置优先级,先让初始化执行,模拟 线程池 先启动
- // 这里仅仅表面控制了,因为即使t 线程先启动,也不能保证pool 初始化完成,为了简单模拟,这里先这样写了
- t1.setPriority();
- t2.setPriority();
- t3.setPriority();
- t1.start();
- t2.start();
- t3.start();
- System.out.println("线程A-> "+a.getConnection());
- System.out.println("线程B-> "+b.getConnection());
- System.out.println("线程C-> "+c.getConnection());
- }
- // 初始化
- public static Thread init() {
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- IConnectionPool pool = initPool();
- while(pool == null || !pool.isActive()){
- pool = initPool();
- }
- }
- });
- return t;
- }
- public static IConnectionPool initPool(){
- return ConnectionPoolManager.getInstance().getPool("testPool");
- }
- }
package pool; public class Client {
public static void main(String[] args) throws InterruptedException {
// 初始化连接池
Thread t = init();
t.start();
t.join(); ThreadConnection a = new ThreadConnection();
ThreadConnection b = new ThreadConnection();
ThreadConnection c = new ThreadConnection();
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
Thread t3 = new Thread(c); // 设置优先级,先让初始化执行,模拟 线程池 先启动
// 这里仅仅表面控制了,因为即使t 线程先启动,也不能保证pool 初始化完成,为了简单模拟,这里先这样写了
t1.setPriority(10);
t2.setPriority(10);
t3.setPriority(10);
t1.start();
t2.start();
t3.start(); System.out.println("线程A-> "+a.getConnection());
System.out.println("线程B-> "+b.getConnection());
System.out.println("线程C-> "+c.getConnection());
} // 初始化
public static Thread init() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
IConnectionPool pool = initPool();
while(pool == null || !pool.isActive()){
pool = initPool();
}
}
});
return t;
} public static IConnectionPool initPool(){
return ConnectionPoolManager.getInstance().getPool("testPool");
} }
小结 :
1.连接池诞生原因是,如果每次都从数据库获得连接,时间比较长,因此我们提前做建立一些连接,放在连接池里面,每次都从里面取
2.上面仅仅写了连接池基本原理,关于多线程下连接池的管理没写,后面对多线程操作熟练了添加吧
java的几种连接池的更多相关文章
- Java自己动手写连接池四
Java自己动手写连接池四 测试: package com.kama.cn; import java.sql.Connection; public class Test { public static ...
- Java自己动手写连接池三
Java自己动手写连接池三,核心代码; package com.kama.cn; import java.sql.Connection;import java.util.ArrayList;impor ...
- java操作mongodb(连接池)(转)
原文链接: java操作mongodb(连接池) Mongo的实例其实就是一个数据库连接池,这个连接池里默认有10个链接.我们没有必要重新实现这个链接池,但是我们可以更改这个连接池的配置.因为Mong ...
- Java操作数据库——使用连接池连接数据库
Java操作数据库——使用连接池连接数据库 摘要:本文主要学习了如何使用JDBC连接池连接数据库. 传统方式和连接池方式 传统方式的步骤 使用传统方式在Java中使用JDBC连接数据库,完成一次数据库 ...
- JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件
JdbcTemplate的使用 Spring为了各种支持的持久化技术,都提供了简单操作的模版和回调. JdbcTemplate 简化 JDBC 操作HibernateTemplate 简化 Hiber ...
- JAVA中事物以及连接池
一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...
- Java中使用C3P0连接池
先看官网给的范例: import java.sql.*; import javax.naming.*; import javax.sql.DataSource; import com.mchange. ...
- java学习笔记—标准连接池的实现(27)
javax.sql.DataSource. Java.sql.* DataSource 接口由驱动程序供应商实现.共有三种类型的实现: 基本实现 - 生成标准的 Connection 对象 – 一个D ...
- java操作redis redis连接池
redis作为缓存型数据库,越来越受到大家的欢迎,这里简单介绍一下java如何操作redis. 1.java连接redis java通过需要jedis的jar包获取Jedis连接. jedis-2.8 ...
随机推荐
- Effective C++ -----条款32:确定你的public继承塑模出is-a关系
“public继承”意味is-a.适用于base classes身上的每一件事情一定也适用于derived classes身上,因为每一个derive class对象也都是一个base class对象 ...
- 【QT】C++ GUI Qt4 学习笔记4
感觉这本书的顺序设计的太不合理了,出现的最多的一句话就是后面会讲.按照使用的顺序讲不行吗?搞得代码都运行不了. 我决定先直接跳到73页,子类化QTableWidgetItem这一节.因为前面功能的实现 ...
- 【hadoop2.6.0】数据丢失问题解决
想自己走一遍从代码到运行的流程,结果各种错,郁闷啊. 问题① http://localhost:50070/ 里面一进去就告诉我块丢了.... 解决: bin/hadoop fsck -delete ...
- IOS-Uikit框架介绍
•UIKit可识别三种类型的输入事件: –触摸事件 –运动(加速计)事件 –远程控制事件 IKit框架将触击信息封装为一个UIEvent对象,并派发给恰当的视图(有关UIKit如何将事件递送给您的视图 ...
- Div内部的内容超出部分显示省略号(仅仅只有一行内容)
效果如下:
- function与感叹号
原文链接:https://swordair.com/function-and-exclamation-mark/ 最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2 ...
- setw()函数
- JavaScript基础——实现循环
循环是多次执行同一段代码的一种手段.当你需要在一个数组或对象集上重复执行相同的任务时,这是非常有用的. JavaScript提供执行for和while循环的功能. 1.while循环 JavaScri ...
- 【翻译九】java-同步方法
Synchronized Methods The Java programming language provides two basic synchronization idioms: synchr ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...