JDBC之连接池
来源:互联网 发布:centos nginx安装目录 编辑:程序博客网 时间:2024/04/30 11:08
在JDBC编程中,如果反复的创建连接,进行数据库的查询,这样会使系统性能大大降低,为了重复利用Connection,所以引入了连接池的
概念,下面看看我的最简单的连接池.V1.0版。
因为我的Connection不能被别人随便的new,所以采用了单利模式。
package viekie.du.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
public class ConnectionPool {
private static final String url = "jdbc:mysql://localhost:3306/jdbc";
private static final String user = "root";
private static final String password = "duchaolihaixu";
public static final int MAX_CONNECTION_NUMBER = 5;
public static final int EMPTY_POOL = -100;
private static int connectionNumbers = 0;
private static ConnectionPool instance = null;
private LinkedList<Connection> listPool = new LinkedList<Connection>();
private ConnectionPool() throws Exception{
for(int i=0; i<5; i++){
this.listPool.addFirst(this.createConnection());
connectionNumbers ++;
}
}
public static ConnectionPool getInstance() throws Exception{
if(null==instance){
synchronized(ConnectionPool.class){
if(null==instance){
instance = new ConnectionPool();
}
}
}
return instance;
}
public Connection getConnection() throws SQLException{
if(this.listPool!=null && this.listPool.size()>0){
connectionNumbers --;
return listPool.removeFirst();
}else{
throw new SQLException("the connection is empty or null");
}
}
private Connection createConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
DatabaseConnection conn = new DatabaseConnection(DriverManager.getConnection(url, user, password),this);
return conn;
}
public int getSize(){
if(connectionNumbers>=0){
return connectionNumbers;
}
return EMPTY_POOL;
}
public void setSize(int size){
connectionNumbers = size;
}
public void add(Connection conn) {
this.listPool.addLast(conn);
}
}
在来看看我的Connection的包装。
package viekie.du.jdbc;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
public class DatabaseConnection implements Connection {
private Connection conn = null;
private ConnectionPool connPool= null;
public DatabaseConnection(Connection conn, ConnectionPool pool){
this.conn = conn;
this.connPool = pool;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
@Override
public Statement createStatement() throws SQLException {
return null;
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return null;
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return null;
}
@Override
public String nativeSQL(String sql) throws SQLException {
return null;
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
}
@Override
public boolean getAutoCommit() throws SQLException {
return false;
}
@Override
public void commit() throws SQLException {
}
@Override
public void rollback() throws SQLException {
}
@Override
public void close() throws SQLException {
if(null!=this){
int size = this.connPool.getSize();
if(size<ConnectionPool.MAX_CONNECTION_NUMBER){
this.connPool.add(this);
this.connPool.setSize(size++);
}else{
conn.close();
}
}
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Savepoint setSavepoint() throws SQLException {
return null;
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return null;
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
return null;
}
@Override
public Clob createClob() throws SQLException {
return null;
}
@Override
public Blob createBlob() throws SQLException {
return null;
}
@Override
public NClob createNClob() throws SQLException {
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
return null;
}
@Override
public boolean isValid(int timeout) throws SQLException {
return false;
}
@Override
public void setClientInfo(String name, String value)
throws SQLClientInfoException {
}
@Override
public void setClientInfo(Properties properties)
throws SQLClientInfoException {
}
@Override
public String getClientInfo(String name) throws SQLException {
return null;
}
@Override
public Properties getClientInfo() throws SQLException {
return null;
}
@Override
public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
return null;
}
@Override
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
return null;
}
}
我的Connection对MySQL的Connection进行了包装,采用了代理模式,这样在connection的close调用的时候,就可以被我自定的
connection所捕获,然后,根据连接池的状态,重新把connection放入到连接池中或者关闭。
- JDBC之连接池
- JDBC之连接池
- jdbc之连接池
- JDBC之 连接池
- JDBC之 连接池
- JDBC之连接池
- Jdbc连接之数据池
- JDBC之C3P0连接池
- Java技术回顾之JDBC:连接池
- JDBC之(5)连接池
- JDBC之事务、连接池、dbutils
- Java技术回顾之JDBC:连接池
- JDBC之事务、连接池、dbutils
- JDBC之连接数据库
- JDBC基础教程之连接
- JDBC基础之连接
- JDBC基础教程之连接
- 备忘录之 JDBC 连接
- .CTOR问题
- 基于jQuery UI CSS Framework开发Widget
- 全球银行业新监管,中国无法置身事外
- switch() case注意的地方和用法
- 宠物配对 查询
- JDBC之连接池
- Android 歌词同步
- New Version for Locaccino iPhone Client
- JDBC之连接池
- 自定义控件
- 算法之个人总结:Hash表之简单应用
- Study from open source code -- malloc trace (1)
- VC++中使用ADO方式操作ACCESS数据库(转)
- 掌握三条简单net命令保证网络安全检测