Use commons-pool to connect pools

xiaoxiao2021-03-06  62

1. Interface class for database links: DatabaseConnection

Package Net.Pingsoft.kelefa.pool;

Import java.sql. *;

Import javax.sql. *;

Import com.wish.jdbc.wconnection;

Import org.apache.commons.pool. *;

Import org.apache.commons.pool.Impl. *;

/ **

* Database link interface class. Call the static method GetDbConnection () to get the Connection object directly.

* Related parameters are set by the poolconfigservlet class according to Web.xml, so web.xml needs to register PoolConfigServlet

*

* Copyright: Copyright (C) 2004

* @Author kelefa yang

* @version 1.0

* @see poolconfigservlet

* /

Public Class DatabaseConnection

{

/ ** Database username * /

Public stat String user = "sa";

/ ** Database password * /

Public static string pass = "yf1";

/ ** Startup program class name * /

Public Static String DBDriver =

"com.microsoft.jdbc.sqlserver.sqlserdriver";

/ ** Database link address * /

Public Static String Dburl =

"JDBC: Microsoft: SQLSERVER: //192.9.200.23: 1433; DatabaseName = FC";

/ ** Do you use JNDI * /

Public static boolean usejndi = false;

/ ** JNDI name, usejndi == false is invalid * /

Public static string jndi = "wishjndi";

/ **

* Do you encode a database link?

* @DepRecated does not test, you should directly set the database or add the encoded parameters in the link URL

* /

Public Static Boolean ConvertConNetion = FALSE;

/ ** Whether buffer link * /

Public Static Boolean Pool_Connection = FALSE;

/ **

* There is no need to generate an instance

* /

Private DatabaseConnection ()

{}

/ **

* Retrieve the actual database link according to the corresponding parameters.

* If usejndi is true, the JNDI name is linked from the data source; otherwise links directly from JDBC.

* If ConvertConnetion is true, the link is resembled, and the encoding conversion is achieved.

* @Throws Exception When usejndi == false, and User, Pass, DBDriver, DBURL one is empty

* Throw "DatabaseConnection Didn't"! "Exception

* @Return Connection Database Link, return NULL

* /

Static Connection getConnection () THROWS Exception

{

Connection conn = NULL;

IF (! usjndi)

{

IF ((user == null) || (pass == null) || (dbdriver == null) || (dburl == null))

Throw New Exception ("DatabaseConnection Didn't !!");

Class.Forname (dbdriver);

Conn = drivermanager.getConnection (DBURL, User, Pass);

}

Else

{

DataSource DS = Service (). GetDataSource (JNDI);

CONN = ds.getConnection ();

}

IF (ConvertConne && conn! = null)

Return New WConnection (CONN);

Else

Return conn;

}

/ ** Link pool factory * /

Private static genericObjectPoolfactory poolfactory =

New genericObjectPoolfactory (New connectionFactory ());

/ ** Database link pool * /

Private static ObjectPool pool = poolfactory.createpool ();

/ **

* if pool_connection is true, return the pooled connection.

* Pool_connection will be set in class poolconfigservlet When Webapp Start,

* You can change the value in Web.xml.

* *

* poolConnection

* True

*

*

* @Throws Exception

* @Return Connection

* /

Public Static Connection GetDbconnection () THROWS EXCEPTION

{

IF (pool_connection)

{

Object obj = pool.borrowObject ();

IF (null == obj)

Return NULL;

PoolableConnection conn = (poolaableconnection) OBJ;

Conn.setpool (pool);

Return conn;

}

Else

Return getConnection ();

}

}

2. Database Link Factory CONNECTIONFAACTORY

Package Net.Pingsoft.kelefa.pool;

Import org.apache.commons.pool. *;

Import java.sql.connection;

/ **

* Database Link Object Factory, responsible for creating database links and encapsulating it into buffered objects and closing the database link.

* This factory instance is a constructor of org.apache.commons.pool.Impl.GenericObjectPoolFactory

* Parameters.

*

* Copyright: Copyright (C) 2004

* @Author kelefa yang * @version 1.0

* @see org.apache.commons.pool.Impl.GenericObjectPoolFactory

* /

Public Class ConnectionFactory Extends BasepooLableObjectFactory

{

Public connectionFactory ()

{

}

/ **

* Creates a connection instance That Can Be Returned by the pool.

*

* @Return an instance That Can Be Returned by the pool.

* @Throws Exception

* @Todo Implement this org.apache.commons.pool.poolableObjectFactory Method

* /

Public Object MakeObject () THROWS EXCEPTION

{

Connection conn = DatabaseConnection.getConnection ();

IF (conn == null)

Return NULL;

Return New PoolableConnection (CONN);

}

/ **

* Close a connection instance no longer needed by the pool.

*

* @Param Obj the instance to be destroyed

* @Throws Exception

* @Todo Implement this org.apache.commons.pool.poolableObjectFactory Method

* /

Public Void DestroyObject (Object Obj) THROWS EXCEPTION

{

PoolableConnection conn = (poolaableconnection) OBJ;

Conn.SetPool (NULL);

CONN.CLOSE ();

}

}

3. Rebate the connection.close () method so that you can return to the database link pool

Package Net.Pingsoft.kelefa.pool;

Import java.sql.connection;

Import java.sql.sqlexception;

Import org.apache.commons.pool. *;

/ **

* Rebursing the connection.close () method so that the database link pool can be returned.

*

* Copyright: Copyright (C) 2004

* @Author kelefa yang

* @version 1.0

* /

Public Class PoolableConnection Extends ConnectionWrap

{

/ ** Database link pool * /

Private ObjectPool POOL;

Public PoolableConnection (Connection CONN)

{

Super (conn);

}

/ **

* If the database link pool exists, return this link back to the database link pool; otherwise close this database link

* @Throws SQLException

* /

Public void close () THROWS SQLEXCEPTION

{

Try

{

IF (pool! = null)

Pool.ReturnObject (this);

Else

Super.close ();

}

Catch (Exception EX)

{

}

}

/ **

* Set the link pool where this link is located. * If Pool is equal to NULL, the link is used directly, otherwise, returns to the link pool after using it.

* @Param pool database link pool

* /

Public void setpool (ObjectPool Pool)

{

THIS.POOL = POOL;

}

}

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

New Post(0)