etmvc中ActiveRecord(下称AR)在使用上可以独立使用,其数据库的连接信息通过activerecord.properties进行配置,具体如下:

domain_base_class=com.et.ar.ActiveRecordBase
com.et.ar.ActiveRecordBase.driver_class=com.mysql.jdbc.Driver
com.et.ar.ActiveRecordBase.url=jdbc:mysql://localhost/ciccpsmember
com.et.ar.ActiveRecordBase.username=root
com.et.ar.ActiveRecordBase.password=root
com.et.ar.ActiveRecordBase.pool_size=5

但是AR提供的这个简单的连接池(其实根本称不上连接池技术)性能实在不敢恭维,于是本人就琢磨这如何引入别的连接池?

首先,JAVAEE下数据库连接池有很多选择,c3p、dhcp、proxool等,druid作为一名后起之秀,凭借其出色的性能,也逐渐印入了大家的眼帘,于是本人将目标锁定了druid。

然后,连接池怎了和etmvc结合起来,第一个想法就是使用spring,查了一下资料,在不使用spring的情况下,类似于dbcp,druid也提供了原生态的支持,但是本人实在懒得折腾了,所以还是通过集成spring来使用连接池。具体步骤如下:

1、下载druid,地址:http://repo1.maven.org/maven2/com/alibaba/druid/,得到druid的jar包,我这里是druid-0.2.9.jar;

2、下载spring,现在的spring基本都是3.0之后的,但是我这用2.5的,就一个spring.jar;

3、将上面的两个包导入到WEB-INF下的lib;

4、修改WEB-INF下的Web.xml,注意过滤器由DispatcherFilter改成SpringDispatcherFilter,在追加springframework的listener,不使用spring时的Web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ciccpsMember</display-name>
  <filter>
  	<filter-name>etmvc</filter-name>
  	<filter-class>com.et.mvc.DispatcherFilter</filter-class>
  	<init-param>
  		<param-name>controllerBasePackage</param-name>
  		<param-value>controllers</param-value>
  	</init-param>
  	<init-param>
  		<param-name>viewBasePath</param-name>
  		<param-value>/views</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>etmvc</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

修改后如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <display-name>ciccpsMember</display-name>
  <filter>
  	<filter-name>etmvc</filter-name>
  	<filter-class>com.et.mvc.SpringDispatcherFilter</filter-class>
  	<init-param>
  		<param-name>controllerBasePackage</param-name>
  		<param-value>controllers</param-value>
  	</init-param>
  	<init-param>
  		<param-name>viewBasePath</param-name>
  		<param-value>/views</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>etmvc</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5、删掉src下的activerecord.properties,追加一个dbconfig.properties,主要用于保存数据库及连接池的配置信息,内容如下:

url:jdbc:mysql://localhost:3306/ciccpsmember
driverClassName:com.mysql.jdbc.Driver
username:root
password:root

filters:stat

maxActive:20
initialSize:1
maxWait:60000
minIdle:10
#maxIdle:15
 
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
 
validationQuery:SELECT 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
#poolPreparedStatements:true
maxOpenPreparedStatements:20

#\u5BF9\u4E8E\u957F\u65F6\u95F4\u4E0D\u4F7F\u7528\u7684\u8FDE\u63A5\u5F3A\u5236\u5173\u95ED
removeAbandoned:true
#\u8D85\u8FC730\u5206\u949F\u5F00\u59CB\u5173\u95ED\u7A7A\u95F2\u8FDE\u63A5
removeAbandonedTimeout:1800
#\u5C06\u5F53\u524D\u5173\u95ED\u52A8\u4F5C\u8BB0\u5F55\u5230\u65E5\u5FD7
logAbandoned:true

6、在WEB-INF下追加spring配置文件applicationContext.xml,dataSource就是连接池的配置,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       
                
   <bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>/WEB-INF/classes/dbconfig.properties</value>
			</list>
		</property>
	</bean>
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<property name="driverClassName" value="${driverClassName}" />
		<property name="filters" value="${filters}" />

		<property name="maxActive" value="${maxActive}" />
		<property name="initialSize" value="${initialSize}" />
		<property name="maxWait" value="${maxWait}" />
		<property name="minIdle" value="${minIdle}" />

		<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />

		<property name="validationQuery" value="${validationQuery}" />
		<property name="testWhileIdle" value="${testWhileIdle}" />
		<property name="testOnBorrow" value="${testOnBorrow}" />
		<property name="testOnReturn" value="${testOnReturn}" />
		<!-- <property name="maxOpenPreparedStatements"
			value="${maxOpenPreparedStatements}" /> -->
		<property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 -->
	    <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 -->
	    <property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 -->
	</bean>
    
	<bean class="com.et.ar.ConnectionFactoryBean"
          p:dataSource-ref="dataSource"
          p:adapterClass="com.et.ar.adapters.MySqlAdapter"
          p:domainBaseClass="com.et.ar.ActiveRecordBase" />
          
    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    
</beans>

7、这样就配置好了,AR该怎用还是怎用,主要的是性能也上去了。

druid教程参考地址:http://blog.csdn.net/yunnysunny/article/details/8657095


GitHub 加速计划 / druid / druid
27.83 K
8.56 K
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:3 个月前 )
f060c270 - 8 天前
1613a765 * Improve gaussdb ddl parser * fix temp table 10 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