private static void initInternal(final Vertx vertx, final String name) { vertxRef = vertx; Fn.pool(CONFIGS, name, () -> Infix.init(Plugins.Infix.JOOQ, (config) -> { // Initialized client final Configuration configuration = new DefaultConfiguration(); configuration.set(SQLDialect.MYSQL_8_0); final ConnectionProvider provider = new DefaultConnectionProvider(HikariCpPool.getConnection( config.getJsonObject("provider") )); // Initialized default configuration configuration.set(provider); return configuration; }, JooqInfix.class)); }
@Override public void run(T dwConfiguration, Environment environment) throws Exception { final DataSourceFactory dbConfig = getDataSourceFactory(dwConfiguration); ManagedDataSource dataSource = dbConfig.build(environment.metrics(), "jooq"); this.configuration = new DefaultConfiguration(); this.configuration.set(new DataSourceConnectionProvider(dataSource)); configure(this.configuration); environment.jersey().register(JooqTransactionalApplicationListener.class); environment.jersey().register( new ConfigurationFactoryProvider.Binder(this.configuration, dataSource, multiTenantConnectionProvider)); environment.lifecycle().manage(dataSource); if (multiTenantConnectionProvider != null) { environment.lifecycle().manage(multiTenantConnectionProvider); } environment.healthChecks().register("jooq", new JooqHealthCheck( DSL.using(this.configuration.derive(new DefaultConnectionProvider(dataSource.getConnection()))), dbConfig.getValidationQuery())); }
public void end() { DSLContext jooqFactory = threadFactory.get(); DefaultConnectionProvider conn = threadConnection.get(); // Let's not penalize users for calling end() multiple times. if (null == jooqFactory) { return; } try { logger.debug("Closing JDBC connection"); conn.acquire().close(); } catch (SQLException e) { throw new RuntimeException(e); } threadFactory.remove(); threadConnection.remove(); }
@Before public void setUp() throws Exception { interceptor = new JdbcLocalTxnInterceptor(Providers.of(jooqPersistService), Providers.of(unitOfWork)); when(connection.getAutoCommit()).thenCallRealMethod(); doCallRealMethod().when(connection).setAutoCommit(anyBoolean()); connection.setAutoCommit(true); DefaultConnectionProvider connectionProvider = new DefaultConnectionProvider(connection); when(jooqPersistService.getConnectionWrapper()).thenReturn(connectionProvider); when(jooqPersistService.isWorking()).thenReturn(false); // Method is final. Mockito doesn't support mocking final classes. Using reflection Method defaultTransaction = JdbcLocalTxnInterceptorTest.class.getMethod("transaction"); when(methodInvocation.getMethod()).thenReturn(defaultTransaction); }
void run(Transactional tx) { // Initialise some jOOQ objects final DefaultConnectionProvider c = new DefaultConnectionProvider(connection); final Configuration configuration = new DefaultConfiguration().set(c).set(SQLDialect.H2); try { // Run the transaction and pass a jOOQ // DSLContext object to it tx.run(DSL.using(configuration)); // If we get here, then commit the // transaction c.commit(); } catch (RuntimeException e) { // Any exception will cause a rollback c.rollback(); System.err.println(e.getMessage()); // Eat exceptions in silent mode. if (!silent) throw e; } }
/** * Returns True if rollback DID NOT HAPPEN (i.e. if commit should continue). * * @param transactional The metadata annotation of the method * @param e The exception to test for rollback * @param txn A JPA Transaction to issue rollbacks on */ private boolean rollbackIfNecessary(final Transactional transactional, final Exception e, final DefaultConnectionProvider conn) { boolean commit = true; //check rollback clauses for (Class<? extends Exception> rollBackOn : transactional.rollbackOn()) { //if one matched, try to perform a rollback if (rollBackOn.isInstance(e)) { commit = false; //check ignore clauses (supercedes rollback clause) for (Class<? extends Exception> exceptOn : transactional.ignore()) { //An exception to the rollback clause was found, DON'T rollback // (i.e. commit and throw anyway) if (exceptOn.isInstance(e)) { commit = true; break; } } //rollback only if nothing matched the ignore check if (!commit) { logger.debug("Rolling back JDBC transaction for this thread"); conn.rollback(); } //otherwise continue to commit break; } } return commit; }
@Before public void setUp() { configuration.set(new DefaultConnectionProvider(connection)); dslContext = DSL.using(configuration); healthCheck = new JooqHealthCheck(dslContext, "SELECT 1"); }
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; }
public DefaultConnectionProvider getConnectionWrapper() { return threadConnection.get(); }