编程式事务

    /**
* 1. 根据DataSource去创建事务管理器
* 构造方法 , 参数1. DataSource
*/
DataSourceTransactionManager txManager = new DataSourceTransactionManager(getDataSource());
/**
* 2. 创建事务模版对象, 并将事务管理器传递给模版
*/
TransactionTemplate ttlate = new TransactionTemplate(txManager); /**
* 3. 通过事务模版 , 执行事务, 并指定事务的回调函数
*/
Boolean flag = ttlate.execute(new TransactionCallback<Boolean>() { /**
* 我们要处理的JDBC操作, 可以放到这个方法中, 在这个方法里的所有JDBC操作, 将视为一个事务
*
* 这个方法, 如何认定事务是否应该提交 :
*
* 当方法中出现异常, 则表示事务执行失败 ,
* 如果异常进行了处理, 则事务执行成功!
*/
@Override
public Boolean doInTransaction(TransactionStatus status) {
try{ getJdbcTemplate().update("update book set bookname=? where bookid=?", "西游记",10002);
if(1==2){
throw new RuntimeException("停电了, 哈哈哈");
}
getJdbcTemplate().update("update book set bookname=? where bookid=?", "红楼梦",10003);
return true;
}catch(Exception e){
e.printStackTrace();
//处理了异常 , 没事了
//加入回滚标记 ( 本次事务不提交 )
status.setRollbackOnly();
return false; }
}
});
System.out.println("事务执行的结果:"+flag);

声明式事务

相较于编程式的事务 ,有利有弊 !

优点:

是一种aop的编程思想, 给一段代码添加事务, 无需修改原代码

缺点:

因为采用了注解, 注解的最小范围只能给类的成员 , 也就是说, 声明时的事务 ,最小的处理范围是一个方法 !

使用步骤:

1.  向容器中添加事务的管理对象
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean> 2. 开启事务的扫描 , 指定事务管理器 <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> 3. 在具体要被执行事务的方法上, 添加注解@Transactional即可
@Transactional也可以添加到类上,表示对该类的所有方法使用事务

@Transactional注解的属性

@Transactional注解标记有以下属性,在使用时
可以根据需要做特殊设定。
propagation: 设置事务传播
isolation : 设置事务隔离级别
readOnly : 设置为只读,还是可读写
rollbackFor : 设置遇到哪些异常必须回滚
noRollbackFor : 设置遇到哪些异常不回滚

看一个Demo:

User类:

package cn.wxg.bean;

public class User {

    private int id;
private String name;
private String password;
public User() {
super();
}
public int getId() {
return id;
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
} }

User.java

Dao类:

package cn.wxg.dao;

import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate; import cn.wxg.bean.User; public class UserDao extends JdbcDaoSupport { @Transactional
public void test2(List<User> user){
String sql1="update test_user set password=? where name=?";
String sql2="update test_user set password=? where name=?"; getJdbcTemplate().update(sql1, user.get(0).getPassword(),user.get(0).getName());
getJdbcTemplate().update(sql2, user.get(1).getPassword(),user.get(1).getName());
} public boolean updateUser(List<User> user){
String sql1="update test_user set password=? where name=?";
String sql2="update test_user set password=? where name=?"; //声明事务管理组件
DataSourceTransactionManager dstt = new
DataSourceTransactionManager(getDataSource());
//声明事务组件
TransactionTemplate tt = new
TransactionTemplate(dstt);
//执行事务
Boolean res = tt.execute(new TransactionCallback<Boolean>(){ @Override
public Boolean doInTransaction(TransactionStatus status){
try{
getJdbcTemplate().update(sql1, user.get(0).getPassword(),user.get(0).getName());
getJdbcTemplate().update(sql2, user.get(1).getPassword(),user.get(1).getName());
return true;
}catch(Exception e){
status.setRollbackOnly();//回滚
}
return false;
}
});
return res;
}
}

UserDao

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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean id="database" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 驱动地址 -->
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<!-- 数据库的连接地址 -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<!-- 数据库的帐号 -->
<property name="username" value="system"></property>
<!-- 数据库的密码 -->
<property name="password" value="517839"></property>
</bean> <bean id="userDao" class="cn.wxg.dao.UserDao">
<!--
在调用set方法进行赋值,
Spring容器是通过反射技术 ,根据我们传递的name参数 , 得到对应的set方法名称, 将其调用
-->
<property name="dataSource" ref="database"></property>
</bean> <!--配置DataSourceTransactionManager,事务管理着对象|-->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg index="0" ref="database"></constructor-arg>
</bean> <!--开启事务扫描-->
<tx:annotation-driven transaction-manager="txManage" proxy-target-class="true"/>
<!--文件扫描-->
<context:component-scan base-package="cn"></context:component-scan> </beans>

