spring中的事务

spring怎么操作事务的:

事务的转播行为:

事务代码转账操作如下:

接口:

public interface AccountDao {

    //加钱
void addMoney(Integer id,Double money);
//减钱
void decreaseMoney(Integer id,Double Money); }

实现类:

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
public void addMoney(Integer id, Double money) {
String sql="update t_account set money=money+? where id = ?";
getJdbcTemplate().update(sql,money,id);
// TODO Auto-generated method stub } @Override
public void decreaseMoney(Integer id, Double money) {
String sql="update t_account set money=money-? where id = ?";
getJdbcTemplate().update(sql,money,id); } }

service层接口

package cn.itcast.service;

public interface AccountService {

    //转账方法
void transfer(Integer from,Integer to,Double money); }

service实现类:

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; import cn.itcast.dao.AccountDao;
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public class AccountServiceImpl implements AccountService { private AccountDao ad;
private TransactionTemplate tt; @Override public void transfer(final Integer from, final Integer to,final Double money) {
//事务
//减钱
ad.decreaseMoney(from, money);
//加钱
ad.addMoney(to, money); } public AccountDao getAd() {
return ad;
}
public void setAd(AccountDao ad) {
this.ad = ad;
} public TransactionTemplate getTt() {
return tt;
} public void setTt(TransactionTemplate tt) {
this.tt = tt;
} /* @Override
手动代码:
public void transfer(final Integer from, final Integer to,final Double money) {
//事务
tt.execute(new TransactionCallbackWithoutResult() { @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
// TODO Auto-generated method stub
//减钱
ad.decreaseMoney(from, money);
int i=1/0;
//加钱
ad.addMoney(to, money);
}
});
}
*/
}

分析:

XML配置式实现事务:

原理;

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 事物核心管理器,
封装了所有事物操作,
依赖于连接池
-->
<bean name="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 事务模板对象 -->
<bean name="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="dataSourceTransactionManager"></property>
</bean> <!-- 配置事务通知
isolation:隔离级别
propagation:传播行为
read-only:是否只读
-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- 配置织入 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.service..*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将JDBCTemplate放入Spring容器
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- 3.将accountDao放入spring容器 -->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property> </bean>
<!-- 3.将accountService放入spring容器 -->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl">
<property name="ad" ref="accountDao"></property>
<property name="tt" ref="transactionTemplate"></property> </bean> </beans>

xml配置式,中java写法:

1、打开事务

2、调用doInTransactionWithoutResult

3、提交事务

package cn.itcast.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; import cn.itcast.dao.AccountDao;public class AccountServiceImpl implements AccountService { private AccountDao ad;
private TransactionTemplate tt; @Override public void transfer(final Integer from, final Integer to,final Double money) {
//事务
//减钱
ad.decreaseMoney(from, money);
//加钱
ad.addMoney(to, money); } public AccountDao getAd() {
return ad;
}
public void setAd(AccountDao ad) {
this.ad = ad;
} public TransactionTemplate getTt() {
return tt;
} public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}

测试:

package cn.itcast.tx;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext1.xml") public class Demo { @Resource(name="accountService")
private AccountService ad;
@Test
public void fun1() {
ad.transfer(1, 2, 1000.00);
} }

注解式配置事务:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 事物核心管理器,封装了所有事物操作,依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!--开启使用注解配置事务 -->
<tx:annotation-driven /> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将JDBCTemplate放入Spring容器
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- 3.将accountDao放入spring容器 -->
<bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property> </bean>
<!-- 3.将accountService放入spring容器 -->
<bean name="accountService" class="cn.itcast.service.AccountServiceImpl">
<property name="ad" ref="accountDao"></property>
<property name="tt" ref="transactionTemplate"></property> </bean> </beans>

使用注解:

package cn.itcast.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate; import cn.itcast.dao.AccountDao;public class AccountServiceImpl implements AccountService {

private AccountDao ad;
private TransactionTemplate tt; @Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(final Integer from, final Integer to,final Double money) {
//事务
//减钱
ad.decreaseMoney(from, money);
//加钱
ad.addMoney(to, money); } public AccountDao getAd() {
return ad;
}
public void setAd(AccountDao ad) {
this.ad = ad;
} public TransactionTemplate getTt() {
return tt;
} public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}

测试:

package cn.itcast.tx;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml") public class Demo { @Resource(name="accountService")
private AccountService ad;
@Test
public void fun1() {
ad.transfer(1, 2, 1000.00);
} }

结果:

spring再学习之AOP事务的更多相关文章

  1. spring再学习之AOP准备

    一.aop思想: 横向重复,纵向抽取 1.乱码 2.事务管理 3,action 二.spring能够为容器中管理的对象生成代理对象 1.spring能帮我们生成代理对象 2.spring实现aop的原 ...

  2. spring再学习之AOP实操

    一.spring导包 2.目标对象 public class UserServiceImpl implements UserService { @Override public void save() ...

  3. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...

  4. JAVAEE——spring03:spring整合JDBC和aop事务

    一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...

  5. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  6. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  7. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  8. Spring注解开发系列Ⅵ --- AOP&事务

    注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...

  9. Spring框架学习06——AOP底层实现原理

    在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...

随机推荐

  1. 实操|如何将 Containerd 用作 Kubernetes runtime

    日前专为开发者提供技术分享的又拍云 OpenTalk 公开课邀请了网易有道资深运维开发工程师张晋涛,直播分享<Containerd 上手实践 >,详细介绍 Containerd 的发展历程 ...

  2. StringBuilder和输入输出

    构建字符串(StringBuilder的应用) 有些时候,需要由较短的字符串构建字符串,例如:按键或来自文件的单词,采用字符串连接的方式达到此目的效率比较低.每次连接字符串,都会构建一个新的Strin ...

  3. ROS教程(二):创建工作空间(图文)

    ros教程:创建工作空间 目录 前言 一.工作空间? 二.创建一个工作空间 1.创建目录 2.编译 前言 使用catkin创建一个工作空间 一.工作空间? 在ROS系统下,我们所有的项目都放在一个工作 ...

  4. 夯实基础系列一:Java 基础总结

    前言 大学期间接触 Java 的时间也不短了,不论学习还是实习,都让我发觉基础的重要性.互联网发展太快了,各种框架各种技术更新迭代的速度非常快,可能你刚好掌握了一门技术的应用,它却已经走在淘汰的边缘了 ...

  5. 转 6 jmeter元件的作用域与执行顺序

    6 jmeter元件的作用域与执行顺序   元件的作用域 配置元件(config elements)会影响其作用范围内的所有元件.前置处理程序(Per-processors)在其作用范围内的每一个sa ...

  6. (16)-Python3之--集合(set)操作

    1.定义 集合的关键字:set 集合主要作用: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 集合用大括号{}表示,元素间用逗号分隔. 建立集合类型用{} ...

  7. LVS负载均衡IP隧道模式原理介绍以及配置实战

    LVS 基本工作原理 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间 PREROUTING 链首先会接收到用户请求,判断目标 IP 确定是本机 IP,将数 ...

  8. sql 括号

    <select id="chlTransQueryByChlType" parameterType="map" resultType="java ...

  9. LOJ10129

    AHOI 2009 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为 nn 的数列,不妨设为 a1​,a2​,⋯,an​.有如下三种操作形式: 把数列中的一段数全部乘一个值: 把 ...

  10. IDEA中jdk设置

    电脑运行环境是8, 但是IDEA提醒说1.5已经过时,IDEA中jdk设置还是比较麻烦 https://blog.csdn.net/u012365843/article/details/8138883 ...