struts+spring+hibernate+proxool配置

转自 http://wt8414.iteye.com/blog/242628 收藏参考

基于ssh的proxool连接池配置
环境:MyEclipse5.5,win 2003,oracle,mysql
配置文件1:applicationContext.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
    <bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 数据源类采用JDBC数据源 --> 
        <property name="driverClassName"> 
        <!-- 将数据库连接交给了proxool管理,使它的驱动 --> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> 
        </property> 
        <property name="url"> 
                           <!-- 数据库连接池的别名,与你的proxool.xml中的Alias必须一致 --> 
            <value>proxool.DbPool</value> 
        </property> 
    </bean> 
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource"> 
            <ref bean="dataSource" /> 
        </property> 
        <property name="hibernateProperties"> 
            <props> 
                <prop key="hibernate.dialect"> 
                    org.hibernate.dialect.MySQLDialect  
                </prop> 
                <prop key="hibernate.connection.autocommit">true</prop> 
                <prop key="hibernate.show_sql">true</prop> 
                                    <!--  
此处要注意因为proxool自己释放数据库连接比慢,所以要在此给出释放连接的模式,具体几种模式对应的意思,可以Google一下hibernate.connection.release_mode,有很多说明,在此不多说 
--> 
                <prop key="hibernate.connection.release_mode"> 
                    after_statement  
                </prop> 
            </props> 
        </property> 
        <property name="mappingResources"> 
            <list> 
                <value>cn/wt/vo/Admin.hbm.xml</value> 
                <value>cn/wt/vo/User.hbm.xml</value> 
                <value>cn/wt/vo/Workdate.hbm.xml</value> 
            </list> 
        </property> 
    </bean> 
    <bean id="hibernateTemplate" 
        class="org.springframework.orm.hibernate3.HibernateTemplate"> 
        <property name="sessionFactory"> 
            <ref bean="sessionFactory" /> 
        </property> 
    </bean> 
    <bean id="adminDao" class="cn.wt.dao.AdminDao" abstract="true" /> 
    <bean id="adminDaoImpl" class="cn.wt.dao.impl.AdminDaoImpl" 
        parent="adminDao"> 
        <property name="hibernateTemplate"> 
            <ref bean="hibernateTemplate" /> 
        </property> 
    </bean> 
    <bean id="workDateDao" class="cn.wt.dao.WorkDateDao" 
        abstract="true" /> 
    <bean id="workDateDaoImpl" class="cn.wt.dao.impl.WorkDateDaoImpl" 
        parent="workDateDao"> 
        <property name="hibernateTemplate"> 
            <ref bean="hibernateTemplate" /> 
        </property> 
    </bean> 
    <bean id="userDao" class="cn.wt.dao.UserDao" abstract="true" /> 
    <bean id="userDaoImpl" class="cn.wt.dao.impl.UserDaoImpl" parent="userDao"> 
        <property name="hibernateTemplate"> 
            <ref bean="hibernateTemplate"/> 
        </property> 
    </bean> 
    <bean name="/adminAction" class="cn.wt.struts.action.AdminAction"> 
        <property name="adminDao"> 
            <ref bean="adminDaoImpl" /> 
        </property> 
    </bean> 
    <bean name="/workdateAction" 
        class="cn.wt.struts.action.WorkdateAction"> 
        <property name="workDateDao"> 
            <ref bean="workDateDaoImpl" /> 
        </property> 
        <property name="userDao"> 
            <ref bean="userDaoImpl"/> 
        </property> 
    </bean> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 数据源类采用JDBC数据源 -->
<property name="driverClassName">
<!-- 将数据库连接交给了proxool管理,使它的驱动 --> <value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
                           <!-- 数据库连接池的别名,与你的proxool.xml中的Alias必须一致 -->
<value>proxool.DbPool</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
                                    <!--
此处要注意因为proxool自己释放数据库连接比慢,所以要在此给出释放连接的模式,具体几种模式对应的意思,可以Google一下hibernate.connection.release_mode,有很多说明,在此不多说
-->
<prop key="hibernate.connection.release_mode">
after_statement
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/wt/vo/Admin.hbm.xml</value>
<value>cn/wt/vo/User.hbm.xml</value>
<value>cn/wt/vo/Workdate.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="adminDao" class="cn.wt.dao.AdminDao" abstract="true" />
<bean id="adminDaoImpl" class="cn.wt.dao.impl.AdminDaoImpl"
parent="adminDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<bean id="workDateDao" class="cn.wt.dao.WorkDateDao"
abstract="true" />
<bean id="workDateDaoImpl" class="cn.wt.dao.impl.WorkDateDaoImpl"
parent="workDateDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<bean id="userDao" class="cn.wt.dao.UserDao" abstract="true" />
<bean id="userDaoImpl" class="cn.wt.dao.impl.UserDaoImpl" parent="userDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean name="/adminAction" class="cn.wt.struts.action.AdminAction">
<property name="adminDao">
<ref bean="adminDaoImpl" />
</property>
</bean>
<bean name="/workdateAction"
class="cn.wt.struts.action.WorkdateAction">
<property name="workDateDao">
<ref bean="workDateDaoImpl" />
</property>
<property name="userDao">
<ref bean="userDaoImpl"/>
</property>
</bean>
</beans>

