Proxool单独应用比较麻烦毕竟自己实现的数据库连接池管理工具难免有不足之处因此可以考虑与Spring结合形成一个DataSource对象来操作数据库这样比较简单灵活可靠性也高
下面是在上文例子的基础上所改写的环境如下:
Spring
proxool
JDK
写Spring的配置文件
syndsconfigxml
<?xml version= encoding=UTF?>
<beans
xmlns=/schema/beans
xmlns:xsi=//XMLSchemainstance
xsi:schemaLocation=/schema/beans
class=orglogicalcobwebsproxoolProxoolDataSource>
<property name=driver>
<value>oraclejdbcdriverOracleDriver</value>
</property>
<property name=driverUrl>
<value>jdbc:oracle:thin:@::tim</value>
</property>
<property name=user value=tim/>
<property name=password value=tim_/>
<property name=alias value=Pool_dbname/>
<property name=maximumActiveTime value=/>
<property name=prototypeCount value=/>
<property name=maximumConnectionCount value=/>
<property name=minimumConnectionCount value=/>
<property name=simultaneousBuildThrottle value=/>
<property name=houseKeepingTestSql value=select CURRENT_DATE/>
</bean>
</beans>
写Spring的上下文环境管理工具
import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA
*
* @author leizhimin ::
*/
public class ApplicationContextUtil {
private static ApplicationContext applicationContext;
static {
if (applicationContext == null)
applicationContext = rebuildApplicationContext();
}
/**
* 重新构建Spring应用上下文环境
*
* @return ApplicationContext
*/
public static ApplicationContext rebuildApplicationContext() {
return new ClassPathXmlApplicationContext(/syndsconfigxml);
}
/**
* 获取Spring应用上下文环境
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 简单的上下文环境测试
*/
public static void main(String[] args) {
rebuildApplicationContext();
if (applicationContext == null) {
Systemoutprintln(ApplicationContext is null);
} else {
Systemoutprintln(ApplicationContext is not null!);
}
}
}
写测试类
import javaxsqlDataSource;
import javasqlConnection;
import javasqlResultSet;
import javasqlStatement;
/**
* Created by IntelliJ IDEA
*
* @author leizhimin ::
*/
public class TestProxool {
public static String dburl = jdbc:oracle:thin:@::tim;
public static String user = tim;
public static String password = tim_;
/**
* JDBC方式测试
*
* @throws Exception
*/
public static void test() throws Exception {
String testsql = select * from village t where lastid = ;
//:注册驱动类
ClassforName(oraclejdbcdriverOracleDriver);
//:创建数据库连接
// Connection conn = DriverManagergetConnection(dburl user password);
DataSource ds = (DataSource) ApplicationContextUtilgetApplicationContext()getBean(dataSource);
Connection conn = dsgetConnection();
//:创建执行SQL的对象
Statement stmt = conncreateStatement();
//:执行SQL并获取返回结果
ResultSet rs = stmtexecuteQuery(testsql);
//:处理返回结果此处打印查询结果
while (rsnext()) {
Systemoutprint(rsgetLong(id) + \t);
Systemoutprint(rsgetString(name) + \t);
Systemoutprintln();
}
//:关闭数据库连接
connclose();
}
public static void main(String[] args) throws Exception {
test();
}
}
控制台输出如下:
:: INFO orgreCollectionFactory JDK + collections available
:: INFO orgspringframeworkbeansfactoryxmlXmlBeanDefinitionReader Loading XML bean definitions from class path resource [syndsconfigxml]
:: INFO orgntextsupportClassPathXmlApplicationContext Bean factory for application context [orgntextsupportClassPathXmlApplicationContext;hashCode=]: orgspringframeworkbeansfactorysupportDefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy
:: INFO orgntextsupportClassPathXmlApplicationContext beans defined in application context [orgntextsupportClassPathXmlApplicationContext;hashCode=]
:: INFO orgntextsupportClassPathXmlApplicationContext Unable to locate MessageSource with name messageSource: using default [orgntextsupportDelegatingMessageSource@dd]
:: INFO orgntextsupportClassPathXmlApplicationContext Unable to locate ApplicationEventMulticaster with name applicationEventMulticaster: using default [orgntexteventSimpleApplicationEventMulticaster@cff]
:: INFO orgspringframeworkbeansfactorysupportDefaultListableBeanFactory Preinstantiating singletons in factory [orgspringframeworkbeansfactorysupportDefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy]
张一村
张二村
张三村
张四村
南原村
辛庄村
凡村
西阳村
人马村
前关村
后关村
赵村
水淆村
沟东村
陈村
窑店村
坡头村
大安头
涧里村
人马寨
白草村
窑院村
寺下村
反上村
小安头
五花岭
东沟
西沟
南沟
王村
营前
东阳
太阳
丰阳
宜村
窑头
石原村
庙上村
庙洼
丁管营
涧西
:: INFO orglogicalcobwebsproxoolPool_dbname Shutting down Pool_dbname pool immediately [Shutdown Hook]
:: INFO orglogicalcobwebsproxoolPrototyperController Stopping Prototyper thread
:: INFO orglogicalcobwebsproxoolHouseKeeperController Stopping HouseKeeper thread
Process finished with exit code
info日志是logj输出的
当然可以在Spring中配置多个DataSource都没问题的
另外今天有个同事说Proxool不能配置多个连接池我经过测试Proxool可以配置多个连接池可以放心使用
配置文件如下小:
<?xml version= encoding=UTF?>
<beans
xmlns=/schema/beans
xmlns:xsi=//XMLSchemainstance
xsi:schemaLocation=/schema/beans
class=orglogicalcobwebsproxoolProxoolDataSource>
<property name=driver>
<value>oraclejdbcdriverOracleDriver</value>
</property>
<property name=driverUrl>
<value>jdbc:oracle:thin:@::tim</value>
</property>
<property name=user value=tim/>
<property name=password value=tim_/>
<property name=alias value=proxoola/>
<property name=maximumActiveTime value=/>
<property name=prototypeCount value=/>
<property name=maximumConnectionCount value=/>
<property name=minimumConnectionCount value=/>
<property name=simultaneousBuildThrottle value=/>
<property name=houseKeepingTestSql value=select CURRENT_DATE/>
</bean>
<bean id=dataSource
class=orglogicalcobwebsproxoolProxoolDataSource>
<property name=driver>
<value>oraclejdbcdriverOracleDriver</value>
</property>
<property name=driverUrl>
<value>jdbc:oracle:thin:@::orcl</value>
</property>
<property name=user value=rural/>
<property name=password value=rural/>
<property name=alias value=proxoola/>
<property name=maximumActiveTime value=/>
<property name=prototypeCount value=/>
<property name=maximumConnectionCount value=/>
<property name=minimumConnectionCount value=/>
<property name=simultaneousBuildThrottle value=/>
<property name=houseKeepingTestSql value=select CURRENT_DATE/>
</bean>
</beans>
另外进行极刑的变态测试代码***目的是为了将程序高挂可惜没挂呵呵!
import orgntextApplicationContext;
import javaxsqlDataSource;
import javasqlConnection;
import javasqlSQLException;
import javasqlStatement;
import javasqlResultSet;
/**
* Created by IntelliJ IDEA
*
* @author leizhimin ::
*/
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = ApplicationContextUtilgetApplicationContext();
DataSource ds = (DataSource)ctxgetBean(dataSource);
DataSource ds = (DataSource)ctxgetBean(dataSource);
Connection conn = dsgetConnection();
Connection conn = dsgetConnection();
Statement stmt = conncreateStatement();
//:执行SQL并获取返回结果
ResultSet rs = stmtexecuteQuery(select * from city);
//:处理返回结果此处打印查询结果
while (rsnext()) {
Systemoutprint(rsgetLong(id) + \t);
Systemoutprint(rsgetString(name) + \t);
Systemoutprintln();
Statement stmt = conncreateStatement();
//:执行SQL并获取返回结果
// ResultSet rs = stmtexecuteQuery(select * from city);
ResultSet rs = stmtexecuteQuery(select * from lm where lm_id = +rsgetLong(id));
//:处理返回结果此处打印查询结果
while (rsnext()) {
Systemoutprintln(rsgetLong());
}
Systemoutprintln(<<<<);
//:关闭数据库连接
}
//:关闭数据库连接
connclose();
connclose();
// Systemoutprintln();
//
// Statement stmt = conncreateStatement();
// //:执行SQL并获取返回结果
// ResultSet rs = stmtexecuteQuery(select count(*) from lm);
// //:处理返回结果此处打印查询结果
// while (rsnext()) {
// Systemoutprintln(rsgetLong());
// }
// //:关闭数据库连接
// connclose();
}
}
输出如下:
:: INFO orgreCollectionFactory JDK + collections available
:: INFO orgspringframeworkbeansfactoryxmlXmlBeanDefinitionReader Loading XML bean definitions from class path resource [syndsconfigxml]
:: INFO orgntextsupportClassPathXmlApplicationContext Bean factory for application context [orgntextsupportClassPathXmlApplicationContext;hashCode=]: orgspringframeworkbeansfactorysupportDefaultListableBeanFactory defining beans [dataSourcedataSource]; root of BeanFactory hierarchy
:: INFO orgntextsupportClassPathXmlApplicationContext beans defined in application context [orgntextsupportClassPathXmlApplicationContext;hashCode=]
:: INFO orgntextsupportClassPathXmlApplicationContext Unable to locate MessageSource with name messageSource: using default [orgntextsupportDelegatingMessageSource@dfef]
:: INFO orgntextsupportClassPathXmlApplicationContext Unable to locate ApplicationEventMulticaster with name applicationEventMulticaster: using default [orgntexteventSimpleApplicationEventMulticaster@bed]
:: INFO orgspringframeworkbeansfactorysupportDefaultListableBeanFactory Preinstantiating singletons in factory [orgspringframeworkbeansfactorysupportDefaultListableBeanFactory defining beans [dataSourcedataSource]; root of BeanFactory hierarchy]
郑州市
<<<<
开封市
<<<<
洛阳市
<<<<
平顶山市
<<<<
安阳市
<<<<
鹤壁市
<<<<
新乡市
<<<<
焦作市
<<<<
济源市
<<<<
濮阳市
<<<<
许昌市
<<<<
漯河市
<<<<
三门峡市
<<<<
南阳市
<<<<
商丘市
<<<<
信阳市
<<<<
周口市
<<<<
驻马店市
<<<<
:: INFO orglogicalcobwebsproxoolproxoola Shutting down proxoola pool immediately [Shutdown Hook]
:: INFO orglogicalcobwebsproxoolproxoola Shutting down proxoola pool immediately [Shutdown Hook]
:: INFO orglogicalcobwebsproxoolPrototyperController Stopping Prototyper thread
:: INFO orglogicalcobwebsproxoolHouseKeeperController Stopping HouseKeeper thread
Process finished with exit code