`

spring + struts + hibernate 数据源的几种配置方法(原创)

阅读更多

spring + struts + hibernate 数据源的几种配置方法


环境:JDK 1.6 + tomcat 6.0 + spring2.5 + struts2.18 + hibernate3.x

 

1. c3p0数据连接池 (需要将mysql-connector-java-5.0.8-bin.jar文件copy到tomcat 的lib下)
 <?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-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">
 - <!--  使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入
   -->
   <context:component-scan base-package="cn.com.zqk" />
 - <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   <property name="driverClass" value="com.mysql.jdbc.Driver" />

   <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=gbk" />
 - <property name="properties">
 - <props>
   <prop key="c3p0.minPoolSize">2</prop>
   <prop key="c3p0.maxPoolSize">100</prop>
   <prop key="c3p0.timeout">5000</prop>
   <prop key="c3p0.max_statement">100</prop>
   <prop key="c3p0.testConnectionOnCheckout">true</prop>
 - <!--  获取connnection时测试是否有效 
   -->
   <prop key="c3p0.testConnectionOnCheckin">true</prop>
 - <!--  自动测试的table名称 
   -->
   <prop key="c3p0.automaticTestTable">t_wb_zqk</prop>
 - <!--  set to something much less than wait_timeout, prevents connections from going stale 
   -->
   <prop key="c3p0.idleConnectionTestPeriod">18000</prop>
 - <!--  set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out 
   -->
   <prop key="c3p0.maxIdleTime">25000</prop>
   <prop key="user">root</prop>
   <prop key="password">123456</prop>
 - <!--  set to 'SELECT 1' 
   -->
   <prop key="validationQuery">SELECT 1</prop>
 - <!--  set to 'true' 
   -->
   <prop key="testWhileIdle">true</prop>
 - <!--  some positive integer 
   -->
   <prop key="timeBetweenEvictionRunsMillis">3600000</prop>
 - <!--  set to something smaller than 'wait_timeout' 
   -->
   <prop key="minEvictableIdleTimeMillis">18000000</prop>
 - <!--  if you don't mind a hit for every getConnection(), set to "true"
   -->
   <prop key="testOnBorrow">true</prop>
   </props>
   </property>
   </bean>
 - <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
 - <property name="mappingDirectoryLocations">
 - <list>
   <value>classpath:/cn/com/zqk/model/</value>
   </list>
   </property>
 - <property name="hibernateProperties">
 - <value>
   hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
 - <!--  hibernate.hbm2ddl.auto=update
   -->
   hibernate.show_sql=false hibernate.format_sql=true
   </value>
   </property>
   </bean>
 - <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
   </bean>
 - <!-- 使用基于注解方式配置事务
   -->
   <tx:annotation-driven transaction-manager="txManager" />
   </beans>

2. JNDI + proxoolDB 连接池(需要将mysql-connector-java-5.0.8-bin.jar,proxool-0.9.1.jar,proxool-cglib.jar文件copy到tomcat 的lib下)

 第一步:在tomcat/conf/context.xml中配置JDNI
  <Resource  name="jdbc/jndiname"
      auth="Container"
      type="javax.sql.DataSource"
   factory="org.logicalcobwebs.proxool.ProxoolDataSource"
      proxool.alias="proxoolDB"
      user="root"
      password="123456"
      delegateProperties="foo=1,bar=true"
      proxool.jndi-name="jndiname"   
      proxool.driver-url="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=GBK"    
      proxool.driver-class="com.mysql.jdbc.Driver"  
      proxool.house-keeping-sleep-time="900000"  
      proxool.maximum-active-time="5"
      proxool.prototype-count="3"
      proxool.statistics="1m,15m,1d"
      proxool.simultaneous-build-throttle="10"
      proxool.minimum-connection-count="5"
      proxool.maximum-connection-count="100"
      proxool.house-keeping-test-sql="select * from t_wb_zqk" 
      proxool.test-before-use="true"/>
 
 第二步:applicationContext.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:context="http://www.springframework.org/schema/context"
        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/context
     http://www.springframework.org/schema/context/spring-context-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">
  
  <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
  <context:component-scan base-package="cn.com.zqk" />
        
  <bean id="dataSource"  class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName">
   <value>java:comp/env/jdbc/jndiname</value>
   </property>
  </bean>
  

  
  <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />

   <property name="mappingDirectoryLocations">
    <list>
        <value>classpath:/cn/com/zqk/model/</value>            
    </list>
   </property>
   
   <property name="hibernateProperties">
    <value>
     hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
     <!-- hibernate.hbm2ddl.auto=update -->
     hibernate.show_sql=true
     hibernate.format_sql=true    
     hibernate.cache.use_query_cache = true
     hibernate.cache.provider_class = org.hibernate.cache.EhCacheProvider
     hibernate.cache.use_second_level_cache = true
     <!-- hibernate.connection.release_mode = after_statement --> 
    </value>
   </property>
  </bean>
  
  
  
  <bean id="txManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  <!--使用基于注解方式配置事务 -->
  <tx:annotation-driven transaction-manager="txManager" />

 </beans>

 

 

个人签名

-------------------------------------

 

图盾 淘宝保护 保护图片 图片防盗

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics