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
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28492.html