[align=left][/align]首先把连接数据库的底层操作叼出来:
package com.sun.mydbpool;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Connector {
private static String driver;
private static String url;
private static String username;
private static String password;
private static Properties props;
public Connector(Properties props){
this.props = props;
try{
driver = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
}
然后再开发那个线程管理类:
(1)这个类的维护他自己的一个对象,用单例模式控制。
(2)然后建一个内部类,这是连接池的实现。
这个内部类维护一个集合,这个集合用于装载所有可用的连接对象,然后设计几个同步的方法进行管理 思路是 这个类初始化的时候就往这个集合添加一个初始容量的谅解对象,这样当程序要用的时候可以马上提供,不宜添加太多 导致程序启动的时候比较慢 尤其在游戏的底层编程当中。
下面是实现代码:有待测试 呵呵。不过思路还是没问题的。
package com.sun.mydbpool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
public class PoolManager {
private static PoolManager instance;
private Properties props;
private static Hashtable<String, DBConnectionPool> pools = new Hashtable<String, DBConnectionPool>();
private PoolManager(){
}
public static PoolManager getInstance(){
if(instance == null){
instance = new PoolManager();
}
return instance;
}
class DBConnectionPool{
private Properties props;
private int maxConn;
private int initSize;
private int checkout = 0;
private Vector<Connection> freeConnections = new Vector<Connection>();
//在构造器中初始化一个初始连接池,容量可配置。
public DBConnectionPool(int maxConn,int initSize,Properties props){
this.props = props;
this.maxConn = maxConn;
this.initSize = initSize;
if(initSize < maxConn){
for(int i = 0;i<initSize;i++){
try {
freeConnections.add(newConnection(this.props));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//当连接对象用完了 不再需要的时候将它返回到这个空闲连接集合当中
public synchronized void freeConnection(Connection conn){
freeConnections.add(conn);
checkout --;
notifyAll();
}
public synchronized Connection getConnection() throws SQLException{
//1.检测freeConnection里面是否有空余连接,有则返回,没有再检测已经checkou的数目是否超出最大值限制
Connection con= null;
if(freeConnections.size() > 0 ){
con = (Connection) freeConnections.elementAt(0);
freeConnections.remove(0);
//检测取得的连接对象是否为空,空不可用
if(con.isClosed()){
//自调用 再取一次
getConnection();
}
return con;
}else{
if(checkout < maxConn ){
con = newConnection(this.props);
}
}
return con;
}
public synchronized Connection getConnection(long waitTime) throws SQLException{
long startTime = new Date().getTime();
Connection conn = null;
while((conn = getConnection()) == null){
try {
wait(waitTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = new Date().getTime();
if((endTime - startTime)>waitTime){
return null;
}
}
return conn;
}
public synchronized void realeasePool(){
Enumeration allConnections = freeConnections.elements();
while(allConnections.hasMoreElements()){
Connection conn = (Connection)(allConnections.nextElement());
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
freeConnections.removeAllElements();
}
private synchronized Connection newConnection(Properties props) throws SQLException{
return new Connector(props).getConnection();
}
}
}
package com.sun.mydbpool;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class Connector {
private static String driver;
private static String url;
private static String username;
private static String password;
private static Properties props;
public Connector(Properties props){
this.props = props;
try{
driver = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
Class.forName(driver);
}catch(Exception e){
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
}
然后再开发那个线程管理类:
(1)这个类的维护他自己的一个对象,用单例模式控制。
(2)然后建一个内部类,这是连接池的实现。
这个内部类维护一个集合,这个集合用于装载所有可用的连接对象,然后设计几个同步的方法进行管理 思路是 这个类初始化的时候就往这个集合添加一个初始容量的谅解对象,这样当程序要用的时候可以马上提供,不宜添加太多 导致程序启动的时候比较慢 尤其在游戏的底层编程当中。
下面是实现代码:有待测试 呵呵。不过思路还是没问题的。
package com.sun.mydbpool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
public class PoolManager {
private static PoolManager instance;
private Properties props;
private static Hashtable<String, DBConnectionPool> pools = new Hashtable<String, DBConnectionPool>();
private PoolManager(){
}
public static PoolManager getInstance(){
if(instance == null){
instance = new PoolManager();
}
return instance;
}
class DBConnectionPool{
private Properties props;
private int maxConn;
private int initSize;
private int checkout = 0;
private Vector<Connection> freeConnections = new Vector<Connection>();
//在构造器中初始化一个初始连接池,容量可配置。
public DBConnectionPool(int maxConn,int initSize,Properties props){
this.props = props;
this.maxConn = maxConn;
this.initSize = initSize;
if(initSize < maxConn){
for(int i = 0;i<initSize;i++){
try {
freeConnections.add(newConnection(this.props));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//当连接对象用完了 不再需要的时候将它返回到这个空闲连接集合当中
public synchronized void freeConnection(Connection conn){
freeConnections.add(conn);
checkout --;
notifyAll();
}
public synchronized Connection getConnection() throws SQLException{
//1.检测freeConnection里面是否有空余连接,有则返回,没有再检测已经checkou的数目是否超出最大值限制
Connection con= null;
if(freeConnections.size() > 0 ){
con = (Connection) freeConnections.elementAt(0);
freeConnections.remove(0);
//检测取得的连接对象是否为空,空不可用
if(con.isClosed()){
//自调用 再取一次
getConnection();
}
return con;
}else{
if(checkout < maxConn ){
con = newConnection(this.props);
}
}
return con;
}
public synchronized Connection getConnection(long waitTime) throws SQLException{
long startTime = new Date().getTime();
Connection conn = null;
while((conn = getConnection()) == null){
try {
wait(waitTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = new Date().getTime();
if((endTime - startTime)>waitTime){
return null;
}
}
return conn;
}
public synchronized void realeasePool(){
Enumeration allConnections = freeConnections.elements();
while(allConnections.hasMoreElements()){
Connection conn = (Connection)(allConnections.nextElement());
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
freeConnections.removeAllElements();
}
private synchronized Connection newConnection(Properties props) throws SQLException{
return new Connector(props).getConnection();
}
}
}