java中单例模式连接数据库_资源池模式和单例模式实现的⾃
定义数据库连接池java实现版...
在企业级开发中,我们往往不会直接使⽤原⽣的JDBC操作来实现与数据库得连接。因为数据库的连接是⼀个很宝贵的资源且耗时,我们往往会在内存中引⼊⼀个资源池来统⼀管理数据库的连接。这个模式也被总结为⼀种设计模式:资源池模式和单例模式。 关于原理部分就不多做介绍了,这⾥也是做⼀个简单的原理实现。
ObjectPool连接池接⼝
st.pool;
import java.util.Enumeration;
import java.util.Hashtable;
import urrent.ConcurrentHashMap;
public abstract class ObjectPool {
private long expirationTime;
private Hashtable locked, unlocked;
public ObjectPool() {
expirationTime = 30000; // 30 seconds
locked = new Hashtable();
unlocked = new Hashtable();
}
protected abstract T create();
public abstract boolean validate(T o);
public abstract void expire(T o);
public synchronized T checkOut() {
long now = System.currentTimeMillis();
T t;
if (unlocked.size() > 0) {
Enumeration e = unlocked.keys();
while (e.hasMoreElements()) {
t = e.nextElement();
if ((now - (t)) > expirationTime) {
// object has expired
expire(t);
t = null;
} else {
if (validate(t)) {
locked.put(t, now);
return (t);
} else {
// object failed validation
expire(t);
t = null;
}
}
}
}
// no objects available, create a new one
t = create();
locked.put(t, now);
return (t);
}
public synchronized void checkIn(T t) {
unlocked.put(t, System.currentTimeMillis());
}
}
JDBCConnectionPool 连接池实现
st.pool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCConnectionPool extends ObjectPool { private String driver, url, username, password;
private JDBCConnectionPool() {
}
private static volatile JDBCConnectionPool instance; public static JDBCConnectionPool getInstance() {
if(instance == null) {
synchronized (JDBCConnectionPool.class) {
if(instance == null) {
instance = new JDBCConnectionPool();
}
}
}
return instance;
}
@Override
protected Connection create() {
try {
return (Connection(url, username, password)); } catch (SQLException e) {
e.printStackTrace();
return (null);
}
}
@Override
public void expire(Connection o) {
try {
((Connection) o).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public boolean validate(Connection o) {
try {
return (!((Connection) o).isClosed());
} catch (SQLException e) {
e.printStackTrace();
return (false);
}
}
public String getDsn() {
return url;
}
public void setDsn(String dsn) { this.url = dsn;
}
public String getUsr() {
return username;
}
public void setUsr(String usr) {
this.username = usr;
}
public String getPwd() {
return password;
}
public void setPwd(String pwd) { this.password = pwd;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) { this.driver = driver;
}
public void loadDriver() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) { }
}
}
Main测试
st.pool;
import java.sql.Connection;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) throws SQLException {
JDBCConnectionPool pool = Instance();
pool.setDriver("sql.jdbc.Driver");
pool.setDsn("jdbc:mysql://192.168.2.113:3306/jhbims?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
pool.setUsr("jhbims");
pool.setPwd("jhbims");
pool.loadDriver();
// Get a connection:
Connection con = pool.checkOut();
单例模式的几种实现方式System.out.println(con.isValid(0));
pool.checkIn(con);
}
}
本⽂由博客⼀⽂多发平台 OpenWrite 发布!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论