1.创建事务管理类 TransactionManager.java
1 package com.day02.sation.transaction; 2 3 import com.day02.sation.util.JdbcUtil; 4 5 import java.sql.SQLException; 6 7 /** 8 * Created by Administrator on 1/8. 9 */ 10 public class TransactionManager { 11 /** 12 * 关闭事务自动提交 13 */ 14 public void before1() { 15 System.out.println("-----before-----"); 16 try { 17 JdbcUtil.getConnection().setAutoCommit(false); 18 } catch (SQLException e) { 19 e.printStackTrace(); 20 } 21 } 22 23 /** 24 * 手动提交事务 25 */ 26 public void after2() { 27 System.out.println("-----after-----"); 28 try { 29 JdbcUtil.getConnection().commit(); 30 } catch (SQLException e) { 31 e.printStackTrace(); 32 } 33 } 34 35 /** 36 * 事务回滚 37 */ 38 public void rollback() { 39 System.out.println("-----rollback-----"); 40 try { 41 JdbcUtil.getConnection().rollback(); 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 } 46 }
2.创建配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop 6 http://www.springframework.org/schema/aop/spring-aop.xsd"> 7 <!--需要被管理的业务类--> 8 <bean id="ticket3Service" class="com.day02.sation.transaction.Ticket3Service"/> 9 <!--事务管理器--> 10 <bean id="txManager" class="com.day02.sation.transaction.TransactionManager"/> 11 <!-- aop配置--> 12 <aop:config> 13 <!-- 配置切面--> 14 <aop:aspect ref="txManager"> 15 <!--地点:方法--> 16 <aop:pointcut id="txPoint" expression=" execution(* com.day02.sation.transaction.*Service.*(..) )"/> 17 <!--时间--> 18 <!-- 方法执行前--> 19 <aop:before method="before1" pointcut-ref="txPoint"/> 20 <!--方法执行后--> 21 <aop:after-returning method="after2" pointcut-ref="txPoint"/> 22 <!--抛出异常时--> 23 <aop:after-throwing method="rollback" pointcut-ref="txPoint"/> 24 </aop:aspect> 25 </aop:config> 26 </beans>
3.需要被管理的业务类 Ticket3Service.java
1 package com.day02.sation.transaction; 2 3 import com.day02.sation.util.JdbcUtil; 4 5 import java.sql.Connection; 6 import java.sql.Statement; 7 8 /** 9 * Created by Administrator on 1/8. 10 */ 11 public class Ticket3Service { 12 public void buyTicket() throws Exception { 13 Connection connection = JdbcUtil.getConnection(); 14 Statement statement = connection.createStatement(); 15 //关闭自动提交数据 16 String sql = " UPDATE ticket SET standby=14 WHERE id=3 "; 17 int i = statement.executeUpdate(sql); 18 System.out.println("===" + (1 / 0)); 19 //新增订单 20 String sql2 = "INSERT INTO ticket_order (order_number,user_id,ticket_id) VALUES ('105',1,3)"; 21 statement.executeUpdate(sql2); 22 } 23 }
4.测试方法
1 package com.day02.sation.test; 2 3 import com.day02.sation.transaction.Ticket3Service; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 /** 11 * Created by Administrator on 12/27. 12 */ 13 @RunWith(SpringJUnit4ClassRunner.class) 14 @ContextConfiguration("classpath:spring/spring-myaop.xml") 15 public class TestTransaction { 16 @Autowired 17 private Ticket3Service ticket3Service; 18 19 @Test 20 public void testGetList() { 21 try { 22 ticket3Service.buyTicket(); 23 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 }
5.使用到的jdbc工具类
1 package com.day02.sation.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 6 /** 7 * Created by Administrator on 1/8. 8 */ 9 public class JdbcUtil { 10 private static Connection connection = null; 11 12 static { 13 try { 14 Class.forName("com.mysql.jdbc.Driver"); 15 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station", "root", "admin"); 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } 19 20 } 21 22 public static Connection getConnection() { 23 24 return connection; 25 } 26 }
完成测试吧!