JDBC自定義連接池過(guò)程詳解
這篇文章主要介紹了JDBC自定義連接池過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
開發(fā)中,"獲得連接"和"釋放資源"是非常消耗系統(tǒng)資源的,為了解決此類性能問(wèn)題可以采用連接池技術(shù)來(lái)共享連接Connection。
1、概述
用池來(lái)管理Connection,這樣可以重復(fù)使用Connection.這樣我們就不用創(chuàng)建Connection,用池來(lái)管理Connection對(duì)象,當(dāng)使用完Connection對(duì)象后,將Connection對(duì)象歸還給池,這樣后續(xù)還可以從池中獲取Connection對(duì)象,可以重新再利用這個(gè)連接對(duì)象啦。
java為數(shù)據(jù)庫(kù)連接池提供了公共接口:javax.sql.DataSource,各個(gè)廠商需要讓自己的連接池實(shí)現(xiàn)這個(gè)接口。
常見的連接池:DBCP,C3P0
2、自定義連接池
編寫自定義連接池
1、創(chuàng)建連接池并實(shí)現(xiàn)接口javax.sql.DataSource,并使用接口中的getConnection()方法
2、提供一個(gè)集合,用于存放連接,可以采用LinkedList
3、后面程序如果需要,可以調(diào)用實(shí)現(xiàn)類getConnection(),并從list中獲取鏈接。為保證當(dāng)前連接只能提供給一個(gè)線程使用,所以我們需要將連接先從連接池中移除
4、當(dāng)用戶用完連接后,將連接歸還到連接池中
3、自定義連接池采用裝飾者設(shè)計(jì)模式
public class ConnectionPool implements Connection { private Connection connection; private LinkedList<Connection> pool; public ConnectionPool(Connection connection, LinkedList<Connection> pool){ this.connection=connection; this.pool=pool; } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { return connection.prepareStatement(sql); } @Override public void close() throws SQLException { pool.add(connection); } @Override public Statement createStatement() 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 boolean isClosed() throws SQLException { return false; } @Override public DatabaseMetaData getMetaData() throws SQLException { return null; } @Override public void setReadOnly(boolean readOnly) throws SQLException { } @Override public boolean isReadOnly() throws SQLException { return false; } @Override public void setCatalog(String catalog) throws SQLException { } @Override public String getCatalog() throws SQLException { return null; } @Override public void setTransactionIsolation(int level) throws SQLException { } @Override public int getTransactionIsolation() throws SQLException { return 0; } @Override public SQLWarning getWarnings() throws SQLException { return null; } @Override public void clearWarnings() throws SQLException { } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null; } @Override public Map<String, Class<?>> getTypeMap() throws SQLException { return null; } @Override public void setTypeMap(Map<String, Class<?>> map) throws SQLException { } @Override public void setHoldability(int holdability) throws SQLException { } @Override public int getHoldability() throws SQLException { 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 { } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null; } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return null; } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { 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; } @Override public void setSchema(String schema) throws SQLException { } @Override public String getSchema() throws SQLException { return null; } @Override public void abort(Executor executor) throws SQLException { } @Override public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { } @Override public int getNetworkTimeout() throws SQLException { return 0; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } }
DataSourcePool
public class DataSourcePool implements DataSource { //1.創(chuàng)建1個(gè)容器用于存儲(chǔ)Connection對(duì)象 private static LinkedList<Connection> pool = new LinkedList<Connection>(); //2.創(chuàng)建5個(gè)連接放到容器中去 static{ for (int i = 0; i < 5; i++) { Connection conn = JDBCUtils.getConnection(); //放入池子中connection對(duì)象已經(jīng)經(jīng)過(guò)改造了 ConnectionPool connectionPool = new ConnectionPool(conn, pool); pool.add(connectionPool); } } /** * 重寫獲取連接的方法 */ @Override public Connection getConnection() throws SQLException { Connection conn = null; //3.使用前先判斷 if(pool.size()==0){ //4.池子里面沒(méi)有,我們?cè)賱?chuàng)建一些 for (int i = 0; i < 5; i++) { conn = JDBCUtils.getConnection(); //放入池子中connection對(duì)象已經(jīng)經(jīng)過(guò)改造了 ConnectionPool connectionPool = new ConnectionPool(conn, pool); pool.add(connectionPool); } } //5.從池子里面獲取一個(gè)連接對(duì)象Connection conn = pool.remove(0); return conn; } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } }
測(cè)試代碼如下
@Test public void test1(){ Connection conn = null; PreparedStatement pstmt = null; // 1.創(chuàng)建自定義連接池對(duì)象 DataSource dataSource = new DataSourcePool(); try { // 2.從池子中獲取連接 conn = dataSource.getConnection(); String sql = "insert into USER values(?,?)"; //3.必須在自定義的connection類中重寫prepareStatement(sql)方法 pstmt = conn.prepareStatement(sql); pstmt.setString(1, "李四"); pstmt.setString(2, "1234"); int rows = pstmt.executeUpdate(); System.out.println("rows:"+rows); } catch (Exception e) { throw new RuntimeException(e); } finally { JDBCUtils.relase(conn, pstmt, null); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java?SimpleDateFormat與System類使用示例詳解
這篇文章主要介紹了Java?SimpleDateFormat與System類使用示例,對(duì)于SimpleDateFormat類,是一個(gè)用來(lái)區(qū)分區(qū)域設(shè)置的方式進(jìn)行日期的是指,以及對(duì)日期進(jìn)行處理分析的一個(gè)實(shí)現(xiàn)類2022-11-11SpringBoot?替換?if?的參數(shù)校驗(yàn)示例代碼
Spring?Validation是對(duì)hibernate?validation的二次封裝,用于支持spring?mvc參數(shù)自動(dòng)校驗(yàn),接下來(lái),我們以spring-boot項(xiàng)目為例,介紹Spring?Validation的使用,需要的朋友可以參考下2022-12-12Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11Java深入了解數(shù)據(jù)結(jié)構(gòu)之哈希表篇
哈希表是一種根據(jù)關(guān)鍵碼去尋找值的數(shù)據(jù)映射結(jié)構(gòu),該結(jié)構(gòu)通過(guò)把關(guān)鍵碼映射的位置去尋找存放值的地方,說(shuō)起來(lái)可能感覺(jué)有點(diǎn)復(fù)雜,我想我舉個(gè)例子你就會(huì)明白了,最典型的的例子就是字典2022-01-01IDEA導(dǎo)入Springboot項(xiàng)目,注解和pom文件不識(shí)別的解決
這篇文章主要介紹了IDEA導(dǎo)入Springboot項(xiàng)目,注解和pom文件不識(shí)別的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04使用CORS實(shí)現(xiàn)JavaWeb跨域請(qǐng)求問(wèn)題的方法
這篇文章主要介紹了使用Cors實(shí)現(xiàn)JavaWeb跨域請(qǐng)求問(wèn)題的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09