All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.quickdao.module.database.transaction.kit.TransactionInvocationHandler Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
package cn.schoolwow.quickdao.module.database.transaction.kit;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.exception.SQLRuntimeException;
import cn.schoolwow.quickdao.module.database.dml.kit.DatabaseManipulationImpl;
import cn.schoolwow.quickdao.module.query.kit.QueryOperation;
import cn.schoolwow.quickdao.module.query.kit.QueryOperationImpl;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 事务操作代理模式对象
 */
public class TransactionInvocationHandler implements InvocationHandler {
    private Connection transactionConnection;
    private QuickFlow quickFlow;
    private QuickDAOConfig quickDAOConfig;
    private QueryOperation queryOperation;
    private TransactionOperation transactionOperation;

    public TransactionInvocationHandler(QuickDAOConfig quickDAOConfig) {
        try {
            this.quickFlow = setTransactionConnection(quickDAOConfig);
            this.quickDAOConfig = quickDAOConfig;
            this.queryOperation = new QueryOperationImpl(quickFlow);
            this.transactionOperation = new TransactionOperationImpl(transactionConnection);
        }catch (SQLException e){
            throw new SQLRuntimeException(e);
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String interfaceName = method.getDeclaringClass().getSimpleName();
        switch (interfaceName) {
            case "Closeable":
            case "TransactionOperation": {
                return method.invoke(transactionOperation, args);
            }
            case "Object":
            case "DatabaseDAO":
            case "DatabaseManipulation": {
                return method.invoke(new DatabaseManipulationImpl(quickFlow, quickDAOConfig), args);
            }
            case "QueryOperation":{
                return method.invoke(queryOperation, args);
            }
            default: {
                throw new IllegalArgumentException("不支持的方法调用!接口名:"+interfaceName+",方法:" + method);
            }
        }
    }

    private QuickFlow setTransactionConnection(QuickDAOConfig quickDAOConfig) throws SQLException {
        transactionConnection = quickDAOConfig.databaseContext.dataSource.getConnection();
        transactionConnection.setAutoCommit(false);

        QuickFlow quickFlow = QuickFlowBuilder.newInstance()
                .printTrace(quickDAOConfig.logRecordOption.recordFlowLog)
                .putContextData("transactionConnection", transactionConnection)
                .putContextData("quickDAOConfig", quickDAOConfig)
                .putContextData("databaseType", quickDAOConfig.databaseContext.databaseProvider.getDatabaseType())
                .putContextData("databaseProvider", quickDAOConfig.databaseContext.databaseProvider);
        return quickFlow;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy