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. JSP+JavaBean+Servlet技术(MVC模型)

    一,Servlet开发用户在浏览器中输入一个网址并回车,浏览器会向服务器发送一个HTTP请求.服务器端程序接受这个请求,并对请求进行处理,然后发送一个回应.浏览器收到回应,再把回应的内容显示出来.这种 ...

  2. LwIP raw api下使用tcp keep alive

    // The following code is implemented after tcp_new() or in tcp_connected call back... xxx_connected( ...

  3. 垃圾收集器之:CMS收集器

    HotSpot JVM的并发标记清理收集器(CMS收集器)的主要目标就是:低应用停顿时间.该目标对于大多数交互式应用很重要,比如web应用.在我们看一下有关JVM的参数之前,让我们简要回顾CMS收集器 ...

  4. shell 11函数

    函数定义 function 方法名(){ command return int; } 注意:function可加可不加 #shell #!/bin/sh function fun1(){ echo & ...

  5. 查找占用CPU高线程

    1.根据进程号查看线程 ps -mp pid -o THREAD,tid,time 2 把tid值转成16进制 printf "%x\n" tid 3.根据上面获取到的16进制数据 ...

  6. [UE4]C++ getter and setter

    问:以前面向对象没学好.... 最近老是在想,既然要设为private为什么还要写个setter来改变它的值呢? 为什么不直接把它直接设成public倒省事? 呵呵,谢啦 答:用setter来改变数据 ...

  7. Maven 配置tomcat插件

    使用tomcat插件来访问maven 1 先下载tomcat插件(在pom中配置) <!-- 配置Tomcat插件 --> <plugin> <groupId>or ...

  8. Hive基础之Hive的存储类型

    Hive常用的存储类型有: 1.TextFile: Hive默认的存储类型:文件大占用空间大,未压缩,查询慢: 2.Sequence File:将属于以<KEY,VALUE>的形式序列化到 ...

  9. Spark分析之Job Scheduling Process

    经过前面文章的SparkContext.DAGScheduler.TaskScheduler分析,再从总体上了解Spark Job的调度流程 1.SparkContext将job的RDD DAG图提交 ...

  10. django从请求到响应的过程深入讲解

    django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用django自带的的we ...