spring 事务实现方式有哪些?

spring 事务实现方式有哪些?

  • 编程式事务管理,在代码中调用 commit()、rollback()等事务管理相关的方法

maven pom.xml文件


	org.springframework
	spring-beans
	4.2.4.RELEASE



	org.springframework
	spring-context
	4.2.4.RELEASE



	org.springframework
	spring-aop
	4.2.4.RELEASE



	org.springframework
	spring-tx
	4.2.4.RELEASE



	org.springframework
	spring-jdbc
	4.2.4.RELEASE




	mysql
	mysql-connector-java
	5.1.18

编程式事务管理,可以通过 java.sql.Connection 控制事务。spring 配置文件



        
	

	
		
		
			jdbc:mysql://localhost:3306/test
		
		
			root
		
		
			root
		
	
	

测试代码

package constxiong.interview.transaction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {

	public static void main(String[] args) throws Exception {
		testManualTransaction();//测试函数式控制事务
	}
	
	private static void testManualTransaction() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml");
		DataSource ds = (DataSource)context.getBean("datasource");
		Connection conn = ds.getConnection();
		try {
			initTable(conn);//初始化表
			conn.setAutoCommit(false);//设置不自动提交事务
			queryUsers(conn);//查询打印用户表
			deleteUser(conn);//删除 id=1 用户
			conn.rollback();//回滚
			queryUsers(conn);//查询打印用户表
		} finally {
			conn.close();
		}
	}

	private static void initTable(Connection conn) throws SQLException {
		conn.createStatement().execute("drop table if exists user");
		conn.createStatement().execute("create table user(id int, username varchar(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ");//是否支持事务与数据库引擎有关,此处删除 ENGINE=InnoDB DEFAULT CHARSET=utf8 可能不支持事务
		conn.createStatement().execute("insert into user values(1, 'user1')");
		conn.createStatement().execute("insert into user values(2, 'user2')");
	}

	private static void deleteUser(Connection conn) throws SQLException {
		conn.createStatement().execute("delete from user where id = 1");
	}

	private static void queryUsers(Connection conn) throws SQLException {
		Statement st = conn.createStatement();
		st.execute("select * from user");
		ResultSet rs = st.getResultSet();
		while (rs.next()) {
			System.out.print(rs.getString("id"));
			System.out.print(" ");
			System.out.print(rs.getString("username"));
			System.out.println();
		}
	}
	
}

删除用户语句回滚,打印出两个用户

1 user1
2 user2
1 user1
2 user2

 

  • 基于 TransactionProxyFactoryBean 的声明式事务管理

新增 UserDao 接口

package constxiong.interview.transaction;

import java.util.List;
import java.util.Map;

public interface UserDao {

	/**
	 * 查询用户
	 * @return
	 */
	public List> getUsers();
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	public int deleteUser(int id);
	
}

新增 UserDao 实现

package constxiong.interview.transaction;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
	
	/**
	 * 查询用户
	 * @return
	 */
	public List> getUsers() {
		String sql = "select * from user";
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	public int deleteUser(int id){
		String sql = "delete from user where id = " + id;
		int result = this.getJdbcTemplate().update(sql);
		if (id == 1) {
			throw new RuntimeException();
		}
		return result;
	}
}

修改 spring 配置文件,添加事务管理器 DataSourceTransactionManager 和事务代理类 TransactionProxyFactoryBean



        
	
	
	
		
		
			jdbc:mysql://localhost:3306/test
		
		
			root
		
		
			root
		
	
	
	
		
	
	
	
	
		
	
	
	
		
		
		
			
				
				
				-java.lang.RuntimeException
			
		
	
	

测试代码

package constxiong.interview.transaction;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {

	static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml");
	
	public static void main(String[] args) throws Exception {
		testUseTransactionProxy(); //测试使用 spring TransactionProxyFactoryBean
	}
	
	private static void testUseTransactionProxy() {
		final UserDao userDao = (UserDao)context.getBean("userProxy");
		printUsers(userDao);//打印用户
		userDao.deleteUser(1);//删除 id=1 用户
	}

	private static void printUsers(UserDao userDao) {
		for (Map user : userDao.getUsers()) {
			System.out.println(user);
		}
	}

}

 结果输出

{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
	at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
	at com.sun.proxy.$Proxy3.deleteUser(Unknown Source)
	at constxiong.interview.transaction.TransactionTest.testUseTransactionProxy(TransactionTest.java:32)
	at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)

