Spring事务管理transactionManager
bean.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: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">
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property ref="dataSource" name="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--mysql-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--service-->
<bean class="com.war.service.Service" id="service">
<property name="dao" ref="dao"></property>
</bean>
<!--dao-->
<bean class="com.war.dao.DaoImpl" id="dao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--jdbcTemplate-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
DaoImpl.java
package com.war.dao;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Created by Administrator on 2017/6/21.
*/
public class DaoImpl implements Dao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public String addMoney() {
String sql = "update account set money =money + ? where name = ?";
jdbcTemplate.update(sql,1000,"tom");
System.out.println("add over");
return null;
}
@Override
public String reduceMoney() {
String sql = "update account set money = money - ? where name = ?";
jdbcTemplate.update(sql,1000,"tim");
System.out.println("reduce over");
return null;
}
}
Service.java
package com.war.service;
import com.war.dao.DaoImpl;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by Administrator on 2017/6/21.
*/
@Transactional
public class Service {
private DaoImpl dao;
public DaoImpl getDao() {
return dao;
}
public void setDao(DaoImpl dao) {
this.dao = dao;
}
public void TransferAccounts(){
dao.reduceMoney();
// int a = 10/0;
dao.addMoney();
System.out.println("over");
}
}
TestTransaction.java
package com.war;
import com.war.service.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2017/6/21.
*/
public class TestTransaction {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Service service = (Service) ac.getBean("service");
service.TransferAccounts();
}
}
Spring事务管理transactionManager的更多相关文章
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- spring事务管理器设计思想(二)
上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...
- 事务管理(下) 配置spring事务管理的几种方式(声明式事务)
配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...
- Spring事务管理器的应对
Spring抽象的DAO体系兼容多种数据访问技术,它们各有特色,各有千秋.像Hibernate是非常优秀的ORM实现方案,但对底层SQL的控制不太方便:而iBatis则通过模板化技术让你方便地控制SQ ...
- Spring事务管理(转)
1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是 ...
- [Spring框架]Spring 事务管理基础入门总结.
前言:在之前的博客中已经说过了数据库的事务, 不过那里面更多的是说明事务的一些锁机制, 今天来说一下Spring管理事务的一些基础知识. 之前的文章: [数据库事务与锁]详解一: 彻底理解数据库事务一 ...
- Spring 事务管理高级应用难点剖析--转
第 1 部分 http://www.ibm.com/search/csass/search/?q=%E4%BA%8B%E5%8A%A1&sn=dw&lang=zh&cc=CN& ...
- MyBatis6:MyBatis集成Spring事务管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事务管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基 ...
- 【转】Spring事务管理
原文链接 在 Spring 中,事务是通过 TransactionDefinition 接口来定义的.该接口包含与事务属性有关的方法.具体如清单 1 所示: 清单 1. TransactionDefi ...
随机推荐
- IIS环境配置和项目部署
本人实际工作项目中IIS部署,亲测可用~~ 具体步骤: 1.打开控制面板 2.打开程序和功能 3.打开或关闭Windous功能 然后勾选相关内容: 4.添加应用程序 先打开iis管理器页面(控制面板— ...
- java对象深度拷贝
如何利用序列化来完成对象的拷贝呢?在内存中通过字节流的拷贝是比较容易实现的.把母对象写入到一个字节流中,再从字节流中将其读出来,这样就可以创建一个新的对象了,并且该新对象与母对象之间并不存在引用共享的 ...
- CSAPP之阅读笔记-计算机系统漫游(1)
最近在看CSAPP(深入理解计算机系统第二版),其实最新版是第三版.但是,我看了一下价格100多大洋,于是去老夫子旧书网上买了本第二版的,花了30多块钱.哈哈. 网上看了一些关于此书的书评,都说是本好 ...
- REST(Representational state transfer)的四个级别以及HATEOAS介绍
Rest RES(Representational state transfer):表现层状态转移.其实它省略了主语,「表现层」其实指的是「资源」的「表现层」,所以通俗来讲就是:资源在网络中以某种表现 ...
- 小菜鸟从0基础开始学Linux系统
随着当今信息时代的迅速发展,Linux凭借其诸多优势从操作系统中脱颖而出,受到越来越多电脑用户的青睐.Linux是一个集安全.稳定.自由等众多优点于一身的操作系统,不可思议的是这么好的系统还是免费的! ...
- php 面向对象二
多态: 多态就是多种形态:多态分为方法重写和方法重载,但是php不支持方法重载 重写: 子类和父类的方法名必须一致,严格标准要求参数必须一致,但是参数可以不一致 子类中覆盖的方法不能比父类的方法访问权 ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- 面试题:电梯/雨伞/杯子/笔/A4纸/纸杯… 怎么测试?
目的 面试的时候,面试官出题可能会出其不意: 比如随意指定生活当中的一件物品,问你如何测试,见下 作为测试人员,电梯/雨伞/杯子/笔/A4纸/纸杯… 怎么测试? 面试官的考察点 1.在没有需求文档或者 ...
- jenkins 忘记用户名和密码
有时候会经常遇到这种情况,长时间不用会遗忘用户名和密码 admin初始密码未修改的情况 进入\Jenkins\secrets目录,打开initialAdminPassword文件,复制密码: 访问Je ...
- use this method get wifi from notebook
sudo apt dist-upgrade http://cache.baiducontent.com/c?m=9d78d513d9981de90fb3837e7c478a265b09c0307a8c ...