配置文件2:/WEB-INF/proxool.xml
Xml代码
<?xml version="1.0" encoding="iso-8859-1"?> 
<!-- the proxool configuration can be embedded within your own application's.  
Anything outside the "proxool" tag is ignored. --> 
<something-else-entirely> 
    <proxool> 
        <alias>DbPool</alias> //与applicationContext.xml中url一致  
        <driver-url>jdbc:mysql://localhost:3306/mysql</driver-url> 
        <driver-class>com.mysql.jdbc.Driver</driver-class> 
        <driver-properties> 
            <property name="user" value="root" /> 
            <property name="password" value="123" /> 
            <property name="useUnicode" value="true" /> 
            <property name="characterEncoding" value="GBK" /> 
        </driver-properties> 
        <minimum-connection-count>5</minimum-connection-count> 
        <maximum-connection-count>50</maximum-connection-count> 
        <maximum-active-time>60000</maximum-active-time> 
        <house-keeping-test-sql>select 1</house-keeping-test-sql> 
        <prototype-count>5</prototype-count> 
        <house-keeping-sleep-time>60000</house-keeping-sleep-time> 
    </proxool> 
</something-else-entirely> 

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
    <proxool>
        <alias>DbPool</alias> //与applicationContext.xml中url一致
        <driver-url>jdbc:mysql://localhost:3306/mysql</driver-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver-properties>
            <property name="user" value="root" />
            <property name="password" value="123" />
            <property name="useUnicode" value="true" />
            <property name="characterEncoding" value="GBK" />
        </driver-properties>
        <minimum-connection-count>5</minimum-connection-count>
        <maximum-connection-count>50</maximum-connection-count>
        <maximum-active-time>60000</maximum-active-time>
        <house-keeping-test-sql>select 1</house-keeping-test-sql>
        <prototype-count>5</prototype-count>
        <house-keeping-sleep-time>60000</house-keeping-sleep-time>
    </proxool>
</something-else-entirely>

配置文件3:web.xml
Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
            /WEB-INF/classes/applicationContext.xml  
        </param-value> 
    </context-param> 
    <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> 
                  <!-- 由于proxool要先加载,所以启动优先级要比spring高,否则在spring加载时会报错 --> 
        <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> 
  <servlet> 
           <!-- 数据库连接监控器 --> 
      <servlet-name>proxoolAdmin</servlet-name> 
      <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>proxoolAdmin</servlet-name> 
        <url-pattern>/proxoolAdmin</url-pattern> 
    </servlet-mapping> 
    <servlet> 
        <servlet-name>action</servlet-name> 
        <servlet-class> 
            org.apache.struts.action.ActionServlet  
        </servlet-class> 
        <init-param> 
            <param-name>config</param-name> 
            <param-value>/WEB-INF/struts-config.xml</param-value> 
        </init-param> 
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>3</param-value> 
        </init-param> 
        <init-param> 
            <param-name>detail</param-name> 
            <param-value>3</param-value> 
        </init-param> 
        <load-on-startup>0</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>action</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<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>
                  <!-- 由于proxool要先加载,所以启动优先级要比spring高,否则在spring加载时会报错 -->
<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>
  <servlet>
           <!-- 数据库连接监控器 -->
  <servlet-name>proxoolAdmin</servlet-name>
  <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proxoolAdmin</servlet-name>
<url-pattern>/proxoolAdmin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

基本配置就这些,以前在根据网上所说的很多利用spring直接管理proxool的方法来实现,这种方法也可以,但有几点问题,1.mysql连接的URL:jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK中间的&符号,在xml中使用&amp;来代替,但我在做测试时发现这样做不行,spring在启动时会报没有这样的驱动,还有些根本就是错误的估计是在网上转来转去,没经过测试就发表的.
关于连接释放,在applicationContext.xml中绝对有必要增加hibernate.connection.release_mode这一参数,而且比较的好的设置应该是after_statement,另一种是after_transaction,而使用after_statement优于after_transaction原因是避免了在一个事务结束后连接就释放,大家可以试一下先将这个参数去掉,运几次数据库操作,会发现产了生很多连接没有释放,大概大30秒至15秒之间可以释放有时会更长,然后在加上这个参数测试.

你可能感兴趣的:(spring,Hibernate,mysql,struts,配置管理)