{spring}事务管理的配置操作 - 复制可以直接使用

SQL语言分类 DDL(数据定义语言)create drop alter DML(数据操纵语言)insert update delete select * TCL(事务控制语言)commit rollback DCL(数据控制语言)grant revoke

spring事务管理

1、声明事务管理器(将数据源委托给spring提供的平台同一管理)

<!--配置事务管理器-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源委托事务管理平台管理事务-->
        <property name="dataSource" ref="dataSource"></property>
</bean>

2、使用注解方式实现事务管理

a、spring配置文件添加头部分

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

b、添加使用注解实现事务配置

<!--使用注解方式实现事务管理-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

c、在需要添加事务的业务方法上加上@Transactional

@Transactional
public void zz(){
        Bank b1 = this.bankDAO.get("lifan");
        Bank b2 = this.bankDAO.get("zhangsan");
        b1.setMoney(b1.getMoney()-500);
        b2.setMoney(b2.getMoney()+500);
        this.bankDAO.update(b1);
        String s = null;
        s.charAt(0);
        this.bankDAO.update(b2);
}

a、定义事务管理通知

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
</tx:advice>

b、配置aop实现事务 织入

<aop:config>
        <!--切入点-->
        <aop:pointcut id="txPointcut" expression="execution(public * com.web.service.*.*(..))"/>
        <!--通知-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值