1.创建事务管理类  TransactionManager.java

 package com.day02.sation.transaction;

 import com.day02.sation.util.JdbcUtil;

 import java.sql.SQLException;

 /**
* Created by Administrator on 1/8.
*/
public class TransactionManager {
/**
* 关闭事务自动提交
*/
public void before1() {
System.out.println("-----before-----");
try {
JdbcUtil.getConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 手动提交事务
*/
public void after2() {
System.out.println("-----after-----");
try {
JdbcUtil.getConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 事务回滚
*/
public void rollback() {
System.out.println("-----rollback-----");
try {
JdbcUtil.getConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

2.创建配置文件

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--需要被管理的业务类-->
<bean id="ticket3Service" class="com.day02.sation.transaction.Ticket3Service"/>
<!--事务管理器-->
<bean id="txManager" class="com.day02.sation.transaction.TransactionManager"/>
<!-- aop配置-->
<aop:config>
<!-- 配置切面-->
<aop:aspect ref="txManager">
<!--地点:方法-->
<aop:pointcut id="txPoint" expression=" execution(* com.day02.sation.transaction.*Service.*(..) )"/>
<!--时间-->
<!-- 方法执行前-->
<aop:before method="before1" pointcut-ref="txPoint"/>
<!--方法执行后-->
<aop:after-returning method="after2" pointcut-ref="txPoint"/>
<!--抛出异常时-->
<aop:after-throwing method="rollback" pointcut-ref="txPoint"/>
</aop:aspect>
</aop:config>
</beans>

3.需要被管理的业务类 Ticket3Service.java

 package com.day02.sation.transaction;

 import com.day02.sation.util.JdbcUtil;

 import java.sql.Connection;
import java.sql.Statement; /**
* Created by Administrator on 1/8.
*/
public class Ticket3Service {
public void buyTicket() throws Exception {
Connection connection = JdbcUtil.getConnection();
Statement statement = connection.createStatement();
//关闭自动提交数据
String sql = " UPDATE ticket SET standby=14 WHERE id=3 ";
int i = statement.executeUpdate(sql);
System.out.println("===" + (1 / 0));
//新增订单
String sql2 = "INSERT INTO ticket_order (order_number,user_id,ticket_id) VALUES ('105',1,3)";
statement.executeUpdate(sql2);
}
}

4.测试方法

 package com.day02.sation.test;

 import com.day02.sation.transaction.Ticket3Service;
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; /**
* Created by Administrator on 12/27.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-myaop.xml")
public class TestTransaction {
@Autowired
private Ticket3Service ticket3Service; @Test
public void testGetList() {
try {
ticket3Service.buyTicket(); } catch (Exception e) {
e.printStackTrace();
}
}
}

5.使用到的jdbc工具类

 package com.day02.sation.util;

 import java.sql.Connection;
import java.sql.DriverManager; /**
* Created by Administrator on 1/8.
*/
public class JdbcUtil {
private static Connection connection = null; static {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station", "root", "admin");
} catch (Exception e) {
e.printStackTrace();
} } public static Connection getConnection() { return connection;
}
}

完成测试吧!

大型运输行业实战_day10_1_自定义事务管理类的更多相关文章

  1. 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理

    1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...

  2. 大型运输行业实战_day12_1_权限管理实现

    1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2 ...

  3. 大型运输行业实战_day11_1_aop理论与aop实际业务操作

    1.aop概述 Spring的AOP:什么叫做AOP:Aspect oritention programming(面向切面编程)什么是切面:看图,业务方法 执行前后.AOP的目的:AOP能够将那些与业 ...

  4. 大型运输行业实战_day14_1_webserivce简单入门

    1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...

  5. 大型运输行业实战_day15_1_全文检索之Lucene

    1.引入 全文检索简介: 非结构化数据又一种叫法叫全文数据.从全文数据(文本)中进行检索就叫全文检索. 2.数据库搜索的弊端 案例 :     select  *  from product  whe ...

  6. 大型运输行业实战_day13_1_定时任务spring-quartz

    1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...

  7. 大型运输行业实战_day01_2_需求文档

    1.文档格式 (见模板文件) 2.Axure简单使用 2.1安装Axure傻瓜式安装 2.2简单使用axure 3.总结 需求文件完成后应该包括三种文件: 1.axure文件 2.axure生成的ht ...

  8. 大型运输行业实战_day01_1_业务分析

    1.业务分析 发展历史:  上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析            图文并茂  ...

  9. 大型运输行业实战_day09_2_站间互售实现

    1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...

随机推荐

  1. WIN7 X64 下 VS2008升级补丁 (显示隐藏按钮)

    原文地址:http://blog.sina.com.cn/s/blog_57b5da120100gk7l.html 更新列表: 2010年3月26日:增加对日文版的支持. 2010年3月3日:更新代码 ...

  2. Hadoop高级培训课程大纲-管理员版

    一.课程概述 本次培训课程主要面向大数据系统管理人员和开发设计人员,基于开源社区大数据应用最活跃的Hadoop和HBase技术框架.围绕分布式文件存储(HDFS).分布式并行计算(Map/Recue) ...

  3. C/C++基础----泛型算法

    算法不依赖与容器(使用迭代器),但大多数依赖于元素类型.如find需要==运算符,其他算法可能要求支持<运算符. 算法永远不会执行容器的操作,永远不会改变底层容器的大小(添加或删除元素). ac ...

  4. 关于office2016桌面新建不显示execl问题

    在百度,google找了很多方法都没有文档可以解决此问题,office2016安装完在新建是由ececl的,应该是我用了清理注册表工具,对execl项进行清理,造成没有execl,所以贴出原版xls, ...

  5. 基于nginx和tengine的tcp反向代理,负载均衡 安装和配置

    先下载nginx_tcp_proxy_module模块. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master ...

  6. 一份CTR的特征工程图

  7. Spring Batch批处理以及编程模型

    1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...

  8. 洛谷P1414 又是毕业季II

    题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...

  9. [Python] numpy.nonzero

    numpy.nonzero(a) Return the indices of the elements that are non-zero. Returns a tuple of arrays, on ...

  10. java开发-问题清单

    本人是做Java开发的,这是我参加58,搜狐,搜狗,新浪微博,百度,腾讯文学,网易以及其他一些小的创业型公司的面试常被问的问题,当然有重复,弄清楚这些 1. junit用法,before,before ...