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. Os-hackNos-特权文件提权

    一 信息收集 netdiscover -i eth0 -r 10.10.10.0/24 扫描ip nmap -sP 192.168.43.0/24 扫描开放的端口 使用"-sP"选 ...

  2. 工作记录:记一次线上ZK掉线问题排查

    目录 问题的发现 zk的情况以及分析 总结 问题的发现 最早问题的发现在于用户提的,用户提出他支付时支付失败,过了一会儿再试就好了,于是翻日志,查询到当时duboo调用出现了下类错误: [TraceI ...

  3. sap的内核升级,修补了源代码保护的方式

    众所周知,在SAP的内核位701或者之前的版本中,我们可以通过向源代码的中加入"*@#@@[SAP]"这样的代码,来实现对源代码的保护.但是在内核升级到721和以后的版本中,你会发 ...

  4. 一个简单的IM聊天程序Pie IM(以后会更新)

    这个程序用多线程,实现设备之间的聊天,支持win10通知,欢迎下载 依赖的第三方库 win10toast 代码 将以下代码写入任意.py文件 1 print('Welcome to use Pie I ...

  5. JVM(五)手动解析.class文件

    一:不同进制之间的转换 二进制:逢2进1,数字0-1. 八进制:逢8进1,数字0-7.三位二进制表示一位八进制.三位二进制最大为111,最大为7. 十进制:逢10进1,数字0-9.四位二进制表示一位十 ...

  6. 前端中的script标签

    script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...

  7. Netty之JAVA BIO模型

    一.JAVA BIO模型 1.I/O模型 I/O 模型简单的理解:就是用什么样的通道进行数据的发送和接收,很大程度上决定了程序通信的性能 Java 共支持 3 种网络编程模型/IO 模式:BIO.NI ...

  8. WPF权限控制——【1】界面布局

    本来就不怎么喜欢写博客,好不容易申请了博客园的账号,迈出了先前没有跨越的第一步:转眼间几年的时间就过去了,还是空空如也.今天的心境是这样的,发现wpf相关的资料及源码实在不多,就想写下随笔:一方面是自 ...

  9. 20.LVM

    1.在硬盘分好区或者部署为RAID 磁盘阵列之后,再想修改硬盘分区大小就不容易了.换句话说,当用户想要随着实际需求的变化调整硬盘分区的大小时,会受到硬盘"灵活性"的限制. 这时就需 ...

  10. 数理统计4:均匀分布的参数估计,次序统计量的分布,Beta分布

    接下来我们就对除了正态分布以外的常用参数分布族进行参数估计,具体对连续型分布有指数分布.均匀分布,对离散型分布有二项分布.泊松分布几何分布. 今天的主要内容是均匀分布的参数估计,内容比较简单,读者应尝 ...