spring 事务实现方式有哪些?_第1张图片

 

  • 基于注解 @Transactional 的声明式事务管理

UserDaoImpl 删除用户方法添加注解 @Transactional(rollbackFor=RuntimeException.class) 出现 RuntimeException 回滚

package constxiong.interview.transaction;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Transactional;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
	
	/**
	 * 查询用户
	 * @return
	 */
	public List> getUsers() {
		String sql = "select * from user";
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@Transactional(rollbackFor=RuntimeException.class)
	public int deleteUser(int id){
		String sql = "delete from user where id = " + id;
		int result = this.getJdbcTemplate().update(sql);
		if (id == 1) {
			throw new RuntimeException();
		}
		return result;
	}
}

修改 spring 配置文件,开启 spring 的事务注解能力



        
	
	
	
		
		
			jdbc:mysql://localhost:3306/test
		
		
			root
		
		
			root
		
	
	
	
		
	
	
	
	
		
	
	
	
	
	

测试代码

package constxiong.interview.transaction;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {

	static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml");
	
	public static void main(String[] args) throws Exception {
		testAnnotationTransaction();
	}
	
	
	private static void testAnnotationTransaction() {
		UserDao userDao = (UserDao)context.getBean("userDao");
		printUsers(userDao);
		userDao.deleteUser(1);
	}


	private static void printUsers(UserDao userDao) {
		for (Map user : userDao.getUsers()) {
			System.out.println(user);
		}
	}

}

输出结果

{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
	at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:30)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
	at com.sun.proxy.$Proxy5.deleteUser(Unknown Source)
	at constxiong.interview.transaction.TransactionTest.testAnnotationTransaction(TransactionTest.java:20)
	at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)

 spring 事务实现方式有哪些?_第2张图片

  • 基于 Aspectj AOP 配置(注解)事务

maven pom.xml 添加 Aspectj 的支持


    org.aspectj
    aspectjweaver
    1.8.13

去除 UserDaoImpl 注解@Transactional(rollbackFor=RuntimeException.class)

package constxiong.interview.transaction;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
	
	/**
	 * 查询用户
	 * @return
	 */
	public List> getUsers() {
		String sql = "select * from user";
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	public int deleteUser(int id){
		String sql = "delete from user where id = " + id;
		int result = this.getJdbcTemplate().update(sql);
		if (id == 1) {
			throw new RuntimeException();
		}
		return result;
	}
}

修改 spring 配置文件,织入切面



        
	
	
	
		
		
			jdbc:mysql://localhost:3306/test
		
		
			root
		
		
			root
		
	
	
	
		
	
	
	
	
		
	
	
	
		
			
			
		
	
	
	
		
		
		
	
	

测试代码

package constxiong.interview.transaction;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {

	static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml");
	
	public static void main(String[] args) throws Exception {
		testAspectjTransaction();
	}
	
	
	private static void testAspectjTransaction() {
		UserDao userDao = (UserDao)context.getBean("userDao");
		printUsers(userDao);
		userDao.deleteUser(1);
	}


	private static void printUsers(UserDao userDao) {
		for (Map user : userDao.getUsers()) {
			System.out.println(user);
		}
	}

}

输出结果

{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
	at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
	at com.sun.proxy.$Proxy2.deleteUser(Unknown Source)
	at constxiong.interview.transaction.TransactionTest.testAnnotationTransaction(TransactionTest.java:20)
	at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)

spring 事务实现方式有哪些?_第3张图片

 

PS:

这篇仅用事务回滚为例,了解 spring 事务控制,还需要关注数据库的ACID四种特性、事务传播特性、事务的隔离级别(脏读、不可重复读、幻读)。

详细可参考:https://blog.csdn.net/chinacr07/article/details/78817449

spring 事务的源码学习可以参考:

https://my.oschina.net/zhangxufeng/blog/1935556

https://my.oschina.net/zhangxufeng/blog/1943983

https://my.oschina.net/zhangxufeng/blog/1973493

 


【Java面试题与答案】整理推荐

  • 基础与语法
  • 集合
  • 网络编程
  • 并发编程
  • Web
  • 安全
  • 设计模式
  • 框架
  • 算法与数据结构
  • 异常
  • 文件解析与生成
  • Linux
  • MySQL
  • Oracle
  • Redis
  • Dubbo

 

你可能感兴趣的:(面试,自做,Java,面试题)