JdbcTemplate + proxool
Only jdbcTemplate is used, and proxool is used in the connection pool.
1.
Proxool:
Http://proxool.sourceforge.net/
2. Imported packages
Asm-3.3.1.jar
Cglib-2.2.2.jar
Commons-logging-1.1.1.jar
Log4j-1.2.16.jar
Mysql-connector-java-5.1.8-bin.jar
Org. springframework. beans-3.1.1.RELEASE.jar
Org. springframework. core-3.1.1.RELEASE.jar
Org. springframework. jdbc-3.1.1.RELEASE.jar
Org. springframework. transaction-3.1.1.RELEASE.jar
Proxool-0.9.1.jar
Proxool-cglib.jar
3. Profiles/WEB-INF/proxool. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Something-else-entirely>
<Proxool>
<Alias> dbname </alias>
<Driver-url> jdbc: mysql: // 118.186.210.242: 447/wucaiju? User = root & password = root & useUnicode = true & amp; characterEncoding = UTF-8 </driver-url>
<Driver-class> com. mysql. jdbc. Driver </driver-class>
<Driver-properties>
<Property name = "user" value = "root"/>
<Property name = "password" value = "root"/>
</Driver-properties>
<Maximum-connection-count> 100 </maximum-connection-count>
<Minimum-connection-count> 10 </minimum-connection-count>
<House-keeping-sleep-time> 30000 <Maximum-new-connections> 10 </maximum-new-connections>
<Prototype-count> 5 </prototype-count>
</Proxool>
</Something-else-entirely>
4. web. xml
<Servlet>
<Servlet-name> ServletConfigurator </servlet-name>
<Servlet-class>
Org. logicalcobwebs. proxool. configuration. ServletConfigurator
</Servlet-class>
<Init-param>
<Param-name> xmlFile </param-name>
<Param-value> WEB-INF/proxool. xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> performance_dbname </servlet-Name>
<Servlet-class> org. logicalcobwebs. proxool. admin. servlet. adminservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> performance_dbname </servlet-Name>
<URL-pattern>/datasource_dbname </url-pattern>
</Servlet-mapping>
<! -- Configure the protected domain. Only the Tomcat administrator can view the connection pool information. -->
<Security-constraint>
<Web-resource-collection>
<Web-resource-name> proxool </web-resource-name>
<Url-pattern>/datasource_dbname </url-pattern>
</Web-resource-collection>
<Auth-constraint>
<Role-name> manager </role-name>
</Auth-constraint>
</Security-constraint>
<Login-config>
<Auth-method> BASIC </auth-method>
<Realm-name> proxool manager Application </realm-name>
</Login-config>
<Security-role>
<Description>
The role that is required to log in to the Manager
Application
</Description>
<Role-name> manager </role-name>
</Security-role>
5. Test class
Public class TestDao {
Public void test () throws Exception {
Context context = new InitialContext ();
// DataSource ds = (DataSource) context. lookup ("java:/comp/env/jdbc/mysql ");
// DataSource ds = (DataSource) context. lookup ("proxool. dbname ");
DataSource ds = new ProxoolDataSource ("dbname"); // the alias name instead of "proxool. alias"
JdbcTemplate jdbcTemplate = new JdbcTemplate (ds );
JdbcTemplate. afterPropertiesSet ();
System. out. println (jdbcTemplate );
String SQL = "select * from t_pic ";
List list = jdbcTemplate. queryForList (SQL );
For (int I = 0; I <list. size (); I ++ ){
Map map = (Map) list. get (I );
System. out. println (map. get ("createTime "));
}
}
}
6. Use proxool for direct jdbc
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
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;
Import org. logicalcobwebs. proxool. configuration. JAXPConfigurator;
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. dbname"); // note that the proxool + alias is used here.
// Datasource1 is the alias of the Connection Pool configured in proxool. xml. You can use datasource2 as needed.
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 ("dbname", 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 t_pic ");
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 ();
}
}
Public static void main (String args []) {
PoolManager pm = new PoolManager ();
Pm. getQuery ();
}
}