spring中的事务

spring 事务

事务的概念

事务:是逻辑上一组操作,要么全都成功,要么全都失败.事物目的就是解决【数据不一致】的问题。

事务特性

		ACID:
        原子性:事务不可分割
        一致性:事务执行的前后,数据完整性保持一致.
        隔离性:一个事务执行的时候,不应该受到其他事务的打扰
        持久性:一旦结束,数据就永久的保存到数据库.

事务的隔离级别

		未提交读:   三类读问题都有可能发生。
        已提交读:   避免脏读,但不可重复读,虚读是有可能发生。
        可重复读:   避免脏读,不可重复读,但是虚读有可能发生。
        串行的:        避免以上所有情况.    系统吞吐量很低 

Spring中事务管理

三层架构:web层:Servlet/jsp---->service层—>dao/mapper层
分层开发:事务处在Service层.
1.Spring提供事务管理API:
PlatformTransactionManager:平台事务管理器.
commit(TransactionStatus status)
getTransaction(TransactionDefinition definition)
rollback(TransactionStatus status)
2.TransactionDefinition:事务定义
ISOLation_XXX: 事务隔离级别.
PROPAGATION_XXX: 事务的传播行为.(不是JDBC中有的,为了解决实际开发问题.)
Timeout: 过期时间
3.TransactionStatus:事务状态
是否有保存点
是否一个新的事务
事务是否已经提交
4.三者关系
PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务,管理事务过程中,产生一些事务状态:状态由TransactionStatus记录.
API详解:
Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现
org.springframework.jdbc.datasource.DataSourceTransactionManager
使用Spring JDBC或iBatis 进行持久化数据时使用

        org.springframework.orm.hibernate3.HibernateTransactionManager  
                                使用Hibernate3.0版本进行持久化数据时使用
        
        org.springframework.orm.jpa.JpaTransactionManager           
                                使用JPA进行持久化时使用
        
        org.springframework.jdo.JdoTransactionManager   
                                当持久化机制是Jdo时使用
        
        org.springframework.transaction.jta.JtaTransactionManager
                                使用一个JTA实现来管理事务,在一个事务跨越多个资源时必须使用

spring事务的实现

手动(编程)事务
第1步:业务层的哪些方法需要事务

//service接口
package com.jr.service;
public interface MasterService {
	public void zz(int from,int to,int money);
}

第2步:xml中配置事务管理器

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 告诉spring的bean工厂,要生产什么对象 
        id:bean对象的唯一标识
    -->
    <!-- 加载数据库配置文件 -->
    <context:property-placeholder location="database.properties"/>
    <!-- 根据数据库配置文件 -->                                                                                                                                                                                                                                                                                                                                                                                                                
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    	<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${uname}" />
		<property name="password" value="${password}" />
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<!-- 别名 -->
       <property name="typeAliasesPackage"  value="com.jr.model"></property>
       <!-- mapper  XML映射 -->
        <property name="mapperLocations"  value="classpath*:mapper/*Mapper.xml"></property>
		<!-- 数据源 -->
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- mapper接口配置 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	   <property name="basePackage" value="com.jr.mapper"></property>
	</bean>

    <!-- 5、事务管理 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--  开启注解配置-->
	<context:annotation-config/>
	<context:component-scan base-package="com.jr"></context:component-scan>
	<!--6、手动事务模板  -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> 
	   <property name="transactionManager" ref="txManager"></property>
	</bean>
 </beans>

第3步:在业务类定义TransatcionTemplate事务模板类
在这里插入图片描述
第4步:在需要事务管理的方法中

package com.jr.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.jr.mapper.MasterMapper;
import com.jr.model.Master;
import com.jr.service.MasterService;

@Component
public class MasterServiceImpl implements MasterService{
	@Autowired
	private MasterMapper masterMapper;
	
	@Autowired
    private  TransactionTemplate transactionTemplate;
	
	@Override
    public void zz(int from, int to, int money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {

            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    //转账的操作
                    zzOptional(from, to, money);
                    System.out.println("转账成功");
                } catch (Exception ex) {
                    System.out.println("转账失败,进行回滚");
                    status.setRollbackOnly();
                }
            }
        });
    }
	public void zzOptional(int from, int to, int money) throws Exception {
		// ==============一,强哥的钱减少300==============
        // 1.1查询强哥有多少钱
        Master qg = masterMapper.selectByPrimaryKey(from);
        System.out.println(qg);
        // 1.2扣强哥有300
        qg.setMoney(qg.getMoney() - money);
        int result = masterMapper.updateByPrimaryKey(qg);

        // ==============二,二狗的钱增加300==============
        // 1.1查询强哥有多少钱
        Master eg = masterMapper.selectByPrimaryKey(to);
        System.out.println(eg);
        // 1.2扣强哥有300
        eg.setMoney(eg.getMoney() + money);
        int result2 = masterMapper.updateByPrimaryKey(eg);
        
        //==============最后的结果==============
        if (result > 0 && result2 > 0) {
            System.out.println("转账成功!!");
        } else {
            // int a = 10/0;//产生一个异常
            throw new Exception("转账失败");// 产生一个异常
        }
	}
}

