org.jooq.impl.DefaultConnectionProvider Java Examples

The following examples show how to use org.jooq.impl.DefaultConnectionProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JooqPersistService.java    From guice-persist-jooq with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: JdbcLocalTxnInterceptorTest.java    From guice-persist-jooq with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: JdbcLocalTxnInterceptor.java    From guice-persist-jooq with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: JooqResource.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Override
protected void before() throws Throwable {
    this.connection = DriverManager.getConnection("jdbc:hsqldb:mem:junit" + System.currentTimeMillis(), "SA", "");
    this.dslContext = new DefaultDSLContext(new DefaultConnectionProvider(connection), SQLDialect.HSQLDB);
}
 
Example #5
Source File: JdbcLocalTxnInterceptor.java    From guice-persist-jooq with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #6
Source File: JooqPersistService.java    From guice-persist-jooq with Apache License 2.0 4 votes vote down vote up
public DefaultConnectionProvider getConnectionWrapper() {
 return threadConnection.get();
}