package cn.com.capcloud.api.action.myself.system;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * 测试事务 执行SQL异常回滚
 */
@Service
public class TestTransactionService {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Transactional // 使用@Transactional注解声明启用事务
    public void updateTableData(){
        String dataSource;//数据来源:form：系统填报、 excel：导入
        dataSource =jdbcTemplate.queryForObject("select data_source from tbl_data_test_new_features where id=1",String.class);
        System.out.println("原始值："+dataSource);

        if(dataSource.equals("form")){
            dataSource="excel";
        }else{
            dataSource="form";
        }
        jdbcTemplate.execute("update tbl_data_test_new_features set data_source='"+dataSource+"' where id=1");
        dataSource =jdbcTemplate.queryForObject("select data_source from tbl_data_test_new_features where id=1",String.class);
        System.out.println("变更后："+dataSource);

        System.out.println("模拟错误的SQL让其回滚（update tbl_data_test_new_features set ccccc=3 where id=1）");
        jdbcTemplate.execute("update tbl_data_test_new_features set ccccc=3 where id=1");

    }
}
