Proxool数据库连接池整合问题
近期在整合struts2、spring、hibernate,使用proxool配置数据库连接池总是报错,查找了下原因如下:
当Struts与Spring整合时,spring必须以listener加载,而proxool是使用Servlet加载的。
这样就导致spring早于proxool加载,spring在创建datasource时proxool连接池还没加载,从而报错。在网上有牛人用如下方法解决
解决方案一:自己写监听器早于Spring加载proxool
<context-param>
<param-name>xmlFileparam-name>
<param-value>WEB-INF/classes/proxool-conf.xmlparam-value>
context-param>
<listener>
<listener-class>sgwmis.web.listener.ProxoolListenerlistener-class>
listener>
public class ProxoolListener implements ServletContextListener {
private static final NetLogger log = NetLogger.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;
@Override
public void contextDestroyed(ServletContextEvent contextEvent) {
if (autoShutdown) {
ProxoolFacade.shutdown(0);
}
}
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
ServletContext context = contextEvent.getServletContext(); // 对应servlet的init方法中ServletConfig.getServletContext()
String appDir = contextEvent.getServletContext().getRealPath("/");
Properties properties = new Properties();
Enumeration
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
properties.setProperty(name, value);
}
}
if (properties.size() > 0) {
try {
PropertyConfigurator.configure(properties);
} catch (ProxoolException e) {
log.error("Problem configuring using init properties", e);
}
}
}
}
方法二:就是有spring完全管理proxool,这个网络上有很多,大家可以参考下