Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to see the Application of design pattern from the realization of ConnectionPool

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to look at the use of ConnectionPool from the implementation of design pattern. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

On the Application of design pattern from the implementation of a ConnectionPool (source code for Java 1.1)

ConnectionPool.Java:

Public interface ConnectionPool {

Connection getConnection ()

Throws test.res.ResourceNotAvailableException, sqlException

Connection getConnection (long timeout)

Throws test.res.ResourceTimeOutException, SQLException

Void clear ()

}

ConnectionHome.java:

Public interface ConnectionHome {

Void releaseConnection (Connection conn)

}

ConnectionPooling.java:

Public interface ConnectionPooling extends ConnectionHome {

Connection getConnection ()

Throws test.res.ResourceNotAvailableException, SQLException

Connection getConnection (long timeout)

Throws test.res.ResourceTimeOutException, SQLException

Void clear ()

Void releaseConnection (Connection conn)

}

ConnectionFactory.java:

Public interface ConnectionFactory {

Public Connection createConnection () throws SQLException

}

PooledConnection.java: (for Connection in Java 1.1.8)

Public final class PooledConnection implements Connection {

Private interface ConnectionState {

ConnectionState close ()

Boolean isClosed ()

Connection getOpenConnection ()

Throws SQLException

}

Private static class ClosedConnection implements ConnectionState {

Public final ConnectionState close () {return this;}

Public final Connection getOpenConnection ()

Throws SQLException {

Throw new SQLException ("Connection closed")

}

Public final boolean isClosed () {return true;}

Private ClosedConnection () {}

Private static final ConnectionState _ instance = new ClosedConnection ()

Static ConnectionState instance (Connection conn, ConnectionHome home) {return _ instance;}

}

Private static class OpenConnection implements ConnectionState {

Private final ConnectionHome home

Private final Connection conn

Public final ConnectionState close () {

Home.releaseConnection (conn)

Return ClosedConnection.instance (conn, home)

}

Public final Connection getOpenConnection ()

{return conn;}

Public final boolean isClosed () {return false;}

OpenConnection (Connection conn, ConnectionHome home) {

This.conn = conn; this.home = home

}

Static ConnectionState instance (Connection conn, ConnectionHome home) {

Return new OpenConnection (conn, home)

}

}

Private ConnectionState state

Public static Connection decorate (Connection conn, ConnectionHome home)

Throws SQLException {

Return new PooledConnection (conn, home)

}

Private PooledConnection (Connection conn, ConnectionHome home)

Throws SQLException {

If (conn.isClosed ()) {

State = ClosedConnection.instance (conn, home)

}

Else {

State = OpenConnection.instance (conn, home)

}

}

Public final boolean isClosed () {

Return state.isClosed ()

}

Public final void close () {

State = state.close ()

}

Protected void finalize () {

Close ()

}

Private final Connection getOpenConnection ()

Throws SQLException

{return state.getOpenConnection ();}

/ * then, delegate all the other methods****/

Public final Statement createStatement ()

Throws SQLException {

Return getOpenConnection () .createStatement ()

}

/ /....

Public final void clearWarnings () throws SQLException {

GetOpenConnection () .clearWarnings ()

}

Public final void commit () throws SQLException {

GetOpenConnection () .commit ()

}

/ *

Public final Statement createStatement (int resultSetType

Int resultSetConcurrency)

Throws SQLException {

Return getOpenConnection () .createStatement (resultSetType resultSetConcurrency)

} * /

/ *

Public final Statement createStatement (int resultSetType

Int resultSetConcurrency, int resultSetHoldability)

Throws SQLException {

Return getOpenConnection () .createStatement (resultSetType

ResultSetConcurrency, resultSetHoldability)

} * /

Public final boolean getAutoCommit () throws SQLException {

Return getOpenConnection () .getAutoCommit ()

}

Public final String getCatalog () throws SQLException {

Return getOpenConnection () .getCatalog ()

}

/ *

Public final int getHoldability () throws SQLException {

Return getOpenConnection () .getHoldability ()

} * /

Public final DatabaseMetaData getMetaData () throws SQLException {

Return getOpenConnection () .getMetaData ()

}

Public final int getTransactionIsolation () throws SQLException {

Return getOpenConnection () .getTransactionIsolation ()

}

/ *

Public final Map getTypeMap () throws SQLException {

Return getOpenConnection () .getTypeMap ()

} * /

Public final SQLWarning getWarnings () throws SQLException {

Return getOpenConnection () .getWarnings ()

}

Public final boolean isReadOnly () throws SQLException {

Return getOpenConnection () .isReadOnly ()

}

Public final String nativeSQL (String sql) throws SQLException {

Return getOpenConnection () nativeSQL (sql)

}

Public final CallableStatement prepareCall (String sql) throws SQLException {

Return getOpenConnection () prepareCall (sql)

}

/ *

Public final CallableStatement prepareCall (String sql

Int resultSetType, int resultSetConcurrency)

Throws SQLException {

Return getOpenConnection () .prepareCall (sql, resultSetType, resultSetConcurrency)

}

Public final CallableStatement prepareCall (String sql

Int resultSetType, int resultSetConcurrency, int resultSetHoldability)

Throws SQLException {

Return getOpenConnection () .prepareCall (sql, resultSetType

ResultSetConcurrency, resultSetHoldability)

} * /

Public final PreparedStatement prepareStatement (String sql)

Throws SQLException {

Return getOpenConnection () prepareStatement (sql)

}

/ *

Public final PreparedStatement prepareStatement (String sql, int autoGeneratedKeys)

Throws SQLException {

Return getOpenConnection () .prepareStatement (sql autoGeneratedKeys)

}

Public final PreparedStatement prepareStatement (String sql, int [] columnIndexes)

Throws SQLException {

Return getOpenConnection () .prepareStatement (sql columnIndexes)

}

Public final PreparedStatement prepareStatement (String sql

Int resultSetType, int resultSetConcurrency)

Throws SQLException {

Return getOpenConnection () .prepareStatement (sql

ResultSetType, resultSetConcurrency)

}

Public final PreparedStatement prepareStatement (String sql

Int resultSetType, int resultSetConcurrency, int resultSetHoldability)

Throws SQLException {

Return getOpenConnection () .prepareStatement (sql

ResultSetType, resultSetConcurrency, resultSetHoldability)

}

Public final PreparedStatement prepareStatement (String sql

String [] columnNames)

Throws SQLException {

Return getOpenConnection () .prepareStatement (sql columnNames)

}

Public final void releaseSavepoint (Savepoint savepoint) throws SQLException {

GetOpenConnection () releaseSavepoint (savepoint)

} * /

Public final void rollback () throws SQLException {

GetOpenConnection () .rollback ()

}

/ *

Public final void rollback (Savepoint savepoint)

Throws SQLException {

GetOpenConnection () rollback (savepoint)

} * /

Public final void setAutoCommit (boolean autoCommit)

Throws SQLException {

GetOpenConnection () setAutoCommit (autoCommit)

}

Public final void setCatalog (String catalog)

Throws SQLException {

GetOpenConnection () setCatalog (catalog)

}

/ *

Public final void setHoldability (int holdability)

Throws SQLException {

GetOpenConnection () setHoldability (holdability)

} * /

Public final void setReadOnly (boolean readOnly)

Throws SQLException {

GetOpenConnection () setReadOnly (readOnly)

}

/ *

Public final Savepoint setSavepoint () throws SQLException {

Return getOpenConnection () .setSavepoint ()

}

Public final Savepoint setSavepoint (String name)

Throws SQLException {

Return getOpenConnection () setSavepoint (name)

} * /

Public final void setTransactionIsolation (int level)

Throws SQLException {

GetOpenConnection () setTransactionIsolation (level)

}

/ * public final void setTypeMap (Map map) throws SQLException {

GetOpenConnection () setTypeMap (map)

} * /

/ * /

}

ConnectionPooling2Pool.java:

Public class ConnectionPooling2Pool implements ConnectionPool {

Public final Connection getConnection ()

Throws test.res.ResourceNotAvailableException, SQLException {

Return PooledConnection.decorate (pooling.getConnection (), pooling)

}

Public final Connection getConnection (long timeout)

Throws test.res.ResourceTimeOutException, SQLException {

Return PooledConnection.decorate (pooling.getConnection (timeout), pooling)

}

Public final void clear () {

Pooling.clear ()

}

Private final ConnectionPooling pooling

Private ConnectionPooling2Pool (ConnectionPooling pooling) {

This.pooling = pooling

}

Public static ConnectionPool decorate (ConnectionPooling pooling) {

Return new ConnectionPooling2Pool (pooling)

}

}

ConnectionPoolingImpl.java: (a simple implementation of ConnectionMan)

Public class ConnectionPoolingImpl implements ConnectionPooling

{

Private int client=0

Private final Vector freeConn = new Vector ()

Private final int maxConn

Private final ConnectionFactory factory

Static public ConnectionPooling instance (ConnectionFactory factory, int max) {

Return new ConnectionPoolingImpl (factory, max)

}

Private ConnectionPoolingImpl (ConnectionFactory factory, int max) {

This.factory = factory

This.maxConn = max

}

Public final synchronized void releaseConnection (Connection conn)

{

FreeConn.addElement (conn)

Client--

Notify ()

}

Public final synchronized Connection getConnection ()

Throws ResourceNotAvailableException, SQLException

{

Connection conn = null

If (freeConn.size () > 0)

{

Conn = (Connection) freeConn.lastElement ()

FreeConn.removeElementAt (freeConn.size () 1)

}

Else if (client

< maxConn) { conn = factory.createConnection(); } if(conn != null) { client++; return conn; } else { throw new ResourceNotAvailableException(); } } public final synchronized Connection getConnection(long timeout) throws ResourceTimeOutException, SQLException { for(long startTime = new java.util.Date().getTime();;) { try { return getConnection(); } catch(ResourceNotAvailableException e1) { try { wait(timeout); } catch(InterruptedException e2) {} if((new java.util.Date().getTime() - startTime) >

= timeout)

{

Throw new ResourceTimeOutException ()

}

}

}

}

Public final synchronized int getfreeconn () {

Return freeConn.size ()

}

Public final int getmaxConn () {

Return maxConn

}

Public final synchronized int getclient () {

Return client

}

Public final synchronized void setclient () {

Client=0

}

Public final synchronized void clear () {

CloseAll ()

FreeConn.removeAllElements ()

}

Private final void closeAll () {

For (int item0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report