我写的一个简单的多线程JDBC连接池
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;
}
}
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;
}
}
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) {
}
}
}
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) {
}
}
}
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;
}
}
}
}
}
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;
}
}
}
}
}
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);
}
}
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);
}
}