关于数据库连接组件Proxool配置及使用(一)

来源:互联网 发布:淘宝店铺复核不通过 编辑:程序博客网 时间:2024/05/20 20:46


近期项目使用了proxool进行数据库连接,大大简化了Dao层的开发工作,将其记录自己的分析过程:

一、相关配置:

       在项目中导入proxool-0.9.1.jar、proxool-cglib.jar两个包,可以下载到相关组件且内部有相关配置及使用说明,大致有5中配置连接的方法,包括写属性文件,xml文件、还可以直接在连接处编写相关代码连接,下面只对项目中用到的写xml配置文件做简要分析:

写先关配置文件如:proxool.xml如下:

<?xml version="1.0" encoding="UTF-8"?><something-else-entirely>  <proxool><!-- alias 是连接的别名,在进行获取连接时用到-->  <alias>avso.mysql</alias>     <driver-url>jdbc:mysql://localhost:3306/youdb</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class>    <driver-properties>      <property name="user" value="root"/>      <property name="password" value="root"/>      <property name="useUnicode" value="true"/>      <property name="zeroDateTimeBehavior" value="convertToNull"/>  <property name="characterEncoding" value="iso-8859-1"/>     </driver-properties>    <test-before-use>true</test-before-use>    <minimum-connection-count>15</minimum-connection-count>    <maximum-connection-count>150</maximum-connection-count>    <maximum-active-time>1500000</maximum-active-time>    <maximum-connection-lifetime>50000000</maximum-connection-lifetime>    <house-keeping-sleep-time>60000</house-keeping-sleep-time>    <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>    <prototype-count>5</prototype-count>  </proxool>   <!--  <proxool>可以配置多个……   </proxool>--></something-else-entirely>


配置文件编写也了可以参考帮助文档说明。

下面就是在web.xml中配置该路径,只需声明一个servlet即可,如下

<servlet><servlet-name>proxoolServletConfigurator</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>


打开查看org.logicalcobwebs.proxool.configuration.ServletConfigurator.java源文件可以看到这个配置加载的过程:

public class ServletConfigurator extends HttpServlet  {    private static final Log LOG = LogFactory.getLog(ServletConfigurator.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";    private boolean autoShutdown = true;    public void init(ServletConfig servletConfig) throws ServletException {        super.init(servletConfig);        String appDir = servletConfig.getServletContext().getRealPath("/");        Properties properties = new Properties();        Enumeration names = servletConfig.getInitParameterNames();        while (names.hasMoreElements()) {            String name = (String) names.nextElement();            String value = servletConfig.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(PropertyConfigurator.PREFIX)) {                properties.setProperty(name, value);            }        }        if (properties.size() > 0) {            try {                PropertyConfigurator.configure(properties);            } catch (ProxoolException e) {                LOG.error("Problem configuring using init properties", e);            }        }    }    /**     * Shuts down Proxool by removing all connection pools. If you want     * to disable this behaviour then use:     * <pre>     * <init-param>     *   <param-name>autoShutdown</param-name>     *   <param-value>false</param-value>     * </init-param>     * </pre>     */    public void destroy() {        if (autoShutdown) {            ProxoolFacade.shutdown(0);        }    }}


我们使用的xml写配置,所以使用的就是JAXPConfigurator.configure()来处理,如果编写的配置文件时.properties形式的,采用处理是 PropertyConfigurator.configure()

进入JAXPConfigurator.configure()这个方法查看它对proxool.xml的解析和处理:SAX解析……

后面再应用层的数据库连接:

public static Connection getConnection(String driver) {Connection con = null;try {Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");con = DriverManager.getConnection(driver);} catch (SQLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return con;}


其中driver是我们配置文件中别名加proxool前缀proxool.avso.mysql

没有错误的话就会得到一个连接,当然,配置文件中可以配置相关很多数据库配置。