Proxool使用,通过DataSource,简单记录下,做个备份,参考了网上很多的资料,在此表示感谢。
proxool.xml
- <?xml version="1.0" encoding="gbk"?>
- <!--
- Document : proxool.xml
- Created on : 2008年12月2日, 下午1:09
- Author : zhappy
- Description:
- Purpose of the document follows.
- -->
- <something-else-entirely>
- <proxool>
- <alias>test</alias>
- <driver-url>jdbc:mysql://127.0.0.1:3306/happy</driver-url>
- <driver-class>com.mysql.jdbc.Driver</driver-class>
- <driver-properties>
- <property name="user" value="happy"/>
- <property name="password" value="happy"/>
- </driver-properties>
- <house-keeping-sleep-time>90000</house-keeping-sleep-time>
- <house-keeping-test-sql>select curdate()</house-keeping-test-sql>
- <simultaneous-build-throttle>150</simultaneous-build-throttle>
- <prototype-count>3</prototype-count>
- <maximum-connection-count>100</maximum-connection-count>
- <minimum-connection-count>3</minimum-connection-count>
- </proxool>
- </something-else-entirely>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/TestDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- <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/proxool.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet>
- <servlet-name>Admin</servlet-name>
- <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Admin</servlet-name>
- <url-pattern>/admin</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>
- 30
- </session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
context.xml(META-INF文件夹中,可以配置多应用)
- <?xml version="1.0" encoding="UTF-8"?>
- <Context antiJARLocking="true" path="/test">
- <!--Resource name="jdbc/TestDB" auth="Container"
- type="javax.sql.DataSource"
- maxActive="100" maxIdle="30" maxWait="10000"
- username="happy" password="happy"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/happy"/-->
- <Resource
- name="jdbc/TestDB"
- auth="Container"
- type="javax.sql.DataSource"
- factory="org.logicalcobwebs.proxool.ProxoolDataSource"
- proxool.alias="test"
- user="happy"
- password="happy"
- proxool.driver-url="jdbc:mysql://127.0.0.1:1521/happy"
- proxool.driver-class="com.mysql.jdbc.Driver"/>
- </Context>
Conn.java(通过数据源获取连接的类)
- package cn.happy;
- import java.sql.Connection;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.sql.DataSource;
- /**
- *
- * @author zhappy
- */
- public class Conn {
- public Conn() {
- }
- public static synchronized Connection get() throws Exception {
- Context ctx = new InitialContext();
- Context envctx = (Context) ctx.lookup("java:comp/env");
- DataSource ds = (DataSource) envctx.lookup("jdbc/TestDB");
- return ds.getConnection();
- }
- }
index.jsp
- <%@ page language="java" pageEncoding="GB2312"%>
- <%@ page import="java.sql.*" %>
- <%@ page import="javax.sql.*" %>
- <%@ page import="java.util.*" %>
- <%@ page import="java.io.*" %>
- <%@ page import="javax.naming.*" %>
- <%@ page import="cn.happy.*" %>
- <html>
- <head>
- <title>测试</title>
- </head>
- <body>
- <%
- Connection conn = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- conn = Conn.get();
- st = conn.createStatement();
- rs = st.executeQuery("select * from goodsmain");
- while (rs.next()) {
- out.print(rs.getString(3));%><br><%
- }
- st.close();
- rs.close();
- conn.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- %>
- </body>
- </html>
http://localhost:8080/test可以访问index.jsp
http://localhost:8080/test/admin可以访问连接池监控