was数据源配置文件 配置Spring的Proxool数据源
配置Spring的Proxool数据源
Proxool单独应用比较麻烦 毕竟自己实现的数据库连接池管理工具难免有不足之处 因此可以考虑与Spring结合 形成一个DataSource对象来操作数据库 这样比较简单灵活 可靠性也高
下面是在上文例子的基础上所改写的 环境如下:
Spring
proxool
JDK
写Spring的配置文件
syndsconfig xml
<?xml version= encoding= UTF ?>
<beans
xmlns= /schema/beans
xmlns:xsi= / /XMLSchema instance
xsi:schemaLocation= /schema/beans
class= logicalcobwebs proxool ProxoolDataSource >
<property name= driver >
<value>oracle jdbc driver OracleDriver</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 ntext ApplicationContext;
import ntext support ClassPathXmlApplicationContext;
/**
* 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( /syndsconfig xml );
}
/**
* 获取Spring应用上下文环境
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 简单的上下文环境测试
*/
public static void main(String[] args) {
rebuildApplicationContext();
if (applicationContext == null) {
System out println( ApplicationContext is null );
} else {
System out println( ApplicationContext is not null! );
}
}
}
写测试类
import javax sql DataSource;
import java sql Connection;
import java sql ResultSet;
import java sql Statement;
/**
* 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 = ;
// :注册驱动类
Class forName( oracle jdbc driver OracleDriver );
// :创建数据库连接
// Connection conn = DriverManager getConnection(dburl user password);
DataSource ds = (DataSource) ApplicationContextUtil getApplicationContext() getBean( dataSource );
Connection conn = ds getConnection();
// :创建执行SQL的对象
Statement stmt = conn createStatement();
// :执行SQL 并获取返回结果
ResultSet rs = stmt executeQuery(testsql);
// :处理返回结果 此处打印查询结果
while (rs next()) {
System out print(rs getLong( id ) + t );
System out print(rs getString( name ) + t );
System out println();
}
// :关闭数据库连接
conn close();
}
public static void main(String[] args) throws Exception {
test ();
}
}
控制台输出如下:
: : INFO re CollectionFactory JDK + collections available
: : INFO springframework beans factory xml XmlBeanDefinitionReader Loading XML bean definitions from class path resource [syndsconfig xml]
: : INFO ntext support ClassPathXmlApplicationContext Bean factory for application context [ntext support ClassPathXmlApplicationContext;hashCode= ]: springframework beans factory support DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy
: : INFO ntext support ClassPathXmlApplicationContext beans defined in application context [ntext support ClassPathXmlApplicationContext;hashCode= ]
: : INFO ntext support ClassPathXmlApplicationContext Unable to locate MessageSource with name messageSource : using default [ntext support DelegatingMessageSource@ dd]
: : INFO ntext support ClassPathXmlApplicationContext Unable to locate ApplicationEventMulticaster with name applicationEventMulticaster : using default [ntext event SimpleApplicationEventMulticaster@c f f ]
: : INFO springframework beans factory support DefaultListableBeanFactory Pre instantiating singletons in factory [ springframework beans factory support DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy]
张一村
张二村
张三村
张四村
南原村
辛庄村
凡村
西阳村
人马村
前关村
后关村
赵村
水淆村
沟东村
陈村
窑店村
坡头村
大安头
涧里村
人马寨
白草村
窑院村
寺下村
反上村
小安头
五花岭
东沟
西沟
南沟
王村
营前
东阳
太阳
丰阳
宜村
窑头
石原村
庙上村
庙洼
丁管营
涧西
: : INFO logicalcobwebs proxool Pool_dbname Shutting down Pool_dbname pool immediately [Shutdown Hook]
: : INFO logicalcobwebs proxool PrototyperController Stopping Prototyper thread
: : INFO logicalcobwebs proxool HouseKeeperController Stopping HouseKeeper thread
Process finished with exit code
info日志是log j输出的
当然 可以在Spring中配置多个DataSource 都没问题的
另外今天有个同事说Proxool不能配置多个连接池 我经过测试 Proxool可以配置多个连接池 可以放心使用
配置文件如下小:
<?xml version= encoding= UTF ?>
<beans
xmlns= /schema/beans
xmlns:xsi= / /XMLSchema instance
xsi:schemaLocation= /schema/beans
class= logicalcobwebs proxool ProxoolDataSource >
<property name= driver >
<value>oracle jdbc driver OracleDriver</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= proxool a />
<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= logicalcobwebs proxool ProxoolDataSource >
<property name= driver >
<value>oracle jdbc driver OracleDriver</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= proxool a />
<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 ntext ApplicationContext;
import javax sql DataSource;
import java sql Connection;
import java sql SQLException;
import java sql Statement;
import java sql ResultSet;
/**
* Created by IntelliJ IDEA
*
* @author leizhimin : :
*/
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = ApplicationContextUtil getApplicationContext();
DataSource ds = (DataSource)ctx getBean( dataSource );
DataSource ds = (DataSource)ctx getBean( dataSource );
Connection conn = ds getConnection();
Connection conn = ds getConnection();
Statement stmt = conn createStatement();
// :执行SQL 并获取返回结果
ResultSet rs = stmt executeQuery( select * from city );
// :处理返回结果 此处打印查询结果
while (rs next()) {
System out print(rs getLong( id ) + t );
System out print(rs getString( name ) + t );
System out println();
Statement stmt = conn createStatement();
// :执行SQL 并获取返回结果
// ResultSet rs = stmt executeQuery( select * from city );
ResultSet rs = stmt executeQuery( select * from lm where lm_id = +rs getLong( id ));
// :处理返回结果 此处打印查询结果
while (rs next()) {
System out println(rs getLong( ));
}
System out println( <<<< );
// :关闭数据库连接
}
// :关闭数据库连接
conn close();
conn close();
// System out println( );
//
// Statement stmt = conn createStatement();
// // :执行SQL 并获取返回结果
// ResultSet rs = stmt executeQuery( select count(*) from lm );
// // :处理返回结果 此处打印查询结果
// while (rs next()) {
// System out println(rs getLong( ));
// }
// // :关闭数据库连接
// conn close();
}
}
输出如下:
: : INFO re CollectionFactory JDK + collections available
: : INFO springframework beans factory xml XmlBeanDefinitionReader Loading XML bean definitions from class path resource [syndsconfig xml]
: : INFO ntext support ClassPathXmlApplicationContext Bean factory for application context [ntext support ClassPathXmlApplicationContext;hashCode= ]: springframework beans factory support DefaultListableBeanFactory defining beans [dataSource dataSource ]; root of BeanFactory hierarchy
: : INFO ntext support ClassPathXmlApplicationContext beans defined in application context [ntext support ClassPathXmlApplicationContext;hashCode= ]
: : INFO ntext support ClassPathXmlApplicationContext Unable to locate MessageSource with name messageSource : using default [ntext support DelegatingMessageSource@ dfef ]
: : INFO ntext support ClassPathXmlApplicationContext Unable to locate ApplicationEventMulticaster with name applicationEventMulticaster : using default [ntext event SimpleApplicationEventMulticaster@ bed ]
: : INFO springframework beans factory support DefaultListableBeanFactory Pre instantiating singletons in factory [ springframework beans factory support DefaultListableBeanFactory defining beans [dataSource dataSource ]; root of BeanFactory hierarchy]
郑州市
<<<<
开封市
<<<<
洛阳市
<<<<
平顶山市
<<<<
安阳市
<<<<
鹤壁市
<<<<
新乡市
<<<<
焦作市
<<<<
济源市
<<<<
濮阳市
<<<<
许昌市
<<<<
漯河市
<<<<
三门峡市
<<<<
南阳市
<<<<
商丘市
<<<<
信阳市
<<<<
周口市
<<<<
驻马店市
<<<<
: : INFO logicalcobwebs proxool proxool a Shutting down proxool a pool immediately [Shutdown Hook]
: : INFO logicalcobwebs proxool proxool a Shutting down proxool a pool immediately [Shutdown Hook]
: : INFO logicalcobwebs proxool PrototyperController Stopping Prototyper thread
: : INFO logicalcobwebs proxool HouseKeeperController Stopping HouseKeeper thread
lishixinzhi/Article/program/Java/ky/201311/28492