Spring-tx-TransactionInfo

该类是每层事务的抽象,并且是一个链表结构,一个节点表示一层事务,这链表(这多层事务)对应着同一个TransactionInterceptor和同一个PlatformTransactionManager

由于该类是一个protected修饰的TransactionAspectSupport的内部类,所以如果自己想查看执行事务时候,当前事务的属性,应该通过反射机制调用currentTransactionInfo方法才可以,本文后续会有调试代码

该类是TransactionAspectSupport类的内部类,有下面几个属性

@Nullable
private final PlatformTransactionManager transactionManager;
@Nullable
private final TransactionAttribute transactionAttribute;

private final String joinpointIdentification;
@Nullable
private TransactionStatus transactionStatus;
@Nullable
private TransactionInfo oldTransactionInfo;

其中transactionManagertransactionAttributetransactionStatus,都已经介绍过,本文着重阐述joinpointIdentification和oldTransactionInfo

一:joinpointIdentification
要执行事务的方法的全限定名,字符串形式
二:oldTransactionInfo
表示在哪个事务中开启的事务,比如A事务里有事务B,那么此时就有两个TransactionInfo,后一个TransactionInfo(B)包含着一个前一个TransactionInfo(A)的引用,这个指针用oldTransactionInfo属性来表示

下面是证明oldTransactionInfo过程,不爱看的不看
创建第一个事务类Create1

@Component
public class Creater1 {
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Autowired
	Creater2 creater2;

	@Transactional(rollbackFor = Exception.class)
	public void create() throws Exception {
		jdbcTemplate.update("insert into t1(value) values ('养1只垂耳兔')");
		Class<TransactionAspectSupport> clazz = TransactionAspectSupport.class;
		Method m = clazz.getDeclaredMethod("currentTransactionInfo");
		m.setAccessible(true);
		Object o = m.invoke(clazz);
		System.out.println(o);// 将断点打到这里观察oldTransactionInfo属性
		creater2.create();
	}
}

创建第二个事务类Create2,用来在Create1中嵌套

@Component
public class Creater2 {
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void create() throws Exception {
		jdbcTemplate.update("insert into t1(value) values ('养2只垂耳兔')");
		Class<TransactionAspectSupport> clazz = TransactionAspectSupport.class;
		Method m = clazz.getDeclaredMethod("currentTransactionInfo");
		m.setAccessible(true);
		Object o = m.invoke(clazz);// 将断点打到这里观察oldTransactionInfo属性
		System.out.println(o);
	}
}

创建Main方法,并且运行,查看断点处,可发现oldTransactionInfo的值,正好一个事务对应一个info

@Configuration
@EnableTransactionManagement
public class TestMain {

	@Bean
	public DataSource dataSource() {
		HikariDataSource ds = new HikariDataSource();
		ds.setJdbcUrl("jdbc:mysql://localhost:3306/swttest?serverTimezone=UTC&&useSSL=false");
		ds.setUsername("root");
		ds.setPassword("123456");
		return ds;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		JdbcTemplate jdbc = new JdbcTemplate();
		jdbc.setDataSource(dataSource());
		return jdbc;
	}

	@Bean
	public PlatformTransactionManager platformTransactionManager() {
		DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource());
		return manager;
	}

	public static void main(String[] args) throws Throwable {
		// 依赖容器的话必须要开启@EnableTransactionManagement注解,表示告诉spring我要使用事务
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
				"基础包名");
		Creater1 tt = ctx.getBean(Creater1.class);
		tt.create();
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值