spring hibernate c3p0 mysql 配置

来源:互联网 发布:网络清洁器 编辑:程序博客网 时间:2024/05/21 17:32

c3p0   c3p0-0.9.1.2.jar

 

 

 

jdbc.properties

 

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.user=****
jdbc.password=****

jdbc.initialPoolSize=1
jdbc.minPoolSize=1
jdbc.maxPoolSize=10

 

 

spring配置

 

<?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:util="http://www.springframework.org/schema/util"
    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/context
                  http://www.springframework.org/schema/context/spring-context-2.5.xsd
                  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
 
 <!-- 引入参数配置文件 -->
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>
 
 <!-- 配置数据源  -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass">
   <value>${jdbc.driverClass}</value>
  </property>
  <property name="jdbcUrl">
   <value>${jdbc.url}</value>
  </property>
  <property name="user">
   <value>${jdbc.user}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
  <property name="initialPoolSize">
   <value>${jdbc.initialPoolSize}</value>
  </property>
  <property name="minPoolSize">
   <value>${jdbc.minPoolSize}</value>
  </property>
  <property name="maxPoolSize">
   <value>${jdbc.maxPoolSize}</value>
  </property>
 </bean>

 
  <!-- spring Hibernate SessionFactory -->
 <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.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.jdbc.fetch_size">80</prop>
    <prop key="hibernate.jdbc.batch_size">35</prop>
    
   </props>
  </property>
  <property name="mappingDirectoryLocations">
   <list>
    <value>classpath:/com/pojos/</value>
   </list>
  </property>
 </bean>
 
 
 <!-- 事务管理器 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 
 <!-- 事务属性(新功能) 
   propagation="REQUIRED"(默认)事务传播行为(没有事务就传,有事务就用原来的)
   isolation="DEFAULT"   (默认)事务隔离级别 
 -->
 <tx:advice id="mytx">
     <tx:attributes>
         <tx:method name="add*"    propagation="REQUIRED" isolation="DEFAULT"/>
         <tx:method name="del*"    propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
         <tx:method name="*"       propagation="SUPPORTS" read-only="true"/>
     </tx:attributes>
 </tx:advice>
 
 
 <!-- 织入 -->
 <aop:config>
     <aop:advisor advice-ref="mytx" pointcut="execution(* com.server.*.*(..))"/>
 </aop:config>
 
 
</beans>

文章来源于:http://blog.csdn.net/zhaoweitco/article/details/5657671

0 0