Interface JooqTransactionManager

  • All Superinterfaces:
    TransactionManager

    public interface JooqTransactionManager
    extends TransactionManager
    Transaction manager which uses jOOQ's transaction management. In order to wire into JOOQ's transaction lifecycle, a slightly convoluted construction process is required which involves first creating a JooqTransactionListener, including it in the JOOQ Configuration while constructing the root DSLContext, and then finally linking the listener to the new JooqTransactionManager:
     DataSourceConnectionProvider connectionProvider = new DataSourceConnectionProvider(dataSource);
     DefaultConfiguration configuration = new DefaultConfiguration();
     configuration.setConnectionProvider(connectionProvider);
     configuration.setSQLDialect(SQLDialect.H2);
     configuration.setTransactionProvider(new ThreadLocalTransactionProvider(connectionProvider));
     JooqTransactionListener listener = JooqTransactionManager.createListener();
     configuration.set(listener);
     DSLContext dsl = DSL.using(configuration);
     return JooqTransactionManager.create(dsl, listener);
    • Method Detail

      • createListener

        static JooqTransactionListener createListener()
        Creates the TransactionListener to wire into the DSLContext. See class-level documentation for more detail.
        Returns:
        The transaction listener.
      • create

        static ThreadLocalContextTransactionManager create​(org.jooq.DSLContext dslContext,
                                                           JooqTransactionListener listener)
        Creates a transaction manager which uses thread-local context. Attaches to the supplied JooqTransactionListener to receive notifications of transactions starting and finishing on the local thread so that TransactionOutbox.schedule(Class) can be called for methods that don't explicitly inject a Configuration, e.g.:
        dsl.transaction(() -> outbox.schedule(Foo.class).process("bar"));
        Parameters:
        dslContext - The DSL context.
        listener - The listener, linked to the DSL context.
        Returns:
        The transaction manager.
      • create

        @Beta
        static ParameterContextTransactionManager<org.jooq.Configuration> create​(org.jooq.DSLContext dslContext)
        Creates a transaction manager which uses explicitly-passed context, allowing multiple active contexts in the current thread and contexts which are passed between threads. Requires a Configuration for the transaction context or a Transaction to be used to be passed any method called via TransactionOutbox.schedule(Class). Example:
         void doSchedule() {
           // ctx1 is used to write the request to the DB
           dsl.transaction(ctx1 -> outbox.schedule(getClass()).process("bar", ctx1));
         }
        
         // ctx2 is injected at run time
         void process(String arg, org.jooq.Configuration ctx2) {
           ...
         }

        Or:

         void doSchedule() {
           // tx1 is used to write the request to the DB
           transactionManager.inTransaction(tx1 -> outbox.schedule(getClass()).process("bar", tx1));
         }
        
         // tx2 is injected at run time
         void process(String arg, Transaction tx2) {
           ...
         }
        Parameters:
        dslContext - The DSL context.
        Returns:
        The transaction manager.