天天看點

Spring中使用proxool連接配接池的配置方式與出現Attempt to refer to a unregistered pool by its alias 'ums'異常的解決辦法

                                                      Spring中使用proxool連接配接池的配置方式與出現

                  Attempt to refer to a  unregistered pool by its  alias 'ums'異常的解決辦法

             1.首先找到proxool連接配接池的兩個jar包 proxool-cglib.jar,proxool-0.9.1.jar

               我使用的spring是3.1.1版本的

               我這裡主要講的是通過自己的一個proxool.xml來将連接配接池的一些參數進行配置,

                也可以使用spring的配置檔案将一些參數進行配置,此時就不需要proxool.xml

                檔案了,這種方法我就不講解了,因為本人覺得全都配置在spring配置檔案中可能造成

                配置檔案比較臃腫

              2.将兩個jar拷貝到工程的lib目錄中,在src目錄下建立一個proxool.xml的xml檔案,

                 路徑和檔案名都可以随你的要求隻要在後面一些配置中到時候指定

                 你的檔案的路徑和檔案名稱,配置檔案内容如下:                

<?xml version="1.0" encoding="UTF-8"?>

<something-else-entirely>

 <proxool>

  <alias>LIBARARY</alias><!-- 與applicationContext.xml中url中proxool.*裡面的*一緻 -->  

  <driver-url>jdbc:mysql://localhost:3306/userinfo?characterEncoding=UTF-8&amp;useUnicode=true</driver-url>

  <driver-class>com.mysql.jdbc.Driver</driver-class>

  <driver-properties>

   <property name="user" value="root"/>

   <property name="password" value="sasa" />

  </driver-properties>

  <!-- 保留線程處于睡眠狀态的最長時間 -->

  <house-keeping-sleep-time>90000</house-keeping-sleep-time>

  <!-- 這是我們可一次建立的最大連接配接數,預設是10 -->

  <simultaneous-build-throttle>20</simultaneous-build-throttle>

  <!-- 連接配接池中可用的連接配接數量.如果目前的連接配接池中的連接配接少于這個數值.新的連接配接将被建立  -->

  <prototype-count>3</prototype-count>

  <maximum-connection-count>30</maximum-connection-count>

  <minimum-connection-count>3</minimum-connection-count>

 </proxool>

</something-else-entirely>

alias:資料源的别名 

driver-url:url連接配接串,須确定使用者名和密碼 

driver-class:驅動名 

username:使用者名(proxool沒有使用,但是不能沒有) 

password:密碼(proxool沒有使用,但是不能沒有) 

maximum-connection-count:最大連接配接數(預設5個),超過了這個連接配接數,再有請求時,就排在隊列中等候,最大的等待請求數由maximum-new-connections決定 

minimum-connection-count:最小連接配接數(預設2個) 

house-keeping-sleep-time:proxool自動偵察各個連接配接狀态的時間間隔(毫秒),偵察到空閑的連接配接就馬上回收,逾時的銷毀 預設30秒) 

prototype-count:最少保持的空閑連接配接數(預設2個) 

maximum-new-connections:沒有空閑連接配接可以配置設定而在隊列中等候的最大請求數,超過這個請求數的使用者連接配接就不會被接受 

           3.配置applicationContext.xml,整合Spring和proxool 

          Spring配置檔案applicationContext.xml中設定一個資料源(dataSource)的bean: 

           <!-- 使用proxool作為資料庫連接配接池 --> 

<bean id="dataSource" 

    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

   <property name="driverClassName"> 

    <value>org.logicalcobwebs.proxool.ProxoolDriver</value> 

   </property> 

   <property name="url"> 

    <value>proxool.dbpool</value> <!--要配置成proxool.xml中的别名-->

   </property> 

</bean> 

      4.配置web.xml 

        <servlet>

    <servlet-name>proxool</servlet-name>

    <servlet-class>

      org.logicalcobwebs.proxool.admin.servlet.AdminServlet

    </servlet-class>

    </servlet>

    <servlet-mapping>

    <servlet-name>proxool</servlet-name>

    <url-pattern>/proxool</url-pattern>

    </servlet-mapping>

    <!--proxool  -->

    <context-param>

  <param-name>xmlFile</param-name>

  <param-value>WEB-INF/classes/proxool.xml</param-value>

 </context-param>

     <listener>

  <!-- 一定要排在所有listener前面 -->

        <listener-class>com.test.listener.ProxoolListener</listener-class><!--這個是自定義的一個監聽器,後面會貼出代碼-->

    </listener>

    因為必須先初始化proxool.xml檔案。因為它是連接配接資料庫的,其他很多子產品都用到資料,是以你必須首先加載它,如果你先加載applicationContext.xml

    就會報Attempt to refer to a unregistered pool by its alias 'ums'。。。。。的錯誤

     本人看到網上很多都說這個問題需要将spring的監聽器

  <listener> 

    <listener-class> 

    org.springframework.web.context.ContextLoaderListener 

    </listener-class> 

    </listener> 

    換成如下配置: 

    <servlet> 

    <servlet-name>contextConfigLocation</servlet-name> 

    <servlet-class> 

    org.springframework.web.context.ContextLoaderServlet 

    </servlet-class> 

    <load-on-startup>2</load-on-startup> 

    </servlet> 

    要不然你就會遇見這樣的錯誤: 

    Problem 

    org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its 

    alias 'db' 

然後指定proxool配置檔案時再這樣配置: 

<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> 

    把<load-on-startup>的值設為1,值越小級别就越高,就先被加載初始化。一定要先于applicationContext.xml的加載。

 這個方法我也試過,但是還是報一樣的錯誤,我不知道是不是我的版本有誤還是什麼的,反正不行,是以自己寫了個監聽器來讓proxool.xml先于spring配置檔案之前初始化

自己寫的監聽器代碼如下

package com.founder.base.listener;

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.log4j.Logger;

import org.logicalcobwebs.proxool.ProxoolException;

import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

public class ProxoolListener implements ServletContextListener {

 private static final Logger logger = Logger.getLogger(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"; 

 private boolean autoShutdown = true;

 public void contextDestroyed(ServletContextEvent arg0) {

  System.out.println("destroy database pool....");

 }

 public void contextInitialized(ServletContextEvent contextEvent) {

  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) {

     logger.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) {

     logger.error("Problem configuring " + value, e);

    }

   } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {

    autoShutdown = Boolean.valueOf(value).booleanValue();

   } else if (name.startsWith("jdbc")) { 

    properties.setProperty(name, value);

   }

  }

  if (properties.size() > 0) {

   try {

    PropertyConfigurator.configure(properties);

   } catch (ProxoolException e) {

    logger.error("Problem configuring using init properties", e);

   }

  }

 }

}

繼續閱讀