spring事务配置

要配置spring事务处理,首先要在<beans>中添加以下代码。

 

 

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"


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.x
 

 


如下:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/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">
 

 




<!-- 配置事务管理器 -->

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

 


    <!--     事务的传播特性 -->

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="do*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
 

 



我在这里只为以do开头的方法配置事务,要是需要配置多个就多添加几个

<tx:method name="do*" propagation="REQUIRED"/>

 

要是想给除了以do开头以外的方法加入只读属性,可以这样写

<tx:method name="*" read-only="true"/>
   

 

 

除了REQUIRED事务,spring中还有以下事务
       PROPAGATION_MANDATORY: 方法必须在一个现存的事务中进行,否则丢出异常
       PROPAGATION_NESTED: 在一个嵌入的事务中进行
       PROPAGATION_NEVER: 不应在事务中进行,如果有则丢异常
       PROPAGATION_NOT_SUPPORTED: 不应再事务中进行,如果有就暂停现存的事务
       PROPAGATION_REQUIRED: 支持现在的事务,如果没有就建立一个新的事务
       PROPAGATION_REQUIRES_NEW: 建立一个新的事务,如果现存一个事务就暂停它
       PROPAGATION_SUPPORTS: 支持现在的事务,如果没有就以非事务的方式执行



    <!--     哪些类哪些方法使用事务 -->

<aop:config>
        <aop:pointcut id="allServiceMethod" expression="execution(* org.test.demo.impl.*Impl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
    </aop:config>
 


提醒:注意区分大小写。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值