proxool使用(DataSource、MySQL)

本文档介绍了如何使用Proxool作为DataSource来连接MySQL数据库,包括proxool.xml配置文件和web.xml中设置的详细步骤,如数据库URL、驱动类、用户及密码等参数,并提到了ServletConfigurator和AdminServlet的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Proxool使用,通过DataSource,简单记录下,做个备份,参考了网上很多的资料,在此表示感谢。

 

proxool.xml

  1. <?xml version="1.0" encoding="gbk"?>
  2. <!--
  3.     Document   : proxool.xml
  4.     Created on : 2008年12月2日, 下午1:09
  5.     Author     : zhappy
  6.     Description:
  7.         Purpose of the document follows.
  8. -->
  9. <something-else-entirely>
  10.     <proxool>
  11.         <alias>test</alias>
  12.         <driver-url>jdbc:mysql://127.0.0.1:3306/happy</driver-url>
  13.         <driver-class>com.mysql.jdbc.Driver</driver-class>
  14.         <driver-properties>
  15.             <property name="user" value="happy"/>
  16.             <property name="password" value="happy"/>
  17.         </driver-properties>
  18.         <house-keeping-sleep-time>90000</house-keeping-sleep-time>
  19.         <house-keeping-test-sql>select curdate()</house-keeping-test-sql>
  20.         <simultaneous-build-throttle>150</simultaneous-build-throttle>
  21.         <prototype-count>3</prototype-count>
  22.         <maximum-connection-count>100</maximum-connection-count>
  23.         <minimum-connection-count>3</minimum-connection-count>
  24.     </proxool>
  25. </something-else-entirely>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3.     <resource-ref>
  4.         <description>DB Connection</description>
  5.         <res-ref-name>jdbc/TestDB</res-ref-name>
  6.         <res-type>javax.sql.DataSource</res-type>
  7.         <res-auth>Container</res-auth>
  8.     </resource-ref>
  9.     
  10.     <servlet>
  11.         <servlet-name>ServletConfigurator</servlet-name>
  12.         <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
  13.         <init-param>
  14.             <param-name>xmlFile</param-name>
  15.             <param-value>WEB-INF/proxool.xml</param-value>
  16.         </init-param>
  17.         <load-on-startup>1</load-on-startup>
  18.     </servlet>
  19.     <servlet>
  20.         <servlet-name>Admin</servlet-name>
  21.         <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
  22.     </servlet>
  23.     <servlet-mapping>
  24.         <servlet-name>Admin</servlet-name>
  25.         <url-pattern>/admin</url-pattern>
  26.     </servlet-mapping>
  27.     
  28.     <session-config>
  29.         <session-timeout>
  30.             30
  31.         </session-timeout>
  32.     </session-config>
  33.     
  34.     <welcome-file-list>
  35.         <welcome-file>index.jsp</welcome-file>
  36.     </welcome-file-list>
  37.     
  38. </web-app>

 

context.xml(META-INF文件夹中,可以配置多应用)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context antiJARLocking="true" path="/test">
  3.     <!--Resource name="jdbc/TestDB" auth="Container"
  4.               type="javax.sql.DataSource"
  5.               maxActive="100" maxIdle="30" maxWait="10000"
  6.               username="happy" password="happy" 
  7.               driverClassName="com.mysql.jdbc.Driver"
  8.               url="jdbc:mysql://localhost:3306/happy"/-->
  9.     <Resource
  10.         name="jdbc/TestDB"
  11.         auth="Container"
  12.         type="javax.sql.DataSource"
  13.         factory="org.logicalcobwebs.proxool.ProxoolDataSource"
  14.         proxool.alias="test"
  15.         user="happy"
  16.         password="happy"
  17.         proxool.driver-url="jdbc:mysql://127.0.0.1:1521/happy"
  18.         proxool.driver-class="com.mysql.jdbc.Driver"/>
  19. </Context>

Conn.java(通过数据源获取连接的类)

 

  1. package cn.happy;
  2. import java.sql.Connection;
  3. import javax.naming.Context;
  4. import javax.naming.InitialContext;
  5. import javax.sql.DataSource;
  6. /**
  7.  *
  8.  * @author zhappy
  9.  */
  10. public class Conn {
  11.     public Conn() {
  12.     }
  13.     public static synchronized Connection get() throws Exception {
  14.         Context ctx = new InitialContext();
  15.         Context envctx = (Context) ctx.lookup("java:comp/env");
  16.         DataSource ds = (DataSource) envctx.lookup("jdbc/TestDB");
  17.         return ds.getConnection();
  18.     }
  19. }

index.jsp

  1. <%@ page language="java" pageEncoding="GB2312"%>
  2. <%@ page import="java.sql.*" %>
  3. <%@ page import="javax.sql.*" %>
  4. <%@ page import="java.util.*" %>
  5. <%@ page import="java.io.*" %>
  6. <%@ page import="javax.naming.*" %>
  7. <%@ page import="cn.happy.*" %>
  8. <html>
  9.     <head>
  10.         <title>测试</title>
  11.     </head>
  12.     <body>
  13.         <%
  14.         Connection conn = null;
  15.         Statement st = null;
  16.         ResultSet rs = null;
  17.         try {
  18.             conn = Conn.get();
  19.             st = conn.createStatement();
  20.             rs = st.executeQuery("select * from goodsmain");
  21.             while (rs.next()) {
  22.                 out.print(rs.getString(3));%><br><%
  23.             }
  24.             st.close();
  25.             rs.close();
  26.             conn.close();
  27.         } catch (Exception e) {
  28.             e.printStackTrace();
  29.         }
  30.         %>
  31.     </body>
  32. </html>

http://localhost:8080/test可以访问index.jsp

http://localhost:8080/test/admin可以访问连接池监控

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值