package com.hzya.frame.web.transactiontemplate;

import com.hzya.frame.datasource.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-15 10:22
 * @Version 1.0
 */
public class DataSourceTransactionManagerUtil {

    private static DataSourceTransactionManager dataSourceTransactionManager;
    private static DefaultTransactionDefinition txDefinition;

    //public static TransactionStatus defaultBegin() {
    //    dataSourceTransactionManager = DataSourceUtil.getDataSourceTransactionMaManager("default_db");
    //    txDefinition = new DefaultTransactionDefinition();
    //    // 如果当前没有事物，则新建一个事物；如果已经存在一个事物，则加入到这个事物中。
    //    txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    //    TransactionStatus transaction = dataSourceTransactionManager.getTransaction(txDefinition);
    //    return transaction;
    //}

    // 开启事务
    //public static TransactionStatus begin(String dataSourceKey) {
    //    dataSourceTransactionManager = DataSourceUtil.getDataSourceTransactionMaManager(dataSourceKey);
    //    txDefinition = new DefaultTransactionDefinition();
    //    // 如果当前没有事物，则新建一个事物；如果已经存在一个事物，则加入到这个事物中。
    //    txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    //    TransactionStatus transaction = dataSourceTransactionManager.getTransaction(txDefinition);
    //    return transaction;
    //}

    // 提交事务
    //public static void commit(TransactionStatus transactionStatus) {
    //    dataSourceTransactionManager.commit(transactionStatus);
    //}

    // 回滚事务
    //public static void rollback(TransactionStatus transactionStatus) {
    //    dataSourceTransactionManager.rollback(transactionStatus);
    //}

}