第5步:测试,调用业务类的方法

package com.jr.proxy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jr.mapper.MasterMapper;
import com.jr.service.impl.MasterServiceImpl;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-datasource.xml")
public class MybatisTest {
	@Autowired
    private MasterMapper masterMapper;
    @Autowired
    private MasterServiceImpl masterServiceImpl;  
    @Test
    public void whenZzSuccess() {//測試方法
    	masterServiceImpl.zz(1, 2,300);
    }
}

2、声明式事务(开发)

1步:业务的层的哪些方法需要事务(黑客)。
UserInfoServiceImpl1.zz(int from ,int to ,int money);
         UserInfoServiceImpl1.zz(int from ,int to ,int money);2步:xml中配置事务管理器 (不同的orm框架 事务管理的方式不同)
            (orm框架:mybatis,hibernate,jpa,jdbcTemplate......)
        <bean  id="txTransaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager  ">
            <property name="dataSource" ref="dataSource"/>
        </bean>3步:xml配置中定义切面
    <!-- 1.切面 -->
    <tx:advice id="ndAdvice"  transaction-manager="txTransaction">
            <tx:attributes>
                <!-- 哪个方法需要使用什么事务:哪个屁眼需要插入黑板刷
                zz*:所有以zz开始的方法名的所有方法
                *zz: 所有以zz结束的方法名的所有方法
                -->
                <tx:method name="zz*" />
                <!-- *  其他方法使用默认事务   事务是只读的,就表名事务内不能对数据进行更新。-->
                <tx:method name="*"  read-only="true"/>
        </tx:attributes>
    </tx:advice>4步:xml配置根据切点+增强   ,自动生成业务方法的代理对象

3、spring基于注解的事务管理
第1步: 启用注解事务

<!--  开启注解配置-->
	<context:annotation-config/>
	<context:component-scan base-package="com.jr"></context:component-scan>
	<!--6、手动事务模板  -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> 
	   <property name="transactionManager" ref="txManager"></property>
	</bean>
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="txManager"/>

第2步:使用@Transactional标示需要事务的方法

package com.jr.service.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.jr.mapper.MasterMapper;
import com.jr.model.Master;
import com.jr.service.MasterService;

@Component
public class MasterServiceImpl3 implements MasterService{
	@Autowired
	private MasterMapper masterMapper;
	
	/**
	 * 注解式事务
	 */
	@Transactional
    public void zz(int from, int to, int money)throws Exception {
        //转账的操作
        zzOptional(from, to, money);
    }
	public void zzOptional(int from, int to, int money) throws Exception {
		// ==============一,强哥的钱减少300==============
        // 1.1查询强哥有多少钱
        Master qg = masterMapper.selectByPrimaryKey(from);
        System.out.println(qg);
        // 1.2扣强哥有300
        qg.setMoney(qg.getMoney() - money);
        int result = masterMapper.updateByPrimaryKey(qg);

        // ==============二,二狗的钱增加300==============
        // 1.1查询强哥有多少钱
        Master eg = masterMapper.selectByPrimaryKey(to);
        System.out.println(eg);
        // 1.2扣强哥有300
        eg.setMoney(eg.getMoney() + money);
        int result2 = masterMapper.updateByPrimaryKey(eg);
        
        //==============最后的结果==============
        if (result > 0 && result2 > 0) {
            System.out.println("转账成功!!");
        } else {
            // int a = 10/0;//产生一个异常
            throw new Exception("转账失败");// 产生一个异常
        }
	}
}

第3步:测试

package com.jr.proxy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.jr.mapper.MasterMapper;
import com.jr.service.MasterService;
import com.jr.service.impl.MasterServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-datasource-annotation.xml")
public class SpringTransacionTest {
    
    @Autowired
    @Qualifier("masterServiceImpl3")
    private MasterService masterService;
    
    
    @Test
    public void whenZzSuccess() {
    	try {
			masterService.zz(1, 2,300);
		} catch (Exception e) {
			System.out.println("转账失败!!!!");
		}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值