Application from a ConnectionPool to the application of Design Pattern (3)

zhaozj2021-02-16  45

Application from a ConnectionPool to the application of Design Pattern (3)

Based on the analysis of PooledConnection up-to-back, the following is the implementation of a reusable PooledConnection:

Public Class PooledConnection Implements Connection {

Public interface pool {

// Introduced this interface, because our PooledConnection only needs to know how to return Connection. In the principle of the minimization of the interface, we only define what we need.

Void CloseConnection (Connection CONN);

}

Private interface connectstate {

// State Pattern's Interface.

ConnectionState Close ()

Throws SqlexCeption;

// close () method is the only way to cause status transfer.

Boolean isclosed ();

Connection getopenConnection ()

Throws SqlexCeption;

}

Private static class closedConnection Implements ConnectionState {

Public final connectionState close () {return this;}

// When a connection has been closed, it is actually dead. All for it, except for isclosed () and close (), only exceptions. So, a Closed Connection, it does not need to save the physical database connection and the connection to the adnectionPool. And because all the CLOSED Connection status is the same, you can save memory with Singleton.

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, Pool Pool) {Return_INSTANCE;

}

Private static class openconnection imports connectionState {

PRIVATE FINAL POLEL;

PRIVATE FINAL CONNECTION CONN;

Public Final ConnectionState Close () {

/ / The original database connection will be returned to the Connection Pool at the same time, the connection death.

Pool.CloseConnection (conn);

Return ClosedConnection.Instance (CONN, POOL);

}

Public Final Connection GetopenConnection ()

{Return conn;}

Public final boolean isclosed () {returnaf false;}

OpenConnection (Connection CONN, POOL) {

THIS.CONN = conn; this.pool = pool;}

Static ConnectionState Instance (Connection CONN, POOL) {

Return New OpenConnection (CONN, POOL);

}

}

PRIVATECITATE STATE;

// With a static factory method, we can hide our implementation class. In the future, we can easily modify the implementation class, such as inside the class.

/ / Initialize PooledConnection according to the status of Connection to be modified

Public Static Conn, Pool Pool (Connection CONN, POL)

Throws sqlexception {

IF (conn.isclosed ()) {

Return New PooledConnection (ClosedConnection.Instance (CONN, POOL));

}

Else {

Return New PooledConnection (OpenConnection.Instance (CONN, POOL);

}

}

Private PooledConnection (ConnectionState State) {

THIS.STATE = State;

}

Public final boolean isclosed () {

Return state.isclosed ();

}

Public final void close ()

Throws sqlexception {

State = state.close ();

}

Private Final Connection GetopenConnection ()

Throws SQLEXCEPTION

{Return state.getopenConnection ();

/ ***** Then, be entrusted **** /

Public final statement createstatement ()

Throws sqlexception {

Return getopenConnection (). CreateStatement ();

}

Public final void rollback () throws sqlexception {

GetopenConnection (). rollback ();

}

//Etc., etc

}

Ok, let's take a look at how ConnectionPoolIMPL uses PooledConnection.

Public Class ConnectionPoolImpl Implements ConnectionPool {

Public synchronized connection getConnection () {

CONNECTION RET;

If there is a Connection in pool

Remove a connection conn from the pool;

CLIENTS ;

Ret = conn;

Otherwise, if Clients

Generate a new connection CONN

CLIENTS ;

Ret = conn;

Otherwise, Wait () until there is idle connection in pool

// The following this anonymous class is actually an adapter pattern. J

Return PooledConnection.Decorate (RET,

New pooledConnection.pool {

Public void closeconnection (connection conn) {

ConnectionPoolImpl.This.CloseConnection (conn);

}

}

}

// Other are the same as the original.

In this way, all ConnectionPool implementations can be packaged with the PooledConnection before returning a physical connection. In this way, the code is reused. ConnectionPool's implementation can put main energy in how to treat various functions of the pool. Instead, how to pack the Connection.

The world is so beautiful!

but. . . . .

What should I do if Li Si forget what to do with PooledConnection? The compiler will not report an error, because anyway is a Connection type.

"You are too worrying about people? He didn't get it?" Hey, it is not uniform! People are not machines, always have to make mistakes, when he is used? Still handache.

Classmates, today's homework is: I want to let Li Si's will not affect our ConnectionPool.

转载请注明原文地址:https://www.9cbs.com/read-26049.html

New Post(0)