spring txManager配置

来源:互联网 发布:80端口被系统占用 编辑:程序博客网 时间:2024/06/06 21:02
xmlNameSpace 配置:
xmlns:tx="http://www.springframework.org/schema/tx
schemaLocation 配置:
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  

<tx:annotation-driven transaction-manager="txManager"/> <!--使用注解配置事务  @Transactional  -->
<!-- hibernate框架下 事务管理者  -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

propagation :  事务的传播途径,枚举类型。
    REQUIRED :  默认配置,如果有事务,就使用当前事务,如果没有事务,就创建一个新的事务。
    MANDATORY :  必须已经有一个事务,如果没有就抛出异常。
    NESTED :  如果有事务,建立一个事务保存点,再开启一个事务,内部事务抛出异常后回滚到保存点。
    NEVER : 不能有事务,如果有事务抛出异常。
    NOT-SUPPORTED : 该方法不在事务内执行,如果开启了事务,将事务挂起,执行该方法,然后再执行事务。
    SUPPORTS : 有事务就使用事务,没事务就不使用事务。
    REQUIRES-NEW : 内嵌一个事务,内部回滚,外部不回滚。

read-only=true : 只读,不允许修改数据。此时,spring会使用 readonly 的connection 提高了效率。

rollbackFor  :  抛出什么异常回滚,默认为 抛出 RuntimeException 回滚
noRollbackFor  :  抛出什么异常不回滚

基于xml配置文件的事务管理配置
<tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="save*" />
            <tx:method name="update*"  rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(public * com.xcty.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>  
0 0
原创粉丝点击