Java数据库连接池之proxool_动力节点Java学院整理
程序员文章站
2024-02-17 08:44:22
proxool是一种java数据库连接池技术。sourceforge下的一个开源项目,这个项目提供一个健壮、易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便...
proxool是一种java数据库连接池技术。sourceforge下的一个开源项目,这个项目提供一个健壮、易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便于发现连接泄漏的情况。
目前是和dbcp以及c3p0一起,最为常见的三种jdbc连接池技术。
日前,hibernate官方宣布由于bug太多不再支持dbcp,而推荐使用 proxool或c3p0。
下面通过一个demo说明一下如何使用:
项目结构如下:
dblink.java文件中的代码:
package com.bjpowernode.util; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.sqlexception; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; public class dblink { private connection connection; private preparedstatement preparedstatement; private resultset resultset; static{ try { class.forname("org.logicalcobwebs.proxool.proxooldriver"); //加载驱动 }catch (classnotfoundexception e) { system.out.println("加载驱动失败!"); e.printstacktrace(); } } private connection getconn(){ try { if(connection==null||connection.isclosed()){ connection = drivermanager.getconnection("proxool.mysqlsource"); //获取连接 } } catch (sqlexception e) { system.out.println("获取连接失败!"); e.printstacktrace(); } return connection; } public list<map<string, object>> select(string sql, object [] params){ list<map<string, object>> list = new arraylist<map<string, object>>(); try{ connection = this.getconn(); preparedstatement = connection.preparestatement(sql); if(params != null){ for(int i =0; i<params.length; i++){ preparedstatement.setobject(i+1, params[i]);//逐一给问号赋值 } } resultset = preparedstatement.executequery(); resultsetmetadata rsmd = resultset.getmetadata(); int columncount = rsmd.getcolumncount(); while(resultset.next()){ map<string, object> map = new hashmap<string, object>(); for(int i =1; i<=columncount;i++){ string columnname = rsmd.getcolumnname(i); object obj = null; if(rsmd.getcolumntypename(i).equals("date")){ obj = resultset.getstring(columnname); }else{ obj = resultset.getobject(columnname); } map.put(columnname, obj); } list.add(map); } }catch(sqlexception e){ e.printstacktrace(); }finally{ close(); } return list; } private void close(){ try { if(resultset != null){ resultset.close(); } } catch (sqlexception e) { e.printstacktrace(); } try { if(preparedstatement != null){ preparedstatement.close(); } } catch (sqlexception e) { e.printstacktrace(); } try { connection.close(); } catch (sqlexception e) { e.printstacktrace(); } } }
proxool.xml文件中的代码:
<?xml version="1.0" encoding="utf-8"?> <proxool-config> <proxool> <alias>mysqlsource</alias> <driver-url>jdbc:mysql://localhost:3306/test</driver-url> <driver-class>com.mysql.jdbc.driver</driver-class> <driver-properties> <property name="user" value="root" /> <property name="password" value="root" /> </driver-properties> <!-- 保留线程处于睡眠状态的最长时间 --> <house-keeping-sleep-time>60000</house-keeping-sleep-time> <maximum-connection-count>30</maximum-connection-count> <minimum-connection-count>10</minimum-connection-count> <!-- 连接池中可用的连接数量(空闲状态) --> <prototype-count>5</prototype-count> <maximum-new-connections>1</maximum-new-connections> <simultaneous-build-throttle>1</simultaneous-build-throttle> <!-- 连接池使用状态 --> <statistics>30s,10m,1d</statistics> <!-- 连接池日志类型 --> <statistics-log-level>error</statistics-log-level> <!-- 如果为true,那么每个被执行的sql语句将会在执行期被log记录(debug level) --> <trace>true</trace> <house-keeping-test-sql>select current_date</house-keeping-test-sql> </proxool> </proxool-config>
web.xml文件中的代码:
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" 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_3_0.xsd"> <!-- 连接池 --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp文件中的代码:
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@page import="com.bjpowernode.util.dblink,java.util.list,java.util.map"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>首页</title> </head> <body> <table width="100%" border="1px"> <tr> <th>序号 </th><th>班级名称</th><th>开班日期</th><th>结束日期</th><th>状态</th><th>操作</th> </tr> <% string sql = "select id, class_name, begin_date, end_date, state from class "; dblink db = new dblink(); list<map<string, object>> list = db.select(sql, null); int index = 0; for (map<string, object> map : list) { index = index + 1; int id = integer.parseint(map.get("id").tostring()); string classname = map.get("class_name").tostring(); string begindate = map.get("begin_date").tostring(); string enddate = map.get("end_date").tostring(); int state = integer.parseint(map.get("state").tostring()); string statename = (state == 0 ? "未启用" : "启用"); %> <tr align="center"> <td><%=index%></td><td><%=classname%></td><td><%=begindate%></td><td><%=enddate%></td><td><%=statename%></td><td>【<a href="###" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="">修改</a>】【<a href="###" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="">查看</a>】【<a href="###" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="">删除</a>】</td> </tr> <% } %> </table> </body> </html>
总结
以上所述是小编给大家介绍的java数据库连接池之proxool,希望对大家有所帮助
上一篇: mysql 5.6.17 绿色版(免安装)安装配置教程
下一篇: 去掉广告的代码
推荐阅读
-
Java数据库连接池之proxool_动力节点Java学院整理
-
Java数据库连接池之DBCP浅析_动力节点Java学院整理
-
Java动态代理实现_动力节点Java学院整理
-
Java字符编码简介_动力节点Java学院整理
-
Java分页简介_动力节点Java学院整理
-
Java中使用jaxp进行sax解析_动力节点Java学院整理
-
PipedWriter和PipedReader源码分析_动力节点Java学院整理
-
RandomAccessFile简介_动力节点Java学院整理
-
Java 中的Printstream介绍_动力节点Java学院整理
-
简述Java中进程与线程的关系_动力节点Java学院整理