proxool的使用总结

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

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

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  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>数据源名</alias> 
  7.       <driver-url>jdbc:mysql://localhost:3306/数据库名</driver-url> 
  8.       <driver-class>com.mysql.jdbc.Driver</driver-class> 
  9.       <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.       <house-keeping-sleep-time>40000</house-keeping-sleep-time> 
  16.       <maximum-connection-count>250</maximum-connection-count> 
  17.       <minimum-connection-count>3</minimum-connection-count> 
  18.       <maximum-connection-lifetime>3000000</maximum-connection-lifetime> <!-- 5 hours --> 
  19.       <simultaneous-build-throttle>5</simultaneous-build-throttle> 
  20.       <recently-started-threshold>400000</recently-started-threshold> 
  21.       <overload-without-refusal-lifetime>500000</overload-without-refusal-lifetime> 
  22.       <maximum-active-time>600000</maximum-active-time> 
  23.       <verbose>true</verbose> 
  24.       <trace>true</trace> 
  25.       <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> 
  26.       <fatal-sql-exception>Fatal error</fatal-sql-exception> 
  27.        <prototype-count>2</prototype-count> 
  28.       <statistics-log-level>INFO</statistics-log-level> 
  29.   </proxool> 
  30. </something-else-entirely> 

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

  
  
  
  
  1. <servlet> 
  2.     <servlet-name>ServletConfigurator</servlet-name> 
  3.     <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class> 
  4.     <init-param> 
  5.         <param-name>xmlFile</param-name> 
  6.         <param-value>WEB-INF/proxool.xml</param-value> 
  7.     </init-param> 
  8.     <load-on-startup>1</load-on-startup> 
  9. </servlet> 
  10.  
  11. <!--
  12. <servlet> 
  13.     <servlet-name>Admin</servlet-name> 
  14.     <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> 
  15. </servlet> 
  16. <servlet-mapping> 
  17.     <servlet-name>Admin</servlet-name> 
  18.     <url-pattern>/admin</url-pattern> 
  19. </servlet-mapping>
  20. -->

上述代码中被注释掉的部分在开发过程中可以释放,如果释放,则会开启数据源监控页面,如果您的访问地址为www.***.com,那么数据源监控URL为:www.***.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交给监听器进行处理:

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

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

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

获取数据源连接代码:

  
  
  
  
  1. try
  2.  
  3. Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 
  4.  
  5. Connection con = DriverManager.getConnection("proxool.xml-test"); 
  6.  
  7. //  Connection con = DriverManager.getConnection("proxool.property-test"); 
  8.  
  9. return con; 
  10.  
  11. }catch(Exception e){} 

转自:51cto.comarticle-5496-1.html

你可能感兴趣的:(配置,proxool,使用)