@Override public void configure(final Env env, final Config conf, final Binder binder) { Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name)); Supplier<NoSuchElementException> noSuchElement = () -> new NoSuchElementException( "DataSource missing: " + dskey); HikariDataSource ds = (HikariDataSource) env.get(dskey).orElseThrow(noSuchElement); Configuration jooqconf = new DefaultConfiguration(); ConnectionProvider dscp = new DataSourceConnectionProvider(ds); jooqconf.set(JDBCUtils.dialect(env.get(Key.get(String.class, Names.named(name + ".url"))) .orElseThrow(noSuchElement))); jooqconf.set(dscp); jooqconf.set(new DefaultTransactionProvider(dscp)); if (callback != null) { callback.accept(jooqconf, conf); } ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(Configuration.class, name, k -> binder.bind(k).toInstance(jooqconf)); Provider<DSLContext> dsl = () -> DSL.using(jooqconf); serviceKey.generate(DSLContext.class, name, k -> binder.bind(k).toProvider(dsl)); } }
@Override public final void commit(TransactionContext ctx) { Deque<Savepoint> savepoints = savepoints(ctx.configuration()); Savepoint savepoint = savepoints.pop(); // [#3489] Explicitly release savepoints prior to commit if (savepoint != null && savepoint != UNSUPPORTED_SAVEPOINT && savepoint != IGNORED_SAVEPOINT) try { connection(ctx.configuration()).releaseSavepoint(savepoint); } // [#3537] Ignore those cases where the JDBC driver incompletely implements the API // See also http://stackoverflow.com/q/10667292/521799 catch (DataAccessException ignore) {} // This is the top-level transaction if (savepoints.isEmpty()) { connection(ctx.configuration()).commit(); brace(ctx.configuration(), false); } // Nested commits have no effect else { } }
@Override public final void begin(TransactionContext ctx) { Deque<Savepoint> savepoints = savepoints(ctx.configuration()); // This is the top-level transaction boolean topLevel = savepoints.isEmpty(); if (topLevel) brace(ctx.configuration(), true); Savepoint savepoint = setSavepoint(ctx.configuration(), topLevel); if (savepoint == UNSUPPORTED_SAVEPOINT && !topLevel) throw new DataAccessException("Cannot nest transactions because Savepoints are not supported"); savepoints.push(savepoint); }
@Override public void commit(TransactionContext ctx) { if (delegateTransactionProvider.nestingLevel(ctx.configuration()) == 1) localTxConnection.remove(); configurations().pop(); delegateTransactionProvider.commit(ctx); }
@Override public void rollback(TransactionContext ctx) { if (delegateTransactionProvider.nestingLevel(ctx.configuration()) == 1) localTxConnection.remove(); configurations().pop(); delegateTransactionProvider.rollback(ctx); }
private final Savepoint setSavepoint(Configuration configuration, boolean topLevel) { if (topLevel || !nested()) return IGNORED_SAVEPOINT; switch (configuration.family()) { case CUBRID: return UNSUPPORTED_SAVEPOINT; default: return connection(configuration).setSavepoint(); } }
@Override public void begin(TransactionContext ctx) { delegateTransactionProvider.begin(ctx); configurations().push(ctx.configuration()); if (delegateTransactionProvider.nestingLevel(ctx.configuration()) == 1) localTxConnection.set(((DefaultConnectionProvider) ctx.configuration().data(DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION)).connection); }
/** * Ensure an <code>autoCommit</code> value on the connection, if it was set * to <code>true</code>, originally. */ private final void brace(Configuration configuration, boolean start) { DefaultConnectionProvider connection = connection(configuration); try { boolean autoCommit = autoCommit(configuration); // Transactions cannot run with autoCommit = true. Change the value for // the duration of a transaction if (autoCommit == true) connection.setAutoCommit(!start); } // [#3718] Chances are that the above JDBC interactions throw additional exceptions // try-finally will ensure that the ConnectionProvider.release() call is made finally { if (!start) { connectionProvider.release(connection.connection); configuration.data().remove(DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION); } } }
final int nestingLevel(Configuration configuration) { return savepoints(configuration).size(); }
private final boolean autoCommit(Configuration configuration) { Boolean autoCommit = (Boolean) configuration.data(DATA_DEFAULT_TRANSACTION_PROVIDER_AUTOCOMMIT); if (autoCommit == null) { autoCommit = connection(configuration).getAutoCommit(); configuration.data(DATA_DEFAULT_TRANSACTION_PROVIDER_AUTOCOMMIT, autoCommit); } return autoCommit; }
@Override public final void rollback(TransactionContext ctx) { Deque<Savepoint> savepoints = savepoints(ctx.configuration()); Savepoint savepoint = null; // [#3537] If something went wrong with the savepoints per se if (!savepoints.isEmpty()) savepoint = savepoints.pop(); try { if (savepoint == null || savepoint == UNSUPPORTED_SAVEPOINT) { connection(ctx.configuration()).rollback(); } // [#3955] ROLLBACK is only effective if an exception reaches the // top-level transaction. else if (savepoint == IGNORED_SAVEPOINT) { if (savepoints.isEmpty()) connection(ctx.configuration()).rollback(); } else { connection(ctx.configuration()).rollback(savepoint); } } finally { if (savepoints.isEmpty()) brace(ctx.configuration(), false); } }
@Override public final TransactionProvider transactionProvider() { // [#3229] If transactions are used in client code, the default behaviour // is assumed automatically, for convenience. if (transactionProvider instanceof NoTransactionProvider) { return new DefaultTransactionProvider(connectionProvider); } return transactionProvider; }
/** * @param nested Whether nested transactions via {@link Savepoint}s are * supported. */ public ThreadLocalTransactionProvider(ConnectionProvider connectionProvider, boolean nested) { this.localConnectionProvider = new ThreadLocalConnectionProvider(connectionProvider); this.delegateTransactionProvider = new DefaultTransactionProvider(localConnectionProvider, nested); this.localConfigurations = new ThreadLocal<Deque<Configuration>>(); this.localTxConnection = new ThreadLocal<Connection>(); }