大型运输行业实战_day10_1_自定义事务管理类
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_自定义事务管理类的更多相关文章
- 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理
1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...
- 大型运输行业实战_day12_1_权限管理实现
1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2 ...
- 大型运输行业实战_day11_1_aop理论与aop实际业务操作
1.aop概述 Spring的AOP:什么叫做AOP:Aspect oritention programming(面向切面编程)什么是切面:看图,业务方法 执行前后.AOP的目的:AOP能够将那些与业 ...
- 大型运输行业实战_day14_1_webserivce简单入门
1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...
- 大型运输行业实战_day15_1_全文检索之Lucene
1.引入 全文检索简介: 非结构化数据又一种叫法叫全文数据.从全文数据(文本)中进行检索就叫全文检索. 2.数据库搜索的弊端 案例 : select * from product whe ...
- 大型运输行业实战_day13_1_定时任务spring-quartz
1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...
- 大型运输行业实战_day01_2_需求文档
1.文档格式 (见模板文件) 2.Axure简单使用 2.1安装Axure傻瓜式安装 2.2简单使用axure 3.总结 需求文件完成后应该包括三种文件: 1.axure文件 2.axure生成的ht ...
- 大型运输行业实战_day01_1_业务分析
1.业务分析 发展历史: 上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析 图文并茂 ...
- 大型运输行业实战_day09_2_站间互售实现
1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...
随机推荐
- MySQL的Join使用
在MySQL(以5.1为例)中,表连接的语法可以参见MySQL官方手册:MySQL官方手册-JOIN 在查询中,连接的语法类似 SELECT select_expr FROM table_refere ...
- 杂项:mPaaS
ylbtech-杂项:mPaaS 1. 概述返回顶部 mPaaS 是源于支付宝 App 的移动开发平台,为移动开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛.减少研发成本.提升开 ...
- [转]Outlook HTML渲染
转自:http://www.cnblogs.com/dolphinX/p/4081828.html 是不是很讨厌为Email代码兼容Outlook? 太遗憾了!虽然光都有尽头,但Outlook始终存在 ...
- vSphere client 登陆ESXI主机“您无权登录次服务器”
vCenter安装在虚拟机上,安装好后想调整下内存,直接把虚拟机关闭了电源,突然一想服务器都被我关了,还拿什么修改内存,完蛋! 突然想起,在使用vCenter之前,都是用vsphere client ...
- (转!)大话websocket
邪正看眼鼻,真假看嘴唇,功名看气概,富贵看精神. ---曾国藩<冰鉴> 转自https://www.cnblogs.com/fuqiang88/p/5956363.html 原文http: ...
- innerHTML与jquery里的html()区别介绍
我原本一直以为innerHTML和jquery里的html其实是完全一样的,jquery是多此一举了,直到我遇到一次问题 看个示例: 复制代码 代码如下: var tbody=document.c ...
- django路由初识
静态文件配置 1.项目下面新建一个文件夹static settings.py中最后添加 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static ...
- Spring AOP的总结
- python list unicode转中文显示
[u'\u773c', u'\u8179\u90e8', u'\u4e94\u5b98', u'\u53e3\u8154', u'\u8179\u90e8', u'\u53e3\u8154'] str ...
- Java的this和super总结
内容: 1.this和super作用 2.继承关系图 1.this和super作用 this和super的作用: this:区分本类中的成员变量和局部变量同名的情况,代指本类 super:区分子类中的 ...