在项目中使用proxool做数据源管理,这里记录下配置代码与需要注意的事项。
添加jar包:proxool-0.9.0RC3.jar 在WEB-INF下,创建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>数据源名</alias>
- <driver-url>jdbc:mysql://localhost:3306/数据库名</driver-url>
- <driver-class>com.mysql.jdbc.Driver</driver-class>
- <driver-properties>
- <property name="user" value="用户名"/>
- <property name="password" value="密码"/>
- <property name="useUnicode" value="true"/>
- <property name="characterEncoding" value="UTF-8"/>
- </driver-properties>
- <house-keeping-sleep-time>40000</house-keeping-sleep-time>
- <maximum-connection-count>250</maximum-connection-count>
- <minimum-connection-count>3</minimum-connection-count>
- <maximum-connection-lifetime>3000000</maximum-connection-lifetime> <!-- 5 hours -->
- <simultaneous-build-throttle>5</simultaneous-build-throttle>
- <recently-started-threshold>400000</recently-started-threshold>
- <overload-without-refusal-lifetime>500000</overload-without-refusal-lifetime>
- <maximum-active-time>600000</maximum-active-time>
- <verbose>true</verbose>
- <trace>true</trace>
- <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
- <fatal-sql-exception>Fatal error</fatal-sql-exception>
- <prototype-count>2</prototype-count>
- <statistics-log-level>INFO</statistics-log-level>
- </proxool>
- </something-else-entirely>
然后在web.xml文件中进行配置,添加如下代码:
- <servlet>
- <servlet-name>ServletConfigurator</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>
- <!--
- <servlet>
- <servlet-name>Admin</servlet-name>
- <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Admin</servlet-name>
- <url-pattern>/admin</url-pattern>
- </servlet-mapping>
- -->
上述代码中被注释掉的部分在开发过程中可以释放,如果释放,则会开启数据源监控页面,如果您的访问地址为,那么数据源监控URL为:。网站发布到服务器上后,建议关闭该功能。
注意:如果工程中存在监听器,而监听器中调用了数据库中的数据,那么在系统启动的时候会报错:.sql.SQLException: org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias ‘别名’或者java.sql.SQLException: No suitable driver found for '数据源名'这时的解决方案为把proxool交给监听器进行处理:- 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;
- 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....");
- }
- @SuppressWarnings("unchecked")
- public void contextInitialized(ServletContextEvent contextEvent) {
- System.out.println("init................");
- //对应servlet的init方法中ServletConfig.getServletContext()
- ServletContext context = contextEvent.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,因为此源码是0.9.1版本的,
- //与0.9RC3版本有点不一样
- properties.setProperty(name, value);
- }
- }
- if (properties.size() > 0) {
- try {
- PropertyConfigurator.configure(properties);
- } catch (ProxoolException e) {
- LOG.error("Problem configuring using init properties", e);
- }
- }
- }
- }
然后web.xml中的配置如下:
- <context-param>
- <param-name>xmlFile</param-name>
- <param-value>WEB-INF/proxool.xml</param-value>
- </context-param>
获取数据源连接代码:
- try{
- Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
- Connection con = DriverManager.getConnection("proxool.xml-test");
- // Connection con = DriverManager.getConnection("proxool.property-test");
- return con;
- }catch(Exception e){}
转自:51cto.comarticle-5496-1.html