Tutorials to .com

Tutorials to .com » Jsp » Database » A realization method of database connection pool (2)

A realization method of database connection pool (2)

Print View , by: iSee ,Total views: 11 ,Word Count: 893 ,Date: Sat, 18 Apr 2009 Time: 11:06 PM

Followed by the type of connection pooling factory ConnectionFactory, to the adoption of such a connection pool object with a name corresponding to the user by the name can be accessed on the specified connection pool object, the specific code as follows:


/ **
* Connection pool-type plants, such commonly used to preserve the name of a number of data sources database connection pool together the corresponding hash
* @ Author liusoft
* /
public class ConnectionFactory
(
/ / The hash table to save the data source and connection pool objects the relationship between the table
static Hashtable connectionPools = null;
static (
connectionPools = new Hashtable (2,0.75 F);
)
/ **
* From the connection pool factory to obtain the specified connection pool name of the corresponding object
* @ Param dataSource the corresponding connection pool object name
* @ Return DataSource to return the name of the corresponding connection pool object
* @ Throws NameNotFoundException can not find the specified connection pool
* /
public static DataSource lookup (String dataSource)
throws NameNotFoundException
(
Object ds = null;
ds = connectionPools.get (dataSource);
if (ds == null | |! (ds instanceof DataSource))
throw new NameNotFoundException (dataSource);
return (DataSource) ds;
)

/ **
* The specified name and the database connection configuration and bind together to initialize the database connection pool
* @ Param name the name of the corresponding connection pool
* @ Param param connection pool configuration parameters, see the specific category ConnectionParam
* @ Return DataSource return if binding is successful connection pool object
* @ Throws NameAlreadyBoundException certain the name of name has been thrown out of the binding is abnormal
* @ Throws ClassNotFoundException can not find the connection pool configuration in the driver category
* @ Throws IllegalAccessException connection pool configuration in the driver category is incorrect
* @ Throws InstantiationException driver can not be instantiated type
* @ Throws SQLException not specify the database connection
* /
public static DataSource bind (String name, ConnectionParam param)
throws NameAlreadyBoundException, ClassNotFoundException,
IllegalAccessException, InstantiationException, SQLException
(
DataSourceImpl source = null;
try (
lookup (name);
throw new NameAlreadyBoundException (name);
) catch (NameNotFoundException e) (
source = new DataSourceImpl (param);
source.initConnection ();
connectionPools.put (name, source);
)
return source;
)
/ **
* Re-bind the database connection pool
* @ Param name the name of the corresponding connection pool
* @ Param param connection pool configuration parameters, see the specific category ConnectionParam
* @ Return DataSource return if binding is successful connection pool object
* @ Throws NameAlreadyBoundException certain the name of name has been thrown out of the binding is abnormal
* @ Throws ClassNotFoundException can not find the connection pool configuration in the driver category
* @ Throws IllegalAccessException connection pool configuration in the driver category is incorrect
* @ Throws InstantiationException driver can not be instantiated type
* @ Throws SQLException not specify the database connection
* /
public static DataSource rebind (String name, ConnectionParam param)
throws NameAlreadyBoundException, ClassNotFoundException,
IllegalAccessException, InstantiationException, SQLException
(
try (
unbind (name);
) catch (Exception e) ()
return bind (name, param);
)
/ **
* Delete a database connection pool object
* @ Param name
* @ Throws NameNotFoundException
* /
public static void unbind (String name) throws NameNotFoundException
(
DataSource dataSource = lookup (name);
if (dataSource instanceof DataSourceImpl) (
DataSourceImpl dsi = (DataSourceImpl) dataSource;
try (
dsi.stop ();
dsi.close ();
) catch (Exception e) (
) finally (
dsi = null;
)
)
connectionPools.remove (name);
)

)




ConnectionFactory provides the users will be the main connection pool will be bound to a specific name and the cancellation of the binding operation. Users only need to be concerned about the use of these two types of the functions of database connection pool. Now we are given a period of how to use the connection pooling code:


String name = "pool";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc: odbc: datasource";
ConnectionParam param = new ConnectionParam (driver, url, null, null);
param.setMinConnection (1);
param.setMaxConnection (5);
param.setTimeoutValue (20000);
ConnectionFactory.bind (name, param);
System.out.println ( "bind datasource ok.");
/ / The above code is used to register a connection pool object, the operation procedure can be initialized only once
/ / The following is the user really needs to start writing code
DataSource ds = ConnectionFactory.lookup (name);
try (
for (int i = 0; i <10; i + +) (
Connection conn = ds.getConnection ();
try (
testSQL (conn, sql);
) finally (
try (
conn.close ();
) catch (Exception e) ()
)
)
) catch (Exception e) (
e.printStackTrace ();
) finally (
ConnectionFactory.unbind (name);
System.out.println ( "unbind datasource ok.");
System.exit (0);
)




The sample code from the user can see, we have solved the conventional connection pool will cause two problems. But we most concerned about is how to resolve the approach to take over the close approach. ConnectionFactory to take over the work of the two sentences in the code:


source = new DataSourceImpl (param);
source.initConnection ();


Jsp And Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.