c3p0的介绍可参见:http://www.micmiu.com/j2ee/jdbc-tech/c3p0-simple
dbcp的介绍可参见:http://www.micmiu.com/j2ee/jdbc-tech/apache-dbcp-simple
本文的章节目录:
一、参数详细说明
二、多种实现方式的演示(以mysql为例)
[ 1 ]、最基础的实现
TestProxool.java:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class TestProxool { public static void main(String[] args) { Connection connection = null; Properties info = new Properties(); info.setProperty("proxool.maximum-connection-count", "10"); info.setProperty("proxool.minimum-connection-count", "3"); info.setProperty("proxool.house-keeping-test-sql", "select now()"); info.setProperty("user", "root"); info.setProperty("password", ""); String alias = "test"; String driverClass = "com.mysql.jdbc.Driver"; String driverUrl = "jdbc:mysql://localhost/michaeldemo"; String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl; try { try { // 这步不能缺少 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); } catch (ClassNotFoundException e) { System.out.println("Couldn't find driver" + e.getMessage()); e.printStackTrace(); } try { connection = DriverManager.getConnection(url, info); } catch (SQLException e) { System.out.println("Problem getting connection " + e.getMessage()); e.printStackTrace(); } if (connection != null) { System.out.println("Got connection successful : )"); } else { System.out.println("Didn't get connection, which probably means that no Driver accepted the URL"); } } catch (Exception e) { e.printStackTrace(); } finally { try { // Check to see we actually got a connection before we attempt to close it. if (connection != null) { connection.close(); } } catch (SQLException e) { System.out.println("Problem closing connection" + e.getMessage()); e.printStackTrace(); } } } }
?
运行结果:
Got connection successful : )
[ 2 ]、通过properties配置文件实现
proxool-demo.properties:
?
#The first word (up to the first dot) must start with "jdbc", but it can be anything you like. #Use unique names to identify each pool. Any property not starting with "jdbc" will be ignored. #The properties prefixed with "proxool." will be used by Proxool #while the properties that are not prefixed will be passed on to the delegate JDBC driver. jdbc-0.proxool.alias=property-test jdbc-0.proxool.driver-url=jdbc:mysql://localhost/michaeldemo jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver jdbc-0.user=root jdbc-0.password= jdbc-0.proxool.house-keeping-sleep-time=40000 jdbc-0.proxool.house-keeping-test-sql=select now() jdbc-0.proxool.maximum-connection-count=10 jdbc-0.proxool.minimum-connection-count=3 jdbc-0.proxool.maximum-connection-lifetime=18000000 jdbc-0.proxool.simultaneous-build-throttle=5 jdbc-0.proxool.recently-started-threshold=40000 jdbc-0.proxool.overload-without-refusal-lifetime=50000 jdbc-0.proxool.maximum-active-time=60000 jdbc-0.proxool.verbose=true
?
TestProxoolCfgPp.java:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.logicalcobwebs.proxool.ProxoolException; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; public class TestProxoolCfgPp { /** * * @param args */ public static void main(String[] args) { Connection connection = null; try { try { PropertyConfigurator.configure("src/main/java/michael/jdbc/proxool/proxool-demo.properties"); } catch (ProxoolException e) { System.out.println("JAXPConfigurator configure error:" + e.getMessage()); e.printStackTrace(); } try { connection = DriverManager.getConnection("proxool.property-test"); } catch (SQLException e) { System.out.println("Problem getting connection " + e.getMessage()); e.printStackTrace(); } if (connection != null) { System.out.println("Got connection successful : )"); } else { System.out.println("Didn't get connection, which probably means that no Driver accepted the URL"); } } catch (Exception e) { e.printStackTrace(); } finally { try { // Check to see we actually got a connection before we attempt to close it. if (connection != null) { connection.close(); } } catch (SQLException e) { System.out.println("Problem closing connection" + e.getMessage()); e.printStackTrace(); } } } }
?
运行结果:
Got connection successful : )
ps:代码中DriverManager.getConnection(“proxool.property-test”)的“property-test“需要和配置文件中jdbc-0.proxool.alias=property-test的值一致
[ 3 ]、通过XML配置文件实现
proxool-demo.xml:
<?xml version="1.0" encoding="utf-8"?> <something-else-entirely> <proxool> <alias>xml-test</alias> <driver-url>jdbc:mysql://localhost/michaeldemo</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root" /> <property name="password" value="" /> </driver-properties> <maximum-connection-count>10</maximum-connection-count> <minimum-connection-count>3</minimum-connection-count> <house-keeping-test-sql>select now()</house-keeping-test-sql> </proxool> </something-else-entirely>
?
TestProxoolCfgXml.java?:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.logicalcobwebs.proxool.ProxoolException; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; public class TestProxoolCfgPp { public static void main(String[] args) { Connection connection = null; try { try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); } catch (ClassNotFoundException e) { System.out.println("Couldn't find driver" + e.getMessage()); e.printStackTrace(); } try { PropertyConfigurator.configure("src/main/java/michael/jdbc/proxool/proxool-demo.properties"); } catch (ProxoolException e) { System.out.println("JAXPConfigurator configure error:" + e.getMessage()); e.printStackTrace(); } try { connection = DriverManager.getConnection("proxool.property-test"); } catch (SQLException e) { System.out.println("Problem getting connection " + e.getMessage()); e.printStackTrace(); } if (connection != null) { System.out.println("Got connection successful : )"); } else { System.out.println("Didn't get connection, which probably means that no Driver accepted the URL"); } } catch (Exception e) { e.printStackTrace(); } finally { try { // Check to see we actually got a connection before we attempt to close it. if (connection != null) { connection.close(); } } catch (SQLException e) { System.out.println("Problem closing connection" + e.getMessage()); e.printStackTrace(); } } } }
?
运行结果:
Got connection successful : )ps:代码中DriverManager.getConnection(“proxool.property-test”)的"property-test"需要和配置文件中jdbc-0.proxool.alias=property-test的值一致
[ 4 ]、通过注册的方式实现
TestProxoolRegister.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; import org.logicalcobwebs.proxool.ProxoolException; import org.logicalcobwebs.proxool.ProxoolFacade; public class TestProxoolRegister { public static void main(String[] args) { Connection connection = null; Properties info = new Properties(); info.setProperty("proxool.maximum-connection-count", "10"); info.setProperty("proxool.minimum-connection-count", "3"); info.setProperty("proxool.house-keeping-test-sql", "select now()"); info.setProperty("user", "root"); info.setProperty("password", ""); String alias = "test"; String driverClass = "com.mysql.jdbc.Driver"; String driverUrl = "jdbc:mysql://localhost/iecms"; String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl; try { try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); } catch (ClassNotFoundException e) { System.out.println("Couldn't find driver" + e.getMessage()); e.printStackTrace(); } try { ProxoolFacade.registerConnectionPool(url, info); } catch (ProxoolException e) { System.out.println("Problem getting connection " + e.getMessage()); e.printStackTrace(); } connection = DriverManager.getConnection("proxool.test"); if (connection != null) { System.out.println("Got connection successful : )"); } else { System.out.println("Didn't get connection, which probably means that no Driver accepted the URL"); } Thread.sleep(10 * 1000L); } catch (Exception e) { System.out.println("error:" + e.getMessage()); e.printStackTrace(); } finally { try { // Check to see we actually got a connection before we attempt // to close it. if (connection != null) { connection.close(); } } catch (SQLException e) { System.out.println("Problem closing connection" + e.getMessage()); e.printStackTrace(); } } } }
??
运行结果:
Got connection successful : ) ? ?三、结合spring的实例演示
配置文件michael/jdbc/proxool/proxool.ds.spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value> classpath:michael/jdbc/proxool/proxool.jdbc.properties </value> </list> </property> </bean> <bean id="proxoolDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <property name="alias" value="${jdbc.alias}" /> <property name="driver" value="${jdbc.driverClassName}" /> <property name="driverUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minimumConnectionCount" value="3" /> <property name="maximumConnectionCount" value="10" /> <property name="delegateProperties" value="autoCommit=true, foo=5" /> </bean> </beans>
?
ProxoolInSpringExample.java:
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.logicalcobwebs.proxool.ProxoolDataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProxoolInSpringExample { public static void main(String[] args) { System.out.println("c3p0.ds.spring.xml init start "); ApplicationContext appCt = new ClassPathXmlApplicationContext("michael/jdbc/proxool/proxool.ds.spring.xml"); System.out.println("spring bean create proxool.ProxoolDataSource"); ProxoolDataSource dataSource = (ProxoolDataSource) appCt.getBean("proxoolDataSource"); String testSql = "select * from TB_MYTEST"; Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection start."); conn = dataSource.getConnection(); System.out.println("Creating statement start."); stmt = conn.createStatement(); System.out.println("Executing statement start."); rset = stmt.executeQuery(testSql); System.out.println("executeQuery Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } System.out.println("Results display done."); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { e.printStackTrace(); } try { if (stmt != null){ stmt.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (conn != null){ conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
?
运行结果:
c3p0.ds.spring.xml init start spring bean create proxool.ProxoolDataSource Creating connection start. Creating statement start. Executing statement start. executeQuery Results: 1 batch_add_0 2011-06-16 14:29:08.0 2 batch_add_1 2011-06-16 14:29:08.0 3 batch_add_2 2011-06-16 14:29:08.0 4 batch_add_3 2011-06-16 14:29:08.0 5 batch_add_4 2011-06-16 14:29:08.0 Results display done.
?
?
转载自micmiu – 软件开发+生活点滴[?http://www.micmiu.com/?]
?