• proxool的使用总结


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

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

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- the proxool configuration can be embedded within your own application's.
    Anything outside the "proxool" tag is ignored. -->
    <something-else-entirely>
        <proxool>
            <alias>数据源名</alias>
            <driver-url>jdbc:mysql://localhost:3306/数据库名</driver-url>
    	<driver-class>com.mysql.jdbc.Driver</driver-class>
            <driver-properties>
            <property name="user" value="用户名"/>
            <property name="password" value="密码"/>
            <property name="useUnicode" value="true"/>
            <property name="characterEncoding" value="UTF-8"/>
            </driver-properties>
    
            <house-keeping-sleep-time>40000</house-keeping-sleep-time>
            <maximum-connection-count>250</maximum-connection-count>
            <minimum-connection-count>3</minimum-connection-count>
            <maximum-connection-lifetime>3000000</maximum-connection-lifetime> <!-- 5 hours -->
            <simultaneous-build-throttle>5</simultaneous-build-throttle>
            <recently-started-threshold>400000</recently-started-threshold>
            <overload-without-refusal-lifetime>500000</overload-without-refusal-lifetime>
            <maximum-active-time>600000</maximum-active-time>
            <verbose>true</verbose>
            <trace>true</trace>
            <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
            <fatal-sql-exception>Fatal error</fatal-sql-exception>
            <prototype-count>2</prototype-count>
    	<statistics-log-level>INFO</statistics-log-level>
        </proxool>
    </something-else-entirely>
    

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

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

    <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>
    <servlet>
    	<servlet-name>Admin</servlet-name>
    	<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
    </servlet>-->
    <!-- <servlet-mapping>
    	<servlet-name>Admin</servlet-name>
    	<url-pattern>/admin</url-pattern>
    </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交给监听器进行处理:

    新建类:

    import java.io.File;
    import java.util.Enumeration;
    import java.util.Properties;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.logicalcobwebs.proxool.ProxoolException;
    import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
    import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
    
    /**
     * @author wangtao
     */
    
    public class ProxoolListener implements ServletContextListener {
    	private static final Log LOG = LogFactory.getLog(ProxoolListener.class);
    
    	private static final String XML_FILE_PROPERTY = "xmlFile";
    
    	private static final String PROPERTY_FILE_PROPERTY = "propertyFile";
    
    	private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
    
    	@SuppressWarnings("unused")
    	private boolean autoShutdown = true;
    
    	public void contextDestroyed(ServletContextEvent arg0) {
    		System.out.println("destroy database pool....");
    	}
    
    	@SuppressWarnings("unchecked")
    	public void contextInitialized(ServletContextEvent contextEvent) {
    		System.out.println("init................");
    		ServletContext context = contextEvent.getServletContext(); // 对应servlet的init方法中ServletConfig.getServletContext()
    		String appDir = contextEvent.getServletContext().getRealPath("/");
    		Properties properties = new Properties();
    
    		Enumeration names = context.getInitParameterNames();
    		while (names.hasMoreElements()) {
    			String name = (String) names.nextElement();
    			String value = context.getInitParameter(name);
    
    			if (name.equals(XML_FILE_PROPERTY)) {
    				try {
    					File file = new File(value);
    					if (file.isAbsolute()) {
    						JAXPConfigurator.configure(value, false);
    					} else {
    						JAXPConfigurator.configure(appDir + File.separator + value, false);
    					}
    				} catch (ProxoolException e) {
    					LOG.error("Problem configuring " + value, e);
    				}
    			} else if (name.equals(PROPERTY_FILE_PROPERTY)) {
    				try {
    					File file = new File(value);
    					if (file.isAbsolute()) {
    						PropertyConfigurator.configure(value);
    					} else {
    						PropertyConfigurator.configure(appDir + File.separator + value);
    					}
    				} catch (ProxoolException e) {
    					LOG.error("Problem configuring " + value, e);
    				}
    			} else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
    				autoShutdown = Boolean.valueOf(value).booleanValue();
    			} else if (name.startsWith("jdbc")) { // 此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样
    				properties.setProperty(name, value);
    			}
    		}
    
    		if (properties.size() > 0) {
    			try {
    				PropertyConfigurator.configure(properties);
    			} catch (ProxoolException e) {
    				LOG.error("Problem configuring using init properties", e);
    			}
    		}
    	}
    
    }
    

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

    <context-param>
    	<param-name>xmlFile</param-name>
    	<param-value>WEB-INF/proxool.xml</param-value>
    </context-param>
    

    获取数据源连接代码:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DBConnectionManager {
    	private static Connection conn;
    	public static Connection getConnection()
    	  {
    		try
    	    {
    	      conn = DriverManager.getConnection("proxool.数据源名");
    	      if (conn == null)
    	      {
    	        throw new SQLException("数据库无值");
    	      }
    
    	    }
    	    catch (Exception ex)
    	    {
    	      System.out.println(ex.getMessage());
    	      ex.printStackTrace();
    	    }
    
    	    return conn;
    	  }
    }
    

    参考资料:http://cbf0713.blog.163.com/blog/static/907789052010510105453412/

                  http://sunnylocus.javaeye.com/blog/359570

  • 相关阅读:
    美国航天局的十大编码戒律(转)
    大型数据库应用解决方案总结
    IOCP模型
    SSH 连接慢的解决方案详解
    指针(详解)【转】
    有关推挽输出、开漏输出、复用开漏输出、复用推挽输出以及上拉输入、下拉输入、浮空输入、模拟输入区别【转】
    USB入门开发的八个问题&USB枚举『转』
    浅谈 STM32 硬件I2C的使用 (中断方式 无DMA 无最高优先级)(转)
    KEIL Code RO-data RW-data ZI-data 【转】
    262K Color
  • 原文地址:https://www.cnblogs.com/saeba/p/1875851.html
Copyright © 2020-2023  润新知