applicationContext.xml

测试类:

package cn.wxg.text;

import java.util.ArrayList;
import java.util.List; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.wxg.bean.User;
import cn.wxg.dao.UserDao; public class UserDaoTest { @Test
public void testName1() throws Exception {
//加载配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = ac.getBean("userDao", UserDao.class);
User user1=new User(1001,"jame","987654321");
User user2=new User(1002,"locy","987654321");
List<User> users=new ArrayList<User>();
users.add(user1);
users.add(user2);
// System.out.println(userDao.updateUser(users));
userDao.test2(users); }
}

UserDaoTest.java

【Spring】Spring之事务处理的更多相关文章

  1. hibernate整合进spring后的事务处理

    单独使用hibernate处理事务 本来只用hibernate开发,从而可以省了DAO层实现数据库访问和跨数据库,也可以对代码进行更好的封装,当我们web中单独使用hibernate时,我们需要单独的 ...

  2. Java面试题(设计模式篇+Spring/Spring MVC篇)

    设计模式 88.说一下你熟悉的设计模式? 自行熟悉. 89.简单工厂和抽象工厂有什么区别? 简单理解简单工厂:对 一个对象的创建进行封装.抽象工厂:对 一组对象的创建进行封装. 比如生产 陶瓷马 和  ...

  3. spring + spring mvc + tomcat 面试题(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  4. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  5. 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

  6. spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

  7. [转]基于Spring + Spring MVC + Mybatis 高性能web构建

    http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...

  8. Spring+Spring MVC+MyBatis

    Spring+Spring MVC+MyBatis 目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六. ...

  9. MyBatis+Spring+Spring MVC整合开发

    MyBatis+Spring+Spring MVC整合开发课程观看地址:http://www.xuetuwuyou.com/course/65课程出自学途无忧网:http://www.xuetuwuy ...

  10. 基于Spring + Spring MVC + Mybatis 高性能web构建

    基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...

随机推荐

  1. 利用NATAPP隧道解决微信公众号开发之本地调试难题

    一.问题 众所周知,微信公众号开发需要公网的有效域名和80端口,本机当然互联网是访问不了的.那么我们难道去一个公网的服务器去开发吗?那样是不是太土了. 答案当然是,NO 当然我们在做微信支付的时候,有 ...

  2. Log4net的不能产生Log文件的问题

    [问题] 用如下的步骤应用了Log4Net: 建立了一个公用的项目, 在里面引入了Log4net的Nuget package. 在公用的项目中建立了一个类,加上了Log4net的attribute. ...

  3. Andriod NDK编译的时候无法使用math.h的函数。

    编译NDK项目的时候,当用到sinf的时候,编译报错: 选中sinf函数,右键Go to declaration, 发现math.h的文件路径是: c:\ProgramData\Microsoft\A ...

  4. (算法)Game

    题目: Jeff loves playing games, Gluttonous snake( an old game in NOKIA era ) is one of his favourites. ...

  5. C++ 纯虚方法

    1.纯虚方法解决什么样的问题,为什么要设计出纯虚方法? 考虑下面的需求,基类声明了一个方法,这个方法只针对具体的子类才有意义,比如Animal的Eat()方法,调用Animal的Eat方法是没有意义的 ...

  6. Android Studio “懒人”必备插件android layout id converter

    在一个布局文件里.假设定义了非常多非常多id,代码中一个个findview是一件非常枯燥而且浪费时间的事情. 所以这里向大家推荐一个必备插件android layout id converter. 配 ...

  7. GCC中的内嵌汇编语言

    原文可参考:GCC中的内嵌汇编语言 一.声明   虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇编语言写成的.有些汇编语言代码是直接写在汇编源程序中的,特别是Li ...

  8. python __setattr__和__getattr__

    通过为dict添加__setattr__和__getattr__, 可以属性的语法访问dict的元素 http://www.2cto.com/kf/201507/413971.html class S ...

  9. exception java.lang.OutOfMemoryError: Java heap space

      1.情景展示 java内存溢出异常,将程序代码问题排除在外,如何增大JVM的使用内存? 2.解决方案  在eclipse中的解决办法:增大你要运行的测试类的内存分配. 点击运行或debug按钮旁的 ...

  10. oracle 拼接字符串的两种方式

      方式一:使用管道符||进行拼接 方式二:使用concat()函数 区别: 方式一可以拼接多个字符串:方式二只能将2个字符串拼接到一起. 写在最后 哪位大佬如若发现文章存在纰漏之处或需要补充更多内容 ...