package cn.gsgsoft.ldtm.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class AbstractInitiateService4NotActor {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	AbstractNestInitiateService nestInitiateService;
	
	/**
	 * 一个最简单的保存，它是一个ldtm事务，但没有分布式事务的参与者和嵌套事务
	 * @throws Throwable 
	 * 
	 */
	public void save(String name) throws Throwable{
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
	}
	
	/**
	 * 一个最简单的保存，它是一个ldtm事务，但没有分布式事务的参与者和嵌套事务
	 * @throws Throwable 
	 * 
	 */
	public void saveRollback(String name) throws Throwable{
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
		throw new RuntimeException("测试异常");
	}
	
	/**
	 * 嵌套测试 ts-ldtm
	 */
	public void tsNestLDTMSave(String name){
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
		nestInitiateService.save(name+"_nest");
	}
	
	/**
	 * 嵌套回滚测试 ts-ldtm
	 */
	public void tsNestLDTMSaveRollback(String name){
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
		nestInitiateService.save(name+"_nest");
		throw new RuntimeException("测试异常");
	}
	
	
	/**
	 * 嵌套测试 ldtm-ts
	 */
	public void ldtmNestNsSave(String name){
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
		nestInitiateService.save(name+"_nest");
	}
	

	/**
	 * 嵌套回滚测试 ldtm-ts
	 */
	public void ldtmNestNsSaveRollback(String name){
		jdbcTemplate.execute("insert into initiate (name)values('"+name+"')");
		nestInitiateService.save(name+"_nest");
		throw new RuntimeException("测试异常");
	}
}
