連接池未注冊org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias 'XXX'


情況一

代碼之前一直運行正常,寫了一個定時器后報錯,本地測試為了立馬能執行就用cron表達式* * * * * ?,為了只執行一次在最后面加上Thread.sleep(1000*3600*24)睡眠二十四小時從而達到每次測試只執行一次定時任務。

    @Scheduled(cron="* * * * * ?")
    public void execute() throws InterruptedException {
            System.out.println("調用時間"+CommonTool.getNowDateStr());
            insert();
            Thread.sleep(1000*3600*24);
    }

啟動報錯org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias 'XXX'

原因在於連接池沒注冊完成就調用了dao獲取連接導致報錯,但是只會在剛啟動時報一次。只需要在調用dao前Thread.sleep(10000)睡眠十秒,等到線程池注冊成功在進行dao操作。

情況二

在使用多數據源時,spring jdbctemplate + spring data jpa的情況下。為了防止事務有問題,proxool.xml下寫了兩個配置,一個給jdbc一個給jpa。當springdatajpa使用數據源時,總是會立馬獲取連接,原因不知道為什么。於是只能強制在加載org.springframework.web.context.ContextLoaderListener之前加載proxool。

原代碼

    <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/classes/jdbcproxool.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--spring的監聽器-->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

改為

    <!--proxool的ListenerConfigurator監聽器用的參數-->
    <context-param>
        <param-name>proxoolConfigLocation</param-name>
        <param-value>WEB-INF/classes/jdbcproxool.xml</param-value>
    </context-param>
    <!--proxool的監聽器-->
    <listener>
        <listener-class>org.logicalcobwebs.proxool.configuration.ListenerConfigurator</listener-class>
    </listener>

    <!--spring的監聽器-->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

ListenerConfigurator類需要自己新建

package org.logicalcobwebs.proxool.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;

import javax.servlet.ServletContextEvent;
import java.io.File;
import java.util.Properties;
/**
 * proxool初始化*/
public class ListenerConfigurator implements
        javax.servlet.ServletContextListener {

    private static final Log LOG = LogFactory
            .getLog(ListenerConfigurator.class);

    private static final String XML_FILE_PROPERTY = "proxoolConfigLocation";

    private boolean autoShutdown = true;

    public void contextInitialized(ServletContextEvent servletConfig) {

        String appDir = servletConfig.getServletContext().getRealPath("/");

        Properties properties = new Properties();
        String value = servletConfig.getServletContext().getInitParameter(
                XML_FILE_PROPERTY);
        LOG.debug("proxoolConfigLocation:"+value);
        
        try {
            File file = new File(value);
            if (file.isAbsolute()) {
                JAXPConfigurator.configure(value, false);
            } else {
                LOG.debug(appDir + File.separator + value);
                JAXPConfigurator.configure(appDir + File.separator + value,
                        false);
            }
        } catch (ProxoolException e) {
            LOG.error("Problem configuring " + value, e);
        }
        if (properties.size() > 0) {
            try {
                PropertyConfigurator.configure(properties);
            } catch (ProxoolException e) {
                LOG.error("Problem configuring using init properties", e);
            }
        }
    }

    public void contextDestroyed(ServletContextEvent s) {
        if (autoShutdown) {
            ProxoolFacade.shutdown(0);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM