Spring3使用proxool

Spring3中使用proxool,会出现无法加载proxool的情况。

由于web.xml文件中的加载顺序是:

(1)<context-param>

(2)<listener>

(3)<filter>

(4)<servlet>

因为spring3是采用<listener>进行启动(如下:)

<!-- spring资源文件位置 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:config/applicationContext-*.xml</param-value>
</context-param>

<!-- 启动spring -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

所以如果我们采用proxool提供的<servlet>进行加载的话,因为先启动了spring,造成proxool无法加载的问题。

 

分析以上原因,提出解决方案:

(1)向proxool靠拢:因为proxool只提供了<servlet>的加载方式,如果我们的proxool和spring都采用<servlet>进行加载,且proxool优先于spring加载,如下:

  <!-- 启动连接池 -->
  <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/proxool.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>
        org.springframework.web.context.ContextLoaderServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

但是。。。spring3不再提供<servlet>的加载方式(,可以自定义spring的<servlet>加载方式)。

(2)向spring靠拢:因为spring只提供了<listener>的加载方式,我们可以自定义listener,让proxool采用listener进行加载,且先于spring加载,如下:

<!-- 启动proxool -->
<context-param>
	<param-name>xmlFile</param-name>
	<param-value>WEB-INF/proxool.xml</param-value>
</context-param>

<listener>
	<listener-class>com.ys.listener.ProxoolListener</listener-class>
</listener>

<!-- spring资源文件位置 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:config/applicationContext-*.xml</param-value>
</context-param>

<!-- 启动spring -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


自定义ProxoolListener.java:

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;

 
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("destroy database pool....");
	}

	@Override
	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);
			}
		}
	
	}
}


问题解决。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: bonecp.driverClass=org.gjt.mm.mysql.Driver bonecp.jdbcUrl=jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8 bonecp.username=root bonecp.password=2008 #分区数量 bonecp.partitionCount = 1 #每个分区含有的最小连接数 bonecp.minConnectionsPerPartition = 1 #每个分区含有的最大连接数 bonecp.maxConnectionsPerPartition = 2 #每次新增连接的数量 bonecp.acquireIncrement = 1 #连接池阀值,当 可用连接/最大连接 < 连接阀值 时,创建新的连接 bonecp.poolAvailabilityThreshold = 20 #连接超时时间阀值,获取连接时,超出阀值时间,则获取失败,毫秒为单位 bonecp.connectionTimeout = 10000 #连接池助手线程数量,可设置为0,该参数会降低运行速度,但程序有大量连接时,有助于提升高并发程序的性能 bonecp.releaseHelperThreads = 0 #语句助手线程数,可设置为0,该参数会降低运行速度,但程序有大量的查询语句时,有助于提升高并发程序的性能 bonecp.statementReleaseHelperThreads = 0 #测试连接有效性的间隔时间,单位分钟 bonecp.idleConnectionTestPeriod = 60 #连接的空闲存活时间,当连接空闲时间大于该阀值时,清除该连接 bonecp.idleMaxAge = 240 #语句缓存个数,默认是0 bonecp.statementsCacheSize = 5 Spring中的配置信息 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <aop:config proxy-target-class="true"/> <tx:annotation-driven transaction-manager="txManager"/> <!-- 采用单数据源事务控制方式,通过注解来定义事务--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:proxool.properties</value> </property> </bean> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"> <property name="driver"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="driverUrl"> <value>jdbc:mysql://localhost/manytomany?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>2008</value> </property> <property name="alias"> <value>Db_name</value> </property> <property name="houseKeepingSleepTime"> <value>90000</value> </property> <property name="prototypeCount"> <value>50</value> </property> <property name="maximumConnectionCount"> <value>50</value> </property> <property name="minimumConnectionCount"> <value>2</value> </property> <property name="trace"> <value>true</value> </property> <property name="verbose"> <value>true</value> </property> </bean> </beans>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值