我写的一个简单的多线程JDBC连接池

ContractedBlock.gif ExpandedBlockStart.gif Code
package JoeyUtilities;

import java.sql.*;

public class JoeyConnection {
    
private Connection connection;
    
private Boolean isInUse;
    
private String memoryAddress;

    
public JoeyConnection(Connection connection) {
        
this.connection = connection;
        
this.isInUse = true;// should be used immediately when it is created
        this.memoryAddress = this.connection.toString();
    }

    
/**
     * 
@return the connection
     
*/
    
public Connection getConnection() {
        
return connection;
    }

    
/**
     * 
@param connection
     *            the connection to set
     
*/
    
public void setConnection(Connection connection) {
        
this.connection = connection;
    }

    
/**
     * 
@return the isInUse
     
*/
    
public Boolean getIsInUse() {
        
return isInUse;
    }

    
/**
     * 
@param isInUse
     *            the isInUse to set
     
*/
    
public void setIsInUse(Boolean isInUse) {
        
this.isInUse = isInUse;
    }

    
/**
     * 
@return the memoryAddress
     
*/
    
public String getMemoryAddress() {
        
return memoryAddress;
    }

    
/**
     * 
@param memoryAddress
     *            the memoryAddress to set
     
*/
    
public void setMemoryAddress(String memoryAddress) {
        
this.memoryAddress = memoryAddress;
    }
}

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
package JoeyUtilities;

import java.util.*;

public class JoeyConnectionCleaner implements Runnable {
    
private ArrayList<JoeyConnection> pool;
    
private int connectionsInUse;

    
public JoeyConnectionCleaner(ArrayList<JoeyConnection> pool) {
        
this.pool = pool;
    }

    
public void increaseConnectionsInUse() {
        
this.connectionsInUse++;
    }

    
public void decreaseConnectionsInUse() {
        
this.connectionsInUse--;
    }

    
public void run() {
        
try {
            
while (true) {
                
synchronized (pool) {
                    
if (pool.size() > 10) {
                        
if (connectionsInUse * 100 / pool.size() < 66) {
                            
for (JoeyConnection jc : pool) {
                                
if (!jc.getIsInUse()) {
                                    
try {
                                        jc.getConnection().close();
                                    } 
catch (Exception e) {
                                    }                        
                                    pool.remove(jc);
                                    jc 
= null;
                                    
break;
                                }
                            }
                        }
                    }
                }
                Thread.sleep(
20000);
            }
        } 
catch (Exception e) {
        }
    }
}

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
package JoeyUtilities;

import java.sql.*;
import java.util.*;

public class JoeyConnectionDealer {
    
private ArrayList<JoeyConnection> pool;
    
private String driver;
    
private String server;
    
private String user;
    
private String password;
    
private JoeyConnectionCleaner jcc;

    
public JoeyConnectionDealer(ArrayList<JoeyConnection> pool, String driver,
            String server, String user, String password,
            JoeyConnectionCleaner jcc) {
        
this.pool = pool;
        
this.driver = driver;
        
this.server = server;
        
this.user = user;
        
this.password = password;
        
this.jcc = jcc;
    }

    
private Connection createConnection() {
        
try {
            Class.forName(driver).newInstance();
            Connection connection 
= DriverManager.getConnection(server, user,
                    password);
            JoeyConnection jc 
= new JoeyConnection(connection);
            
synchronized (pool) {
                
this.pool.add(jc);
                
this.jcc.increaseConnectionsInUse();
            }
            
return connection;
        } 
catch (Exception e) {
            
return null;
        }
    }

    
public Connection getConnection() {
        
synchronized (pool) {
            
int index = 0;
            
while (index < pool.size()) {
                JoeyConnection jc 
= pool.get(index);
                
if (!jc.getIsInUse()) {
                    
try {
                        jc.getConnection().createStatement();
                        
if (jc.getConnection().isClosed()) {
                            
throw new SQLException();
                        }
                        jc.setIsInUse(
true);
                        
this.jcc.increaseConnectionsInUse();
                        
return jc.getConnection();
                    } 
catch (Exception e) {
                        
try {
                            jc.getConnection().close();
                        } 
catch (SQLException sqle) {
                        }
                        pool.remove(jc);
                        
continue;
                    }
                }
                index
++;
            }
        }
        
return createConnection();
    }

    
public void returnConnection(Connection connection) {
        
synchronized (pool) {
            
for (JoeyConnection jc : pool) {
                
if (connection.toString().equals(jc.getMemoryAddress())) {
                    
try {
                        connection.createStatement();
                        
if (connection.isClosed()) {
                            
throw new SQLException();
                        }
                        jc.setIsInUse(
false);
                    } 
catch (Exception e) {
                        
try {
                            connection.close();
                        } 
catch (SQLException e1) {
                        }
                        pool.remove(jc);
                    }
                    
this.jcc.decreaseConnectionsInUse();
                    
return;
                }
            }
        }
    }
}

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
package JoeyUtilities;

import java.sql.*;
import java.util.*;

public class JoeyJDBCConnectionPool {
    
private static JoeyJDBCConnectionPool singletenInstance;
    
private JoeyConnectionDealer jcd;

    
public static JoeyJDBCConnectionPool getConnectionPool(String driver,
            String server, String user, String password) {
        
if (singletenInstance == null) {
            singletenInstance 
= new JoeyJDBCConnectionPool(driver, server,
                    user, password);
        }
        
return singletenInstance;
    }

    
private JoeyJDBCConnectionPool(String driver, String server, String user,
            String password) {
        ArrayList
<JoeyConnection> pool = new ArrayList<JoeyConnection>();
        JoeyConnectionCleaner jcc 
= new JoeyConnectionCleaner(pool);
        JoeyConnectionDealer jcd 
= new JoeyConnectionDealer(pool, driver,
                server, user, password, jcc);
        
this.jcd = jcd;
        (
new Thread(jcc)).start();
    }

    
public Connection getConnection() throws Exception {
        Connection connection 
= this.jcd.getConnection();
        
if (connection == null) {
            
throw new SQLException("Database is done");
        } 
else {
            
return connection;
        }
    }

    
public void returnConnection(Connection connection) {
        
this.jcd.returnConnection(connection);
    }
}

转载于:https://www.cnblogs.com/aspxphpjsprb/archive/2008/09/24/1297615.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值