JDBC之连接池
2010-08-19 17:45
253 查看
在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放入到连接池中或者关闭。
概念,下面看看我的最简单的连接池.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放入到连接池中或者关闭。
相关文章推荐
- 11级_Java_曹建波 11.26 JDBC连接池
- JDBC 配置Tomcat的连接池和数据源
- 建立基于JDBC的resin3.0.8的连接池
- springboot 多数据源(三种数据库连接池--JDBC,dbcp2,Druid)
- JDBC连接池、监控组件 Druid
- 在Hibernate中创建连接池,JDBC连接
- 玩转JDBC打造数据库操作万能工具类JDBCUtil,加入了高效的数据库连接池,利用了参数绑定有效防止SQL注入
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Tomcat7 新的数据库连接池Tomcat jdbc pool介绍和配置
- 数据层优化-jdbc连接池简述、druid简介
- JDBC事务处理、连接池及升级替代产品
- 基于JDBC的数据库连接池技术研究与应用
- jdbc连接池
- c3p0、dbcp、tomcat jdbc pool 连接池区别(推荐使用jdbc pool)
- 基于JDBC的数据库连接池技术研究与应用
- 基于JDBC的数据库连接池技术研究与应用
- Oracle JDBC2.0 数据来源(Data Source)与连接池(Connection Pool)
- oracle 11.02新版在win7下安装与Tomcatr的连接池以及jdbc驱动的问题
- JDBC连接池、监控组件 Druid
- JDBC的数据库连接池的工作原理