@Override
    public void batchWriteBegin() throws InterruptedException, JournalException {
        if (currentTransaction != null) {
            throw new JournalException(String.format(
                    "failed to begin batch write because has running txn = %s", currentTransaction));
        }

        JournalException exception = null;
        for (int i = 0; i < RETRY_TIME; i++) {
            try {
                // sleep before retry
                if (i != 0) {
                    Thread.sleep(SLEEP_INTERVAL_SEC * 1000L);
                }

                currentTransaction = currentJournalDB.getDb().getEnvironment().beginTransaction(
                        null, bdbEnvironment.getTxnConfig());
                return;
            } catch (DatabaseException e) {
                String errMsg = String.format("failed to begin txn after retried %d times! db = %s",
                        i + 1, currentJournalDB);
                LOG.error(errMsg, e);
                exception = new JournalException(errMsg);
                exception.initCause(e);
            }
        }
        // failed after retried
        throw exception;
    }
@Test
    public void testBatchWriteBeginRetry(
            @Mocked CloseSafeDatabase database,
            @Mocked BDBEnvironment environment,
            @Mocked Database rawDatabase,
            @Mocked Environment rawEnvironment,
            @Mocked Transaction txn) throws Exception {
        BDBJEJournal journal = new BDBJEJournal(environment, database);
        String data = "petals on a wet black bough";
        DataOutputBuffer buffer = new DataOutputBuffer();
        Text.writeString(buffer, data);

        new Expectations(rawDatabase) {
            {
                rawDatabase.getEnvironment();
                minTimes = 0;
                result = rawEnvironment;
            }
        };
        new Expectations(rawEnvironment) {
            {
                rawEnvironment.beginTransaction(null, (TransactionConfig) any);
                times = 2;
                result = new DatabaseNotFoundException("mock mock");
                result = txn;
            }
        };
        journal.batchWriteBegin();
    }