`

Spring3 集成 Hibernate4事务

 
阅读更多

Spring3 集成 Hibernate4事务

 

注意事项

<!-- 5. 配置事务管理器 注意是,hibernate4,配置成hibernate3报错 -->

           <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

             <property name="sessionFactory" ref="sessionFactory" />

           </bean>

           

配置文件:

<?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: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/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
> 
           <!-- 1. 配置datasource -->
           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                <!-- 指定连接数据库的驱动 -->
                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                <!-- 指定连接数据库的URL -->
                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hibernate?useunicode=true&amp;characterEncoding=utf8"/>
                <!-- 指定连接数据库的用户名 -->
                <property name="user" value="root"/>
                <!-- 指定连接数据库的密码 -->
                <property name="password" value="root"/>
                <!-- 指定连接数据库连接池的最大连接数 -->
                <property name="maxPoolSize" value="40"/>
                <!-- 指定连接数据库连接池的最小连接数 -->
                <property name="minPoolSize" value="1"/>
                <!-- 指定连接数据库连接池的初始化连接数 -->
                <property name="initialPoolSize" value="1"/>
                <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
                <property name="maxIdleTime" value="20"/>
                
           </bean>
           
           <!-- 2. 配置sessionFactory -->
           <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
             <!--dataSource属性指定要用到的连接池--> 
             <property name="dataSource" ref="dataSource"/> 
           
             <!--指定要用到的实体映射文件--> 
             <property name="mappingResources"> 
                <list> 
                  <value>com/test/hibernate/mapping/News.hbm.xml</value> 
                </list> 
             </property> 
           
             <!--配置Hibernate的属性--> 
             <property name="hibernateProperties"> 
                <value> 
                  hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                  hibernate.hbm2ddl.auto=update
                  hibernate.show_sql=true
                  hibernate.format_sql=true
                  hibernate.temp.use_jdbc_metadata_defaults=false
                  
                </value> 
             </property>
           </bean> 
           
           <!--  配置HibernateTemplate, hibernate4里已经舍弃了HibernateTemplate,这个配置不需要了,
                   直接注入 sessionFactory到Dao组件
                   在hibernate4里已经舍弃了HibernateTemplate,因为在hibernate4里的事务已经完全可以实现事务了
                   由于hibernate4已经完全可以实现事务了 与spring3.1中的hibernatedao,hibernateTemplete等有冲突,
                   所以spring3.1里已经不提供hibernatedaosupport,hibernateTemplete了,
                   只能用hibernate原始的方式用session
           <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
             <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
           -->
           
           <!-- 3. 配置Dao层 Bean,该Dao继承自HibernateDaoSupport,需要注入HibernateTemplate -->
           <bean id="newsDao" class="test.hibernate.pojo.dao.NewsDao">
             <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
           
           <!-- 4. 配置business业务逻辑层Bean,需要注入Dao层Bean -->
           <bean id="newsBusiness" class="test.hibernate.business.NewsBusiness">
             <property name="newsDao" ref="newsDao"></property>
           </bean>
           
           <!-- 5. 配置事务管理器 注意是,hibernate4,配置成hibernate3报错 -->
           <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
             <property name="sessionFactory" ref="sessionFactory" />
           </bean>
           
           <!--6. 事务配置,主要是使用的transaction-manager不一样
                配置增强处理的bean(相当于切面类),也是Spring自动生成普通
                业务逻辑bean(targetBean)的代理Bean.里面的tx:method配置每个方法的事务属性,
                name配置方法名,可使用通配符.
           -->
           <tx:advice id="txJpaAdvice" transaction-manager="txManager">
              <!-- 配置详细的事务语义 -->
              <tx:attributes>
                <!-- 表示get开头的方法是只读的 -->
                <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
                <!-- 其他方法使用默认的事务设置 -->
                <tx:method name="*" propagation="REQUIRED"/>
              </tx:attributes>
           </tx:advice>
           
           <!-- 全局事务切面配置,AOP元素配置 -->
           <aop:config>
             <!--
                  配置一个切入点 test.hibernate.business包下面所有类的所有方法,都会被加入事务
             -->
             <aop:pointcut id="myJpaPoint" 
                           expression="execution(* test.hibernate.business.*.*(..))" />
             <!-- 配置 (事务代理)切入点(aop:pointcut) 和 切面类(tx:advice),将二者关联起来  -->
             <aop:advisor advice-ref="txJpaAdvice" pointcut-ref="myJpaPoint" />
           </aop:config>
           
           
           
</beans> 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics