Java应用中使用Proxool

Java应用中使用Proxool
 
本文题目没有强调的是Java应用,不是Java Web应用,原因是从网上找有关Proxool的例子,全是一个摸样,都是将讲解Java Web中的应用,真没劲,难道Proxool离开了web就不能用了?不信你可以google一把看看。
 
在阅读了Proxool的官方指南后,我写了个简单测试,扔出来,希望能对大家有用。
 
环境:
JDK 1.5
proxool-0.9.1.zip
Oracle 10g
 
http://ncu.dl.sourceforge.net/project/proxool/proxool/0.9.1/proxool-0.9.1.zip
 
Proxool准确说还算不上一个连接池,因为里面没有一个获取DataSource对象的方法,也没有一个获取“**Pool”的途径。因此要使用Proxool连接池,有两条路可走,一是自己实现一个连接池的管理工具,二是通过开源的框架来使用,因为好多开源框架实现了池管理功能,比如Spring、Hibernate、iBatis等等。
 
Proxool的性能据说还不错,常常与Spring、Hibernate等整合使用,倒很方便。
 
这里我不打算实现什么连接池管理工具,也不使用开源框架,而是仅仅利用Proxool提供的功能来做一个数据库操作。
 
1、写Proxool的配置
Proxool提供了一个接口,用来读取xml格式或properties格式的配置信息,用来初始化Proxool所需的参数。这里我用xml配置,当然这个XML是网上找的,我懒得写,仅仅改吧改吧能用。
 
xml version ="1.0" encoding ="UTF-8" ?>
< something-else-entirely >
         < proxool >
                 < alias >timalias alias >
                
                 < driver-url >jdbc:oracle:thin:@192.168.104.192:1521:tim driver-url >
                
                 < driver-class >oracle.jdbc.driver.OracleDriver driver-class >
                
                 < driver-properties >
                         < property name ="user" value ="tim" />
                        
                         < property name ="password" value ="tim_8968888" />
                        
                 driver-properties >
                
                 < maximum-connection-count >100 maximum-connection-count >
                
                 < minimum-connection-count >10 minimum-connection-count >
                
                 < house-keeping-sleep-time >90000 house-keeping-sleep-time >
                
                 < maximum-new-connections >10 maximum-new-connections >
                
                 < prototype-count >5 prototype-count >
                
                 < test-before-use >true test-before-use >
                
                 < house-keeping-test-sql >select sysdate from dual house-keeping-test-sql >
         proxool >
something-else-entirely >
 
2、写测试类
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

import java.sql.Connection;
import java.sql.DriverManager;
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);
                 //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();
        }

         /**
         * proxool方式测试
         *
         * @throws Exception
         */

         public static void test2() throws Exception {
                 //Java应用中先要加载配置文件,否则谁知道你配置给谁用的
                JAXPConfigurator.configure( "F:\\_test\\synorg\\src\\proxool.xml", false);
                String testsql = "select * from village t where lastid = 346";
                 //1:注册驱动类,这次这个驱动已经不是Oracle的驱动了,是Proxool专用的驱动
                Class.forName( "org.logicalcobwebs.proxool.ProxoolDriver");
                 //2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名
                Connection conn = DriverManager.getConnection( "proxool.timalias");
                 //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 {
                test2();
        }
}
 
运行结果:
2009-10-14 18:13:05    - INFO    org.logicalcobwebs.proxool.ProxoolFacade         - Proxool 0.9.1 (23-Aug-2008 11:10)
2009-10-14 18:13:05    - WARN    org.logicalcobwebs.proxool.timalias         - Use of proxool.maximum- new-connections is deprecated. Use more descriptive proxool.simultaneous-build-throttle instead.
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-14 18:13:06    - INFO    org.logicalcobwebs.proxool.timalias         - Shutting down 'timalias' pool immediately [Shutdown Hook]
2009-10-14 18:13:07    - INFO    org.logicalcobwebs.proxool.PrototyperController         - Stopping Prototyper thread
2009-10-14 18:13:07    - INFO    org.logicalcobwebs.proxool.HouseKeeperController         - Stopping HouseKeeper thread

Process finished with exit code 0
 
因为使用了log4j,这个结果里面输出了一些日志信息。
 
3、说明
 
Proxool的配置文件路径不能使用ClassPath,我查看过接口代码,可以传入一个XmlReader、或流来解决,这里写成绝对路径就凑合着能跑起来就行。
 
也支持Properties的配置,则个可以参考官方文档,也很方便的。
 
有关配置参数,网上的文档一堆一堆的重复,大家随便找篇就行。
http://wallimn.javaeye.com/blog/486550
 
想看Web应用的,参看:
http://blog.csdn.net/goodhumor/archive/2008/03/04/2144911.aspx

你可能感兴趣的:(数据库,java,开发工具)