proxool的使用总结

在项目中使用proxool做数据源管理,这里记录下配置代码与需要注意的事项。

添加jar包:proxool-0.9.0RC3.jar 在WEB-INF下,创建proxool.xml文件,代码如下:

 

01 <?xml version="1.0" encoding="UTF-8"?>
02 <!-- the proxool configuration can be embedded within your own application's.
03 Anything outside the "proxool" tag is ignored. -->
04 <something-else-entirely>
05     <proxool>
06         <alias>数据源名</alias>
07         <driver-url>jdbc:mysql://localhost:3306/数据库名</driver-url>
08     <driver-class>com.mysql.jdbc.Driver</driver-class>
09         <driver-properties>
10         <property name="user" value="用户名"/>
11         <property name="password" value="密码"/>
12         <property name="useUnicode" value="true"/>
13         <property name="characterEncoding" value="UTF-8"/>
14         </driver-properties>
15  
16         <house-keeping-sleep-time>40000</house-keeping-sleep-time>
17         <maximum-connection-count>250</maximum-connection-count>
18         <minimum-connection-count>3</minimum-connection-count>
19         <maximum-connection-lifetime>3000000</maximum-connection-lifetime> <!-- 5 hours -->
20         <simultaneous-build-throttle>5</simultaneous-build-throttle>
21         <recently-started-threshold>400000</recently-started-threshold>
22         <overload-without-refusal-lifetime>500000</overload-without-refusal-lifetime>
23         <maximum-active-time>600000</maximum-active-time>
24         <verbose>true</verbose>
25         <trace>true</trace>
26         <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
27         <fatal-sql-exception>Fatal error</fatal-sql-exception>
28         <prototype-count>2</prototype-count>
29     <statistics-log-level>INFO</statistics-log-level>
30     </proxool>
31 </something-else-entirely>

 

:proxool.xml的配置参数详细说明请参考这里。

然后在web.xml文件中进行配置,添加如下代码:

 

01 <servlet>
02     <servlet-name>ServletConfigurator</servlet-name>
03     <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
04     <init-param>
05         <param-name>xmlFile</param-name>
06         <param-value>WEB-INF/proxool.xml</param-value>
07     </init-param>
08     <load-on-startup>1</load-on-startup>
09 </servlet>
10 <servlet>
11     <servlet-name>Admin</servlet-name>
12     <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
13 </servlet>-->
14 <!-- <servlet-mapping>
15     <servlet-name>Admin</servlet-name>
16     <url-pattern>/admin</url-pattern>
17 </servlet-mapping> -->

 

上述代码中被注释掉的部分在开发过程中可以释放,如果释放,则会开启数据源监控页面,如果您的访问地址为www.xxx.com,那么数据源监控URL为:www.xxx.com/admin。网站发布到服务器上后,建议关闭该功能。

 

注意:如果工程中存在监听器,而监听器中调用了数据库中的数据,那么在系统启动的时候会报错

java.sql.SQLException: org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias ‘别名’

或者

java.sql.SQLException: No suitable driver found for '数据源名'

 这时的解决方案为:把proxool交给监听器进行处理:

新建类:

 

01 import java.io.File;
02 import java.util.Enumeration;
03 import java.util.Properties;
04  
05 import javax.servlet.ServletContext;
06 import javax.servlet.ServletContextEvent;
07 import javax.servlet.ServletContextListener;
08  
09 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.logicalcobwebs.proxool.ProxoolException;
12 import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
13 import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
14  
15 /**
16  * @author wangtao
17  */
18  
19 public class ProxoolListener implements ServletContextListener {
20     private static final Log LOG = LogFactory.getLog(ProxoolListener.class);
21  
22     private static final String XML_FILE_PROPERTY = "xmlFile";
23  
24     private static final String PROPERTY_FILE_PROPERTY = "propertyFile";
25  
26     private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
27  
28     @SuppressWarnings("unused")
29     private boolean autoShutdown = true;
30  
31     public void contextDestroyed(ServletContextEvent arg0) {
32         System.out.println("destroy database pool....");
33     }
34  
35     @SuppressWarnings("unchecked")
36     public void contextInitialized(ServletContextEvent contextEvent) {
37         System.out.println("init................");
38         ServletContext context = contextEvent.getServletContext(); // 对应servlet的init方法中ServletConfig.getServletContext()
39         String appDir = contextEvent.getServletContext().getRealPath("/");
40         Properties properties = new Properties();
41  
42         Enumeration names = context.getInitParameterNames();
43         while (names.hasMoreElements()) {
44             String name = (String) names.nextElement();
45             String value = context.getInitParameter(name);
46  
47             if (name.equals(XML_FILE_PROPERTY)) {
48                 try {
49                     File file = new File(value);
50                     if (file.isAbsolute()) {
51                         JAXPConfigurator.configure(value, false);
52                     } else {
53                         JAXPConfigurator.configure(appDir + File.separator + value, false);
54                     }
55                 } catch (ProxoolException e) {
56                     LOG.error("Problem configuring " + value, e);
57                 }
58             } else if (name.equals(PROPERTY_FILE_PROPERTY)) {
59                 try {
60                     File file = new File(value);
61                     if (file.isAbsolute()) {
62                         PropertyConfigurator.configure(value);
63                     } else {
64                         PropertyConfigurator.configure(appDir + File.separator + value);
65                     }
66                 } catch (ProxoolException e) {
67                     LOG.error("Problem configuring " + value, e);
68                 }
69             } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
70                 autoShutdown = Boolean.valueOf(value).booleanValue();
71             } else if (name.startsWith("jdbc")) { // 此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样
72                 properties.setProperty(name, value);
73             }
74         }
75  
76         if (properties.size() > 0) {
77             try {
78                 PropertyConfigurator.configure(properties);
79             } catch (ProxoolException e) {
80                 LOG.error("Problem configuring using init properties", e);
81             }
82         }
83     }
84  
85 }

 

然后web.xml中的配置如下:

 

1 <context-param>
2     <param-name>xmlFile</param-name>
3     <param-value>WEB-INF/proxool.xml</param-value>
4 </context-param>

 

 

获取数据源连接代码:

 

view source print ?
01 import java.sql.Connection;
02 import java.sql.DriverManager;
03 import java.sql.SQLException;
04  
05 public class DBConnectionManager {
06     private static Connection conn;
07     public static Connection getConnection()
08       {
09         try
10         {
11           conn = DriverManager.getConnection("proxool.数据源名");
12           if (conn == null)
13           {
14             throw new SQLException("数据库无值");
15           }
16  
17         }
18         catch (Exception ex)
19         {
20           System.out.println(ex.getMessage());
21           ex.printStackTrace();
22         }
23  
24         return conn;
25       }
26 }

你可能感兴趣的:(职场,proxool,休闲)