• javaee spring 声明式事务管理方式2 注解方式


    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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <context:component-scan base-package="com.test" />
        
        <context:property-placeholder location="db.properties" />
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driverClass}" />
            <property name="jdbcUrl" value="${url}" />
            <property name="user" value="${user}" />
            <property name="password" value="${password}" />
        bean>
        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg name="dataSource" ref="comboPooledDataSource" />
        bean>
    
        
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="comboPooledDataSource" />
        bean>
        
        <tx:annotation-driven transaction-manager="txManager" />
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    方法

    package com.test.service.impl;
    
    import com.test.dao.ICardInfoDao;
    import com.test.exception.MyException;
    import com.test.service.ICardInfoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    public class CardInfoService implements ICardInfoService {
    
        @Autowired
        private ICardInfoDao cardInfoDao;
    
        public ICardInfoDao getCardInfoDao() {
            return cardInfoDao;
        }
    
        public void setCardInfoDao(ICardInfoDao cardInfoDao) {
            this.cardInfoDao = cardInfoDao;
        }
    
        //实现转账方法
        @Override
        //通过注解的方式 声明事务方法 (隔离级别  传播行为  回滚的条件)
        @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor =MyException.class )
        public void transfer(int from, int to, float money) throws Exception {
    
            //一方减钱
            cardInfoDao.decreaseMoney(from,money);
    
            if(true)
                throw  new MyException("转账异常");
            //一方加钱
            cardInfoDao.increaseMoney(to,money);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
  • 相关阅读:
    微信支付-无法重写WXPayConfig内的部分抽象方法
    一款轻量级事件驱动型应用程序框架
    细粒度图像分类论文研读-2014
    【Tools】工具
    【Spring学习笔记】
    一个超级大的文件如何更快读
    Java持久层框架:MyBatis介绍
    ubuntu20.04下Kafka安装部署及基础使用
    机器人控制算法四之迭代法求解四轴机器人逆解
    es中的匹配显示问题
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/132656687