下面列出了怎么用com.google.inject.persist.UnitOfWork的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void configurePersistence() {
poolsMultibinder = Multibinder.newSetBinder(binder(), PoolManager.class);
final OrientDBFactory info = new OrientDBFactory(
uri, user, password, autoCreateLocalDb, config, serverUser, serverPassword, remoteType);
bind(OrientDBFactory.class).toInstance(info);
bind(TxConfig.class).annotatedWith(Names.named("orient.txconfig"))
.toInstance(txConfig == null ? new TxConfig() : txConfig);
configureCustomTypes();
bind(CustomTypesInstaller.class);
// extension points
bind(TransactionManager.class);
// SchemeInitializer.class
// DataInitializer.class
bind(PersistService.class).to(DatabaseManager.class);
bind(OrientDB.class).toProvider(DatabaseManager.class);
bind(UnitOfWork.class).to(TransactionManager.class);
configurePools();
configureInterceptor();
bindRetryInterceptor();
}
@Override
protected void configurePersistence() {
bind(JooqPersistService.class).in(Singleton.class);
bind(PersistService.class).to(JooqPersistService.class);
bind(UnitOfWork.class).to(JooqPersistService.class);
bind(DSLContext.class).toProvider(JooqPersistService.class);
transactionInterceptor = new JdbcLocalTxnInterceptor(getProvider(JooqPersistService.class),
getProvider(UnitOfWork.class));
requestInjection(transactionInterceptor);
}
@Inject
public JdbcLocalTxnInterceptor(Provider<JooqPersistService> jooqPersistServiceProvider,
Provider<UnitOfWork> unitOfWorkProvider) {
this.jooqPersistServiceProvider = jooqPersistServiceProvider;
this.unitOfWorkProvider = unitOfWorkProvider;
}
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
UnitOfWork unitOfWork = unitOfWorkProvider.get();
JooqPersistService jooqProvider = jooqPersistServiceProvider.get();
// Should we start a unit of work?
if (!jooqProvider.isWorking()) {
unitOfWork.begin();
didWeStartWork.set(true);
}
Transactional transactional = readTransactionMetadata(methodInvocation);
DefaultConnectionProvider conn = jooqProvider.getConnectionWrapper();
// Allow 'joining' of transactions if there is an enclosing @Transactional method.
if (!conn.getAutoCommit()) {
return methodInvocation.proceed();
}
logger.debug("Disabling JDBC auto commit for this thread");
conn.setAutoCommit(false);
Object result;
try {
result = methodInvocation.proceed();
} catch (Exception e) {
//commit transaction only if rollback didn't occur
if (rollbackIfNecessary(transactional, e, conn)) {
logger.debug("Committing JDBC transaction");
conn.commit();
}
logger.debug("Enabling auto commit for this thread");
conn.setAutoCommit(true);
//propagate whatever exception is thrown anyway
throw e;
} finally {
// Close the em if necessary (guarded so this code doesn't run unless catch fired).
if (null != didWeStartWork.get() && conn.getAutoCommit()) {
didWeStartWork.remove();
unitOfWork.end();
}
}
// everything was normal so commit the txn (do not move into try block above as it
// interferes with the advised method's throwing semantics)
try {
logger.debug("Committing JDBC transaction");
conn.commit();
logger.debug("Enabling auto commit for this thread");
conn.setAutoCommit(true);
} finally {
//close the em if necessary
if (null != didWeStartWork.get()) {
didWeStartWork.remove();
unitOfWork.end();
}
}
//or return result
return result;
}