Posted on 2006-06-28 09:51
多力宇扬 阅读(601)
评论(0) 编辑 收藏 所属分类:
View tier
Tomcat从5.x开始就可以在server.xml里配置数据源,后来用到了Proxool对比了一下觉得Proxool更方便更好用,它提供了更多的方法,简单,推荐大家使用.
下载地址:http://proxool.sourceforge.net/ 最新版本为: Proxool 0.9.0RC2
下载后解压缩Proxool 0.9.0RC2后,把Proxool 0.9.0RC2/lib下的.jar文件部署到WEB-INF/lib下.
看看下面的范例(example for Oracle):
Proxool.jsp
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Proxool.jsp</title>
</head>
<body>
<h2>使用Proxool.jsp</h2>
<%
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String ename="";
try
{
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
con=DriverManager.getConnection("proxool.JSPBook:oracle.jdbc.driver.OracleDriver:
jdbc:oracle:thin:@yang:1521:orcl","scott","ss");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query="select * from emp";
rs=stmt.executeQuery(query);
while(rs.next())
{
ename=rs.getString("ename");
%>
从emp表中取出姓名<%=ename%><br>
<%
}
stmt.close();
con.close();
}
catch(SQLException e)
{
out.println("发生异常"+e);
}
finally
{
try
{
if(con!=null)
{
con.close();
}
}
catch(SQLException ne)
{
out.println("SQLException:"+ne);
}
}
%>
</bdoy>
</html>
Proxool.jsp通过Proxool连接池取得Connection,然后显示emp表格中的ename.
首先动态加载Proxool的driver:
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver);
然后将Proxool URL分为三个部分: 连接池的别名,JDBC驱动程序(DataBase JDBC Drivers),连接URL.
设定Proxool
proxool提供了许多连接池的参数,例如:连接池最多有几个Connection,最少有几个Connection,Connection生命期限等等。
Proxool主要有以下四种设定方式:
(一) 通过java.util.Properties对象来设定,例如:
Properties info=new Properties();
info.setProperty("proxool.maximum-connection-count","20");
info.setProperty("proxool.house-keeping-test-sql","select CURRENT_DATE");
info.setProperty("user","your DB username");
info.setProperty("password","your DB prassword");
String alias="Develop"; //Proxool连接池的别名,根据自己喜好随便写^_^
String driverClass="oracle.jdbc.driver.OracleDriver";
String driverUrl="jdbc:oracle:thin:@127.0.0.1:1521:DatabaseName";
String url="proxool."+alias+":"+drvierClass+":"+driverUrl;
connection=DrvierManager.getConnection(rul,info);
(二) 通过XML文件来设定,例如:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--the proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is ignored.-->
<proxool>
<alias>Develop</alias>
<driver-url>
jdbc:oracle:thin:@127.0.0.1:1521:DatabaseName
</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="your database name"/>
<property name="password" value="your DB password"/>
</driver-properties>
<maximum-connection-count>10</maximum-connection-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
然后通过JAXPConfiguration读取XML文件:
JAXPConfigurator.configure("/WEB-INF/classes/proxool.xml",false);
(三) 先通过Properties文件来设定,例如:
jdbc-0.proxool.alias=Develop
jdbc-0.proxool.drvier-url=jdbc:oracle:thin:@127.0.0.1:1521:DatabaseName
jdbc-0.proxool.driver-class=oracle.jdbc.driver.OracleDriver;
jdbc-0.user=your database username
jdbc-0.password=your database password
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
然后通过PropertyConfigurator读取Properties文件:
PropertyConfigurator.configure("/WEB-INF/classes/Proxool.properties");
(四)在web.xml中,通过servlet来设定.方法又有三种,前两种是按照格式来的:
1.XML文件
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/classes/Proxool.xml</param-value>
</init-param>
<load-on-start>1</load-on-start>
</servlet>
2.Properties文件
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>propertyFile</param-name>
<param-value>WEB-INF/classes/Proxool.properties</param-value>
</init-param>
<load-on-start>1</load-on-start>
</servlet>
3.Init Prameter
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<init-name>jdbc-0.proxool.alias</init-param>
<init-value>Develop</init-value>
</init-param>
<init-param>
<init-name>jdbc-0.proxool.driver-rul</init-param>
<init-value>
jdbc:oracle:thin:@127.0.0.1:1521:DatabaseName
</init-value>
</init-param>
<init-param>
<init-name>jdbc-0.proxool.driver-class</init-param>
<init-value>oracle.jdbc.driver.OracleDriver</init-value>
<init-param>
</servlet>
建议大家使用第四种方法,好处在于当Container启动时,Proxool的参数会自动设定加载到内存中,原因在于:
<servlet>
......略
<load-on-start>1</load-on-start>
一个完整的示例如下:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
.......略
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>propertyFile</param-name>
<param-value>WEB-INF/classes/Proxool.properties</param-value>
</init-param>
<load-on-start>1</load-on-start>
</servlet>
.......略
</web-app>
Proxool.proterties
jdbc-0.proxool.alias=Develop
jdbc-0.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc-0.proxool.driver.url=jdbc:oracle:thin:@127.0.0.1:1521:DatabaseName
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.prototype-count=4
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc=0.proxool.verbose=true
我们可以用下面的Test Page来进行测试
Proxool-config.jsp
____________________________________________________
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Proxool.jsp</title>
</head>
<body>
<h2>使用Proxool.jsp</h2>
<%
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String ename="";
try
{
con=DriverManager.getConnection("proxool.Develop");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query="select * from emp";
rs=stmt.executeQuery(query);
while(rs.next())
{
ename=rs.getString("ename");
%>
从emp表中取出姓名<%=ename%><br>
<%
}
stmt.close();
con.close();
}
catch(SQLException e)
{
out.println("发生异常"+e);
}
finally
{
try
{
if(con!=null)
{
con.close();
}
}
catch(SQLException ne)
{
out.println("SQLException:"+ne);
}
}
%>
</bdoy>
</html>
测试结果为:
=====================================
使用Proxool.jsp
从emp表中取出姓名SMITH
从emp表中取出姓名ALLEN
从emp表中取出姓名WARD
从emp表中取出姓名JONES
从emp表中取出姓名MARTIN
从emp表中取出姓名BLAKE
从emp表中取出姓名CLARK
从emp表中取出姓名SCOTT
从emp表中取出姓名KING
从emp表中取出姓名TURNER
从emp表中取出姓名ADAMS
从emp表中取出姓名JAMES
从emp表中取出姓名FORD
从emp表中取出姓名MILLER
从emp表中取出姓名feiyang
从emp表中取出姓名yang
从emp表中取出姓名feifei
从emp表中取出姓名fei