public long beginTransaction(long dbId, List<Long> tableIdList, String label, TUniqueId requestId,
                                 TxnCoordinator coordinator, LoadJobSourceType sourceType, long listenerId,
                                 long timeoutSecond,
                                 long warehouseId)
            throws LabelAlreadyUsedException, RunningTxnExceedException, DuplicatedRequestException, AnalysisException {

        if (Config.disable_load_job) {
            throw ErrorReportException.report(ErrorCode.ERR_BEGIN_TXN_FAILED,
                    "disable_load_job is set to true, all load jobs are rejected");
        }

        if (Config.metadata_enable_recovery_mode) {
            throw ErrorReportException.report(ErrorCode.ERR_BEGIN_TXN_FAILED,
                    "The cluster is under recovery mode, all load jobs are rejected");
        }

        if (GlobalStateMgr.getCurrentState().isSafeMode()) {
            throw ErrorReportException.report(ErrorCode.ERR_BEGIN_TXN_FAILED,
                    "The cluster is under safe mode state, all load jobs are rejected.");
        }

        switch (sourceType) {
            case BACKEND_STREAMING:
                checkValidTimeoutSecond(timeoutSecond, Config.max_stream_load_timeout_second, Config.min_load_timeout_second);
                break;
            case LAKE_COMPACTION:
            case REPLICATION:
                // skip transaction timeout range check for lake compaction and replication
                break;
            default:
                checkValidTimeoutSecond(timeoutSecond, Config.max_load_timeout_second, Config.min_load_timeout_second);
        }

        DatabaseTransactionMgr dbTransactionMgr = getDatabaseTransactionMgr(dbId);
        return dbTransactionMgr.beginTransaction(tableIdList, label, requestId, coordinator, sourceType, listenerId,
                timeoutSecond, warehouseId);
    }
@Test
    public void testBeginTransactionFailed() {
        Config.disable_load_job = true;
        boolean exceptionThrown = false;
        try {
            masterTransMgr.beginTransaction(1L, null, "xxx", null, null, LoadJobSourceType.FRONTEND, 1L, 1000L);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ErrorReportException);
            Assert.assertEquals(ErrorCode.ERR_BEGIN_TXN_FAILED, ((ErrorReportException) e).getErrorCode());
            exceptionThrown = true;
        } finally {
            Config.disable_load_job = false;
        }
        Assert.assertTrue(exceptionThrown);

        Config.metadata_enable_recovery_mode = true;
        exceptionThrown = false;
        try {
            masterTransMgr.beginTransaction(1L, null, "xxx", null, null, LoadJobSourceType.FRONTEND, 1L, 1000L);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ErrorReportException);
            Assert.assertEquals(ErrorCode.ERR_BEGIN_TXN_FAILED, ((ErrorReportException) e).getErrorCode());
            exceptionThrown = true;
        } finally {
            Config.metadata_enable_recovery_mode = false;
        }
        Assert.assertTrue(exceptionThrown);

        GlobalStateMgr.getCurrentState().setSafeMode(true);
        exceptionThrown = false;
        try {
            masterTransMgr.beginTransaction(1L, null, "xxx", null, null, LoadJobSourceType.FRONTEND, 1L, 1000L);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ErrorReportException);
            Assert.assertEquals(ErrorCode.ERR_BEGIN_TXN_FAILED, ((ErrorReportException) e).getErrorCode());
            exceptionThrown = true;
        } finally {
            GlobalStateMgr.getCurrentState().setSafeMode(false);
        }
        Assert.assertTrue(exceptionThrown);
    }