class TrimService {

    public void trimDerivedTables(int height, boolean async) {
        log.debug("TRIM: trimDerivedTables on height={}, async={}", height, async);
        TransactionalDataSource dataSource = dbManager.getDataSource();
        boolean inTransaction = dataSource.isInTransaction();
        lock.lock();
        try {
            try {
                if (!inTransaction) {
                    dataSource.begin();
                }
                long startTime = System.currentTimeMillis();
                doTrimDerivedTablesOnBlockchainHeight(height, async);
                dataSource.commit(!inTransaction);
                log.info("Total trim time: {} ms on height '{}', InTr?=('{}')",
                    (System.currentTimeMillis() - startTime), height, inTransaction);
            } catch (Exception e) {
                log.warn(e.toString(), e);
                dataSource.rollback(!inTransaction);
                throw e;
            }
        } finally {
            lock.unlock();
        }
    }

    @Inject public  TrimService(DatabaseManager databaseManager,
                       DerivedTablesRegistry derivedDbTablesRegistry,
                       GlobalSync globalSync,
                       TimeService timeService,
                       Event<TrimData> trimEvent,
                       Event<TrimConfig> trimConfigEvent,
                       TrimDao trimDao,
                       @Property(value = "apl.maxRollback", defaultValue = "720") int maxRollback
    );

}