spring 声明式事物管理

事物管理用于出错回滚等


1基于xml的配置

applicationContext.xml中声明jdbc事物管理器并配置数据源datasource c3p0等

还需引入namespace 否则会报错且没提示,

aop



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

xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-xsd"



tx


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






<!--jdbc事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
配置事务通知

<!--配置事物通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
配置事物切面

<!--配置事物切面-->
<aop:config>
    <!--配置切点-->
    <aop:pointcut id="serviceMethod" expression="execution(* service.StudentService*.*(..))"/>
                  <!--配置事物通知-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
expression="execution(* service.*.*(..))" 返回值 包 类 方法名  (..)任意参数 ×任意值



2注解式

在applicationContext.xml中加入

<tx:annotation-driven transaction-manager="transactionManager"/>
然后在相应的地方即service层添加@transactional即可service层发生事物管理

总结各有优缺点

1 service多的情况下比较好不用每个service写@transactional注解




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值