Proxool连接池配置
Posted Talk is cheap. Show me the cod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Proxool连接池配置相关的知识,希望对你有一定的参考价值。
1、将数据库链接使用的JDBC库文件和连接池使用的proxool.jar库文件放入项目中
2、配置proxool.xml文件,新建一个proxool.xml文件放在项目的WEB-INF目录下
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <!-- the proxool configuration can be embedded within your own application‘s. 3 Anything outside the "proxool" tag is ignored. --> 4 <something-else-entirely> 5 <proxool> 6 <alias>public</alias> 7 <driver-url>jdbc:oracle:thin:@192.168.9.73:1521:oss</driver-url> 8 <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> 9 <driver-properties> 10 <property name="user" value="upm"/> 11 <property name="password" value="upm"/> 12 </driver-properties> 13 <maximum-connection-lifetime>600000</maximum-connection-lifetime> 14 <maximum-connection-count>5</maximum-connection-count> 15 <house-keeping-test-sql>select CURRENT_DATE FROM dual</house-keeping-test-sql> 16 </proxool> 17 </something-else-entirely>
3、配置web.xml
1 <! -- 配置初始化servlet,在web容器加载的时候自动加载配置文件 --> 2 <servlet> 3 <servlet-name>proxoolServletConfigurator</servlet-name> 4 <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> 5 <init-param> 6 <param-name>xmlFile</param-name> 7 <param-value>WEB-INF/proxool.xml</param-value> 8 </init-param> 9 <load-on-startup>1</load-on-startup> 10 </servlet> 11 <!--配置监控servlet--> 12 <servlet> 13 <servlet-name>dbpoolAdmin</servlet-name> 14 <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>dbpoolAdmin</servlet-name> 18 <url-pattern>/dbpool</url-pattern> 19 </servlet-mapping>
4、使用数据库连接池
1 package com.richinfo.dao.base; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import org.apache.logging.log4j.LogManager; 10 import org.apache.logging.log4j.Logger; 11 12 public class DBManager { 13 private static Logger log = LogManager.getLogger(DBManager.class); 14 15 /** 16 * 默认的数据库链接,链接Oss139 统一位置 管理下的对象 17 * 18 * @return Connection 19 */ 20 public static Connection getConnection() { 21 22 try { 23 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 24 } catch (ClassNotFoundException e) { 25 e.printStackTrace(); 26 return null; 27 } catch (Exception e) { 28 e.printStackTrace(); 29 return null; 30 } 31 Connection conn = null; 32 try { 33 conn = DriverManager.getConnection("proxool.public"); 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 log.error("DBManager getConnection null.{}", e); 37 return null; 38 } 39 return conn; 40 } 41 42 /** 43 * 链接指定的 数据库对象 44 * 45 * @param dataBaseName 46 * 对象名 47 * @return Connection 48 */ 49 public static Connection getConnection(String dataBaseName) { 50 51 try { 52 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 53 } catch (ClassNotFoundException e) { 54 e.printStackTrace(); 55 return null; 56 } catch (Exception e) { 57 e.printStackTrace(); 58 return null; 59 } 60 Connection conn = null; 61 try { 62 conn = DriverManager.getConnection("proxool." + dataBaseName); 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 return null; 66 } 67 return conn; 68 } 69 70 public static void close(Connection conn, Statement st, ResultSet rs) { 71 72 try { 73 if (rs != null) 74 rs.close(); 75 } catch (Exception e) { 76 log.error("DBManager close fail rs. {}", e); 77 } 78 ; 79 try { 80 if (st != null) 81 st.close(); 82 } catch (Exception e) { 83 log.error("DBManager close fail st. {}", e); 84 } 85 ; 86 try { 87 if (conn != null) 88 conn.close(); 89 } catch (Exception e) { 90 log.error("DBManager close fail conn. {}", e); 91 } 92 } 93 }
以上是关于Proxool连接池配置的主要内容,如果未能解决你的问题,请参考以下文章