SSH2框架Hibernate一些配置

来源:互联网 发布:企业搜索软件破解版 编辑:程序博客网 时间:2024/06/05 02:28
数据源配置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" xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"      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/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">             <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location">              <value>classpath:jdbc.properties</value>          </property>      </bean>      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" value="${db.driverClassName}" />          <property name="url" value="${db.url}" />          <property name="username" value="${db.username}" />          <property name="password" value="${db.password}" />        <property name="defaultAutoCommit" value="false" /><property name="maxActive" value="150" />        <property name="maxIdle" value="100" />        <property name="maxWait" value="60000" />        <property name="minIdle"  value="5" />        <property name="testOnBorrow" value="true"/>        <property name="testWhileIdle" value="true"/>         <property name="validationQuery" value="select 1 from dual" />      </bean>      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">          <property name="dataSource" ref="dataSource" />          <property name="hibernateProperties">              <props>             <!--                  <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>   -->  <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>                              <prop key="hibernate.show_sql">true</prop>                  <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>            </props>          </property>          <property name="mappingResources">              <list>                  <value>com/yeshun/bean/User.hbm.xml</value>              </list>          </property>      </bean>          <!--jdbcTemplate--><bean id="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean><!--dao--><bean id="baseDAO" class="com.yeshun.dao.impl.BaseDAOImpl"abstract="true"><property name="sessionFactory" ref="sessionFactory" /><property name="jdbcTemplate" ref="jdbcTemplate" /></bean></beans>
       destroy-method="close">                   
BasicDataSource提供了close()方法关闭数据源,所以必须设定destroy-method=”close”属性, 以便Spring容器关闭时,数据源能够正常关闭。除以上必须d数据源属性外,还有一些常用d属性:
    defaultAutoCommit:设置从数据源中返回d连接匙否采用自动提交机制,默认值为 true;
    defaultReadOnly:设置数据源匙否仅能执行只读操作, 默认值为 false;
    maxActive:最大连接数据库连接数,设置为0时,表示没有限制;
    maxIdle:最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止,设置为0时,表示没有限制;
    minIdle :最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请    
    maxWait:最大等待秒数,单位为毫秒, 超过时间会报出错误信息;
    validationQuery:用于验证连接匙否成功d查询SQL语句,SQL语句必须至少要返回一行数据, 如你可以简单地设置为:“select 1 from dual”;
    removeAbandoned:匙否自我中断,默认匙 false ;
    removeAbandonedTimeout:几秒后数据连接会自动断开,在removeAbandoned为true,提供该值;
    logAbandoned:匙否记录中断事件, 默认为 false;
    initialSize :连接池启动时的初始值
    testOnBorrow : 在取出连接时进行有效验证


<!-- #运行判断连接超时任务的时间间隔,单位为毫秒,默认为-1,即不执行任务。 -->
  <property name="timeBetweenEvictionRunsMillis" value="3600000" />
  <!-- #连接的超时时间,默认为半小时。 -->
  <property name="minEvictableIdleTimeMillis" value="3600000" />  

testOnBorrow:在进行borrowObject进行处理时,对拿到的connection进行validateObject校验,默认为false
testOnReturn:就是在进行returnObject对返回的connection进行validateObject校验,默认为false
testWhileIdle:GenericObjectPool中针对pool管理,起了一个Evict的TimerTask定时线程进行控制(可通过设置参数timeBetweenEvictionRunsMillis>0),定时对线程池中的链接进行validateObject校验,对无效的链接进行关闭后,会调用ensureMinIdle,适当建立链接保证最小的minIdle连接数,默认为false
numTestsPerEvictionRun:代表每次检查链接的数量,建议设置和maxActive一样大,这样每次可以有效检查所有的链接.

<!-- 这是Spring封装hibernate后提供的一个过滤器,这个过滤器的作用是:每一次请求来的时候都打开一个session每次请求结束后关闭session,解析hibernat延迟加载产生的异常。-->
<filter>    <filter-name>hibernateFilter</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter>



原创粉丝点击