Implementation of proxool connection pool

Source: Internet
Author: User
As an open source connection pool Proxool
Has the following advantages: .
Transparency can clearly add successive pools without affecting the JDBC code of your original project;
Openness allows you to easily integrate with other open-source products. Such as the proxool self-contained in hibernate
Standard is developed under j2se. Development with peace of mind
Easy to use.
Proxool is a very powerful connection pool toolkit. I think it is better than DBCP and c3p0 connection pool packages. I tested it with LoadRunner. The performance of these three connection pools is ranked as follows: proxool> c3p0> DBCP. In particular, DBCP always encounters various exceptions in the case of high concurrency.

Below are several ways to implement proxool:

JDBC connection method:
First, create a proxool configuration file proxool. xml

Proxool. XML Code
XML Code

<! -- Sp --> XML version = "1.0" encoding = "UTF-8"?>

<! -- The proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is ignored. -->

<Something-else-entirely>
<Proxool>
<! -- Alias of the connection pool -->
<Alias> dbpool </alias>
<! -- Proxool can only manage connections generated by itself -->
<Driver-URL> JDBC: oracle: thin: @ 192.168.0.40: 1521: drcom </driver-URL>
<! -- JDBC driver -->
<Driver-class> oracle. JDBC. Driver. oracledriver </driver-class>
<Driver-Properties>
<Property name = "user" value = "drcom"/>
<Property name = "password" value = "drcom"/>
</Driver-Properties>
<! -- The proxool automatically detects the time interval (in milliseconds) of each connection status. When idle connections are detected, the system immediately recycles the connection status and destroys the connection timeout. -->
<House-keeping-sleep-time> 90000 <! -- Indicates the maximum number of requests waiting in the queue because no idle connections can be allocated. User connections exceeding the number of requests will not be accepted. -->
<Maximum-New-connections> 150 </maximum-New-connections>
<! -- Minimum number of idle connections maintained -->
<Prototype-count> 3 </prototype-count>
<! -- The maximum number of connections allowed. If the connection is exceeded, the requests are placed in the queue. the maximum number of waiting requests is determined by maximum-New-connections. -->
<Maximum-connection-count> 100 </maximum-connection-count>
<! -- Minimum connections -->
<Minimum-connection-count> 3 </minimum-connection-count>
</Proxool>
</Something-else-entirely>

Then configure in Web. XML, where the servletconfigurator is to load proxool. xml under the WEB-INF directory and set it to load at Tomcat startup. Admin servlet is a tool provided by proxool to view information about the connection pool,

Web. XML Code
XML Code

<! -- Sp --> XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-Name> servletconfigurator </servlet-Name>
<Servlet-class>
Org. logicalcobwebs. proxool. configuration. servletconfigurator
</Servlet-class>
<Init-param>
<Param-Name> XML </fileparam-Name>
<Param-value> WEB-INF/proxool. xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> admin </servlet-Name>
<Servlet-class>
Org. logicalcobwebs. proxool. admin. servlet. adminservlet
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> adminservlet-Name>
<URL-pattern>/adminurl-pattern>
</Servlet-mapping>
<Servlet>
<Servlet-Name> testservlet </servlet-Name>
<Servlet-class>
Selfservice. testservlet
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> testservlet </servlet-Name>
<URL-pattern>/testservlet </url-pattern>
</Servlet-mapping>
</Web-app>

After the preceding configuration, you can create a connection pool class in step 3.

Java code

Package selfservice;

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;

Import org. logicalcobwebs. proxool. proxoolexception;
Import org. logicalcobwebs. proxool. proxoolfacade;
Import org. logicalcobwebs. proxool. admin. snapshotif;


Public class poolmanager {

Private Static int activecount = 0;


Public poolmanager (){

}
/**
* Obtain the connection
* Getconnection
* @ Param name
* @ Return
*/
Public connection getconnection (){
Try {
Class. forname ("org. logicalcobwebs. proxool. proxooldriver"); // proxool Driver Class
Connection conn = drivermanager. getconnection ("proxool. dbpool ");
// Here, dbpool is the alias of the Connection Pool configured in proxool. xml
Showsnapshotinfo ();

Return conn;
} Catch (exception ex ){
Ex. printstacktrace ();
}
Return NULL;
}
/**
* This method can obtain information about the connection pool.
* Showsnapshotinfo
*/
Private void showsnapshotinfo (){
Try {
Snapshotif snapshot = proxoolfacade. getsnapshot ("dbpool", true );
Int curactivecount = snapshot. getactiveconnectioncount (); // gets the number of active connections
Int availablecount = snapshot. getavailableconnectioncount (); // obtain the number of available connections
Int maxcount = snapshot. getmaximumconnectioncount (); // gets the total number of connections
If (curactivecount! = Activecount) // the output information when the number of active connections changes
{
System. out. println ("active connections:" + curactivecount + "(active) available connections:" + availablecount + "(available) Total connections:" + maxcount + "(max) ");
Activecount = curactivecount;
}
} Catch (proxoolexception e ){
E. printstacktrace ();
}
}
/**
* Obtain the connection
* Getconnection
* @ Param name
* @ Return
*/
Public connection getconnection (string name ){
Return getconnection ();
}
/**
* Release a connection
* Freeconnection
* @ Param Conn
*/
Public void freeconnection (connection conn ){
If (Conn! = NULL ){
Try {
Conn. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
}
/**
* Release a connection
* Freeconnection
* @ Param name
* @ Param con
*/
Public void freeconnection (string name, connection con ){
Freeconnection (CON );
}

Public void getquery (){
Try {
Connection conn = getconnection ();
If (Conn! = NULL ){
Statement statement = conn. createstatement ();
Resultset rs = statement.exe cutequery ("select * From tblgxinterface ");
Int c = Rs. getmetadata (). getcolumncount ();
While (Rs. Next ()){
System. Out. println ();
For (INT I = 1; I <= C; I ++ ){
System. Out. Print (Rs. GetObject (I ));
}
}
Rs. Close ();
}
Freeconnection (conn );
} Catch (sqlexception e ){
E. printstacktrace ();
}

}

}

In this way, we have completed the function of a connection pool. In the proxool connection pool, I use LoadRunner to test high concurrency, and the performance is still good.

Proxool connection pool in hibernateMethod:

The first step is to create a proxool. xml configuration file, like the JDBC connection pool, and then configure it in Web. xml. For details, refer to the above.
Step 2 configure the proxool connection settings in the hibernate configuration file hibernate. cfg. xml:

Hibernate. cfg. XML Code
XML Code

<Property name = "hibernate. Connection. provider_class"> org. hibernate. Connection. proxoolconnectionprovider </property>
<Property name = "hibernate. proxool. pool_alias"> dbpool </property>
<Property name = "hibernate. proxool. xml"> proxoolconf. xml </property>

Proxool connection pool in springMethod:

First, just like the JDBC connection pool, create a proxool. xml configuration file and configure it in Web. xml. For details, refer to the above.
Step 2 configure the proxool connection settings in the spring configuration file applicationcontext. xml

Applicationcontext. XML Code
XML Code

<Bean id = "datasource" class = "org. springframework. JDBC. datasource. drivermanagerdatasource" Singleton = "true">
<Property name = "driverclassname" value = "org. logicalcobwebs. proxool. proxooldriver"/>
<Property name = "url" value = "proxool. statdbpool"/>
</Bean>
<Bean id = "transactionmanager"
Class = "org. springframework. JDBC. datasource. datasourcetransactionmanager">
<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
</Bean>

In this way, spring can obtain a datasource data source.

Proxool has many other functions. I am just a simple application. For details, refer to the proxool user guide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.