@Override public Object execute() throws Throwable { return DBRouterUntyped.withRODBIAllowed(true, callback); } });
public EntitySqlDaoTransactionalJdbiWrapper(final IDBI dbi, final IDBI roDbi, final Clock clock, final CacheControllerDispatcher cacheControllerDispatcher, final NonEntityDao nonEntityDao, final InternalCallContextFactory internalCallContextFactory) { this.clock = clock; this.cacheControllerDispatcher = cacheControllerDispatcher; this.nonEntityDao = nonEntityDao; this.internalCallContextFactory = internalCallContextFactory; this.dbRouter = new DBRouterUntyped(dbi, roDbi); }
boolean shouldUseRODBI(final boolean requestedRO) { if (requestedRO) { if (isRODBIAllowed()) { logger.debug("Using RO DBI"); return true; } else { // Redirect to the rw instance, to work-around any replication delay logger.debug("RO DBI requested, but thread state is {}, using RW DBI", getCurrentState()); return false; } } else { // Disable RO DBI for future calls in this thread disallowRODBI(); logger.debug("Using RW DBI"); return false; } }
private boolean isRODBIAllowed() { return getCurrentState() == RO_ALLOWED; }
/** * @param <ReturnType> object type to return from the transaction * @param requestedRO hint as whether to use the read-only connection * @param entitySqlDaoTransactionWrapper transaction to execute * @return result from the transaction fo type ReturnType */ public <ReturnType> ReturnType execute(final boolean requestedRO, final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) { final String debugInfo = logger.isDebugEnabled() ? getDebugInfo() : null; final Handle handle = dbRouter.getHandle(requestedRO); logger.debug("DBI handle created, transaction: {}", debugInfo); try { final EntitySqlDao<EntityModelDao<Entity>, Entity> entitySqlDao = handle.attach(InitialEntitySqlDao.class); // The transaction isolation level is now set at the pool level: this avoids 3 roundtrips for each transaction // Note that if the pool isn't used (tests or PostgreSQL), the transaction level will depend on the DB configuration //return entitySqlDao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper)); logger.debug("Starting transaction {}", debugInfo); final ReturnType returnType = entitySqlDao.inTransaction(new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper)); logger.debug("Exiting transaction {}, returning {}", debugInfo, returnType); return returnType; } finally { handle.close(); logger.debug("DBI handle closed, transaction: {}", debugInfo); } }
public Handle getHandle(final boolean requestedRO) { if (shouldUseRODBI(requestedRO)) { return roDbi.open(); } else { return dbi.open(); } }
public <M extends EntityModelDao<E>, E extends Entity, T extends EntitySqlDao<M, E>> T onDemandForStreamingResults(final Class<T> sqlObjectType) { return dbRouter.onDemand(true, sqlObjectType); }
public static Object withRODBIAllowed(final boolean allowRODBI, final WithProfilingCallback<Object, Throwable> callback) throws Throwable { final THREAD_STATE currentState = getCurrentState(); CURRENT_THREAD_STATE.set(allowRODBI ? RO_ALLOWED : RW_ONLY); try { return callback.execute(); } finally { CURRENT_THREAD_STATE.set(currentState); } }
public void doROCall(final TenantContext tenantContext) { dbRouter.getHandle(true); }
public <T> T inTransaction(final boolean requestedRO, final TransactionCallback<T> callback) { if (shouldUseRODBI(requestedRO)) { return roDbi.inTransaction(callback); } else { return dbi.inTransaction(callback); } }
public <M extends EntityModelDao<E>, E extends Entity, T extends EntitySqlDao<M, E>> T onDemandForStreamingResults(final Class<T> sqlObjectType) { return dbRouter.onDemand(true, sqlObjectType); }
DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() { @Override DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() { @Override
boolean shouldUseRODBI(final boolean requestedRO) { if (requestedRO) { if (isRODBIAllowed()) { logger.debug("Using RO DBI"); return true; } else { // Redirect to the rw instance, to work-around any replication delay logger.debug("RO DBI requested, but thread state is {}, using RW DBI", getCurrentState()); return false; } } else { // Disable RO DBI for future calls in this thread disallowRODBI(); logger.debug("Using RW DBI"); return false; } }
private boolean isRODBIAllowed() { return getCurrentState() == RO_ALLOWED; }
public void doRWCall(final CallContext callContext) { dbRouter.getHandle(false); }
public EntitySqlDaoTransactionalJdbiWrapper(final IDBI dbi, final IDBI roDbi, final Clock clock, final CacheControllerDispatcher cacheControllerDispatcher, final NonEntityDao nonEntityDao, final InternalCallContextFactory internalCallContextFactory) { this.clock = clock; this.cacheControllerDispatcher = cacheControllerDispatcher; this.nonEntityDao = nonEntityDao; this.internalCallContextFactory = internalCallContextFactory; this.dbRouter = new DBRouterUntyped(dbi, roDbi); }
public <T> T onDemand(final boolean requestedRO, final Class<T> sqlObjectType) { if (shouldUseRODBI(requestedRO)) { return roDbi.onDemand(sqlObjectType); } else { return dbi.onDemand(sqlObjectType); } }
@Override public Object execute() throws Throwable { return DBRouterUntyped.withRODBIAllowed(true, callback); } });
public static Object withRODBIAllowed(final boolean allowRODBI, final WithProfilingCallback<Object, Throwable> callback) throws Throwable { final THREAD_STATE currentState = getCurrentState(); CURRENT_THREAD_STATE.set(allowRODBI ? RO_ALLOWED : RW_ONLY); try { return callback.execute(); } finally { CURRENT_THREAD_STATE.set(currentState); } }
/** * @param <ReturnType> object type to return from the transaction * @param requestedRO hint as whether to use the read-only connection * @param entitySqlDaoTransactionWrapper transaction to execute * @return result from the transaction fo type ReturnType */ public <ReturnType> ReturnType execute(final boolean requestedRO, final EntitySqlDaoTransactionWrapper<ReturnType> entitySqlDaoTransactionWrapper) { final String debugInfo = logger.isDebugEnabled() ? getDebugInfo() : null; final Handle handle = dbRouter.getHandle(requestedRO); logger.debug("DBI handle created, transaction: {}", debugInfo); try { final EntitySqlDao<EntityModelDao<Entity>, Entity> entitySqlDao = handle.attach(InitialEntitySqlDao.class); // The transaction isolation level is now set at the pool level: this avoids 3 roundtrips for each transaction // Note that if the pool isn't used (tests or PostgreSQL), the transaction level will depend on the DB configuration //return entitySqlDao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper)); logger.debug("Starting transaction {}", debugInfo); final ReturnType returnType = entitySqlDao.inTransaction(new JdbiTransaction<ReturnType, EntityModelDao<Entity>, Entity>(handle, entitySqlDaoTransactionWrapper)); logger.debug("Exiting transaction {}, returning {}", debugInfo, returnType); return returnType; } finally { handle.close(); logger.debug("DBI handle closed, transaction: {}", debugInfo); } }