配置Spring的Proxool数据源

简介:

Proxool单独应用比较麻烦,毕竟自己实现的数据库连接池管理工具难免有不足之处,因此可以考虑与Spring结合,形成一个DataSource对象来操作数据库,这样比较简单灵活,可靠性也高。

 
下面是在上文例子的基础上所改写的,环境如下:
Spring 2.0
proxool-0.9.1
JDK1.5
 
1、写Spring的配置文件
 
syndsconfig.xml
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< beans 
                 xmlns ="http://www.springframework.org/schema/beans" 
                 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > 

         < bean  id ="dataSource" 
                     class ="org.logicalcobwebs.proxool.ProxoolDataSource" > 
                 < property  name ="driver" > 
                         < value >oracle.jdbc.driver.OracleDriver </ value > 
                 </ property > 
                 < property  name ="driverUrl" > 
                         < value >jdbc:oracle:thin:@192.168.104.192:1521:tim </ value > 
                 </ property > 
                 < property  name ="user"  value ="tim" /> 
                 < property  name ="password"  value ="tim_8968888" /> 
                 < property  name ="alias"  value ="Pool_dbname" /> 
                 < property  name ="maximumActiveTime"  value ="300000" /> 
                 < property  name ="prototypeCount"  value ="0" /> 
                 < property  name ="maximumConnectionCount"  value ="50" /> 
                 < property  name ="minimumConnectionCount"  value ="2" /> 
                 < property  name ="simultaneousBuildThrottle"  value ="50" /> 
                 < property  name ="houseKeepingTestSql"  value ="select CURRENT_DATE" /> 
         </ bean > 
</ beans >
 
2、写Spring的上下文环境管理工具
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

/** 
* Created by IntelliJ IDEA. 

* @author leizhimin 2009-10-15 10:21:38 
*/
 
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!"); 
                } 
        } 
}
 
3、写测试类
import javax.sql.DataSource; 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.Statement; 


/** 
* Created by IntelliJ IDEA. 

* @author leizhimin 2009-10-10 17:59:47 
*/
 
public  class TestProxool { 
         public  static String dburl =  "jdbc:oracle:thin:@192.168.104.192:1521:tim"
         public  static String user =  "tim"
         public  static String password =  "tim_8968888"

         /** 
         * JDBC方式测试 
         * 
         * @throws Exception 
         */
 
         public  static  void test1()  throws Exception { 
                String testsql =  "select * from village t where lastid = 346"
                 //1:注册驱动类 
                Class.forName( "oracle.jdbc.driver.OracleDriver"); 
                 //2:创建数据库连接 
//                Connection conn = DriverManager.getConnection(dburl, user, password); 
                DataSource ds = (DataSource) ApplicationContextUtil.getApplicationContext().getBean( "dataSource"); 
                Connection conn = ds.getConnection(); 
                 //3:创建执行SQL的对象 
                Statement stmt = conn.createStatement(); 
                 //4:执行SQL,并获取返回结果 
                ResultSet rs = stmt.executeQuery(testsql); 
                 //5:处理返回结果,此处打印查询结果 
                 while (rs.next()) { 
                        System.out.print(rs.getLong( "id") +  "\t"); 
                        System.out.print(rs.getString( "name") +  "\t"); 
                        System.out.println(); 
                } 
                 //6:关闭数据库连接 
                conn.close(); 
        } 


         public  static  void main(String[] args)  throws Exception { 
                test1(); 
        } 
}
 
控制台输出如下:
2009-10-15 12:37:03    - INFO    org.springframework.core.CollectionFactory         - JDK 1.4+ collections available 
2009-10-15 12:37:03    - INFO    org.springframework.beans.factory.xml.XmlBeanDefinitionReader         - Loading XML bean definitions from  class path resource [syndsconfig.xml] 
2009-10-15 12:37:04    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Bean factory  for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9737354]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy 
2009-10-15 12:37:04    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - 1 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=9737354] 
2009-10-15 12:37:04    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Unable to locate MessageSource with name 'messageSource': using  default [org.springframework.context.support.DelegatingMessageSource@10655dd] 
2009-10-15 12:37:04    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using  default[org.springframework.context.event.SimpleApplicationEventMulticaster@c8f6f8] 
2009-10-15 12:37:04    - INFO    org.springframework.beans.factory.support.DefaultListableBeanFactory         - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource]; root of BeanFactory hierarchy] 
5411  张一村    
5412  张二村    
5413  张三村    
5414  张四村    
5415  南原村    
5416  辛庄村    
5417  凡村    
5418  西阳村    
5419  人马村    
5420  前关村    
5421  后关村    
5422  赵村    
5423  水淆村    
5424  沟东村    
5425  陈村    
5426  窑店村    
5427  坡头村    
20588  大安头    
20589  涧里村    
20590  人马寨    
20591  白草村    
20592  窑院村    
20593  寺下村    
20594  反上村    
33651  小安头     
33652  五花岭     
33653  东沟     
33654  西沟    
33655  南沟     
33656  王村     
33657  营前    
33659  东阳    
33661  太阳     
33663  丰阳    
33665  宜村    
33667  窑头    
32225  石原村    
32226  庙上村    
32227  庙洼    
38739  丁管营    
38841  涧西    
2009-10-15 12:37:05    - INFO    org.logicalcobwebs.proxool.Pool_dbname         - Shutting down 'Pool_dbname' pool immediately [Shutdown Hook] 
2009-10-15 12:37:05    - INFO    org.logicalcobwebs.proxool.PrototyperController         - Stopping Prototyper thread 
2009-10-15 12:37:05    - INFO    org.logicalcobwebs.proxool.HouseKeeperController         - Stopping HouseKeeper thread 

Process finished with exit code 0
 
info日志是log4j输出的。
 
当然,可以在Spring中配置多个DataSource,都没问题的。
 
----------------------
另外今天有个同事说Proxool不能配置多个连接池,我经过测试,Proxool可以配置多个连接池,可以放心使用。
配置文件如下小:
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< beans 
                 xmlns ="http://www.springframework.org/schema/beans" 
                 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" > 

         < bean  id ="dataSource" 
                     class ="org.logicalcobwebs.proxool.ProxoolDataSource" > 
                 < property  name ="driver" > 
                         < value >oracle.jdbc.driver.OracleDriver </ value > 
                 </ property > 
                 < property  name ="driverUrl" > 
                         < value >jdbc:oracle:thin:@192.168.104.192:1521:tim </ value > 
                 </ property > 
                 < property  name ="user"  value ="tim" /> 
                 < property  name ="password"  value ="tim_8968888" /> 
                 < property  name ="alias"  value ="proxool.a2" /> 
                 < property  name ="maximumActiveTime"  value ="300000" /> 
                 < property  name ="prototypeCount"  value ="0" /> 
                 < property  name ="maximumConnectionCount"  value ="50" /> 
                 < property  name ="minimumConnectionCount"  value ="2" /> 
                 < property  name ="simultaneousBuildThrottle"  value ="50" /> 
                 < property  name ="houseKeepingTestSql"  value ="select CURRENT_DATE" /> 
         </ bean > 
         < bean  id ="dataSource2" 
                     class ="org.logicalcobwebs.proxool.ProxoolDataSource" > 
                 < property  name ="driver" > 
                         < value >oracle.jdbc.driver.OracleDriver </ value > 
                 </ property > 
                 < property  name ="driverUrl" > 
                         < value >jdbc:oracle:thin:@192.168.104.164:1521:orcl </ value > 
                 </ property > 
                 < property  name ="user"  value ="rural" /> 
                 < property  name ="password"  value ="rural" /> 
                 < property  name ="alias"  value ="proxool.a1" /> 
                 < property  name ="maximumActiveTime"  value ="300000" /> 
                 < property  name ="prototypeCount"  value ="0" /> 
                 < property  name ="maximumConnectionCount"  value ="50" /> 
                 < property  name ="minimumConnectionCount"  value ="2" /> 
                 < property  name ="simultaneousBuildThrottle"  value ="50" /> 
                 < property  name ="houseKeepingTestSql"  value ="select CURRENT_DATE" /> 
         </ bean > 
</ beans >
 
另外,进行极刑的变态测试,代码***,目的是为了将程序高挂,可惜没挂,呵呵!
import org.springframework.context.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 2009-10-15 17:39:50 
*/
 
public  class Test { 

         public  static  void main(String[] args)  throws SQLException { 
             ApplicationContext ctx = ApplicationContextUtil.getApplicationContext(); 
                DataSource ds1 = (DataSource)ctx.getBean( "dataSource"); 
                DataSource ds2 = (DataSource)ctx.getBean( "dataSource2"); 

                Connection conn1 = ds1.getConnection(); 
                Connection conn2 = ds2.getConnection(); 

                Statement stmt = conn1.createStatement(); 
                 //4:执行SQL,并获取返回结果 
                ResultSet rs = stmt.executeQuery( "select * from city"); 
                 //5:处理返回结果,此处打印查询结果 
                 while (rs.next()) { 
                        System.out.print(rs.getLong( "id") +  "\t"); 
                        System.out.print(rs.getString( "name") +  "\t"); 
                        System.out.println(); 

                        Statement stmt2 = conn2.createStatement(); 
                         //4:执行SQL,并获取返回结果 
//                        ResultSet rs2 = stmt2.executeQuery("select * from city"); 
                        ResultSet rs2 = stmt2.executeQuery( "select * from lm where lm_id = "+rs.getLong( "id")); 
                         //5:处理返回结果,此处打印查询结果 
                         while (rs2.next()) { 
                                System.out.println(rs2.getLong(1)); 
                        } 
                        System.out.println( "<<<<"); 
                         //6:关闭数据库连接 

                } 
                 //6:关闭数据库连接 
                conn1.close(); 
                conn2.close(); 
                 

//                System.out.println("----------"); 
// 
//                Statement stmt2 = conn2.createStatement(); 
//                //4:执行SQL,并获取返回结果 
//                ResultSet rs2 = stmt2.executeQuery("select count(*) from lm"); 
//                //5:处理返回结果,此处打印查询结果 
//                while (rs2.next()) { 
//                        System.out.println(rs2.getLong(1)); 
//                } 
//                //6:关闭数据库连接 
//                conn2.close(); 


        } 
}
 
输出如下:
2009-10-15 18:25:37    - INFO    org.springframework.core.CollectionFactory         - JDK 1.4+ collections available 
2009-10-15 18:25:37    - INFO    org.springframework.beans.factory.xml.XmlBeanDefinitionReader         - Loading XML bean definitions from  class path resource [syndsconfig.xml] 
2009-10-15 18:25:37    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Bean factory  for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6161922]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,dataSource2]; root of BeanFactory hierarchy 
2009-10-15 18:25:37    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - 2 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=6161922] 
2009-10-15 18:25:37    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Unable to locate MessageSource with name 'messageSource': using  default [org.springframework.context.support.DelegatingMessageSource@18dfef8] 
2009-10-15 18:25:37    - INFO    org.springframework.context.support.ClassPathXmlApplicationContext         - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using  default[org.springframework.context.event.SimpleApplicationEventMulticaster@134bed0] 
2009-10-15 18:25:37    - INFO    org.springframework.beans.factory.support.DefaultListableBeanFactory         - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,dataSource2]; root of BeanFactory hierarchy] 
410100  郑州市    
<<<< 
410200  开封市    
<<<< 
410300  洛阳市    
<<<< 
410400  平顶山市    
<<<< 
410500  安阳市    
<<<< 
410600  鹤壁市    
<<<< 
410700  新乡市    
<<<< 
410800  焦作市    
<<<< 
410881  济源市    
<<<< 
410900  濮阳市    
<<<< 
411000  许昌市    
<<<< 
411100  漯河市    
<<<< 
411200  三门峡市    
<<<< 
411300  南阳市    
<<<< 
411400  商丘市    
<<<< 
411500  信阳市    
<<<< 
411600  周口市    
<<<< 
411700  驻马店市    
<<<< 
2009-10-15 18:25:39    - INFO    org.logicalcobwebs.proxool.proxool.a2         - Shutting down 'proxool.a2' pool immediately [Shutdown Hook] 
2009-10-15 18:25:39    - INFO    org.logicalcobwebs.proxool.proxool.a1         - Shutting down 'proxool.a1' pool immediately [Shutdown Hook] 
2009-10-15 18:25:39    - INFO    org.logicalcobwebs.proxool.PrototyperController         - Stopping Prototyper thread 
2009-10-15 18:25:39    - INFO    org.logicalcobwebs.proxool.HouseKeeperController         - Stopping HouseKeeper thread 

Process finished with exit code 0
 
如果大家想看Web应用的例子,可以参看:
 

本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/212616,如需转载请自行联系原作者
相关文章
|
25天前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
167 73
|
2月前
|
Java 开发者 微服务
手写模拟Spring Boot自动配置功能
【11月更文挑战第19天】随着微服务架构的兴起,Spring Boot作为一种快速开发框架,因其简化了Spring应用的初始搭建和开发过程,受到了广大开发者的青睐。自动配置作为Spring Boot的核心特性之一,大大减少了手动配置的工作量,提高了开发效率。
73 0
|
25天前
|
Java Spring
【Spring配置相关】启动类为Current File,如何更改
问题场景:当我们切换类的界面的时候,重新启动的按钮是灰色的,不能使用,并且只有一个Current File 项目,下面介绍两种方法来解决这个问题。
|
25天前
|
Java Spring
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。
|
25天前
|
Java Spring
【Spring配置】创建yml文件和properties或yml文件没有绿叶
本文主要针对,一个项目中怎么创建yml和properties两种不同文件,进行配置,和启动类没有绿叶标识进行解决。
|
1月前
|
NoSQL Java Redis
Spring Boot 自动配置机制:从原理到自定义
Spring Boot 的自动配置机制通过 `spring.factories` 文件和 `@EnableAutoConfiguration` 注解,根据类路径中的依赖和条件注解自动配置所需的 Bean,大大简化了开发过程。本文深入探讨了自动配置的原理、条件化配置、自定义自动配置以及实际应用案例,帮助开发者更好地理解和利用这一强大特性。
104 14
|
1月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
65 6
|
3月前
|
Java API Spring
在 Spring 配置文件中配置 Filter 的步骤
【10月更文挑战第21天】在 Spring 配置文件中配置 Filter 是实现请求过滤的重要手段。通过合理的配置,可以灵活地对请求进行处理,满足各种应用需求。还可以根据具体的项目要求和实际情况,进一步深入研究和优化 Filter 的配置,以提高应用的性能和安全性。
|
1月前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
113 3
|
2月前
|
Java Spring
[Spring]aop的配置与使用
本文介绍了AOP(面向切面编程)的基本概念和核心思想。AOP是Spring框架的核心功能之一,通过动态代理在不修改原代码的情况下注入新功能。文章详细解释了连接点、切入点、通知、切面等关键概念,并列举了前置通知、后置通知、最终通知、异常通知和环绕通知五种通知类型。
51 1