spring里加
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.web_db</value> </property> </bean>
在web.xml里加
<context-param> <param-name>xmlFile</param-name> <param-value>WEB-INF/proxool.xml</param-value> </context-param> <listener> <listener-class>cn.proxool.listener.ProxoolListener</listener-class> </listener> <servlet> <servlet-name>proxool_Admin</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>proxool_Admin</servlet-name> <url-pattern>/proxoolAdmin</url-pattern> </servlet-mapping>
注意:web.xml里加的内容一定要在
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
上面
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>web_db</alias> <driver-url>jdbc:microsoft:sqlserver://192.168.1.3:1433;DatabaseName=DB;SelectMethod=Cursor</driver-url> <driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class> <driver-properties> <property name="user" value="sa" /> <property name="password" value="sa" /> </driver-properties> <maximum-connection-count>50</maximum-connection-count> <house-keeping-test-sql>select 1</house-keeping-test-sql> </proxool> </something-else-entirely>
最后,加个文件
package cn.proxool.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.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....");
}
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) {
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);
}
}
}
}
http://www.iteye.com/topic/252675 有有关使struts2框架与spring时proxool要注意的问题