ultm icon indicating copy to clipboard operation
ultm copied to clipboard

Two DataSources

Open qrman opened this issue 10 years ago • 3 comments

Hi @witoldsz!

I've been playing with ultm for a while. I've noticed that when I don't want run SQL operation inside transaction I have to use different DataSource (not provided by ULTM::getManagedDataSource() ) for that.

The question is: Will it be (in some next releases) possible to use managedDataSource for transactional and non transactional operations?

qrman avatar Oct 01 '15 20:10 qrman

Hi Krzysztof! Sure, I am open for enhancements! What kind of non transactional operations you need, the auto-commit one, or rather a new transaction which suspends the outer one and then let you do commit and go back? How would you like the API to support this?

witoldsz avatar Oct 02 '15 06:10 witoldsz

From my point of view, it's more about behaviour, not about API itself.

As a jOOQ user, I want use same instance of DSLContext (DSLContext need DataSource in constructor) for both transactional and non-transactional operations.

Right now I have to have two different DSLContext because it is not possible to use DataSource provided by ULTM::getManagedDataSource() without wrapping it with TXManager::tx().

qrman avatar Oct 05 '15 19:10 qrman

To expand on this a little - I think the expected use-case for ULTM + JOOQ is that

  1. You would setup ULTM with your ordinary PooledDataSource
  2. You would configure jOOQ with ULTM's ManagedDataSource
  3. Use jOOQ to operate on the db via DSL.using(jooq).xyz. This will ask the ManagedDataSource for a connection... and release it!
  4. Sometimes use jOOQ within a ULTM transactional context

Currently 3. (which in my case and I assume many others is the most common case) does not work because of this: https://github.com/witoldsz/ultm/blob/master/src/main/java/com/github/witoldsz/ultm/internal/ThreadLocalTxManager.java#L40

FTR this is how you would configure a typical dropwizard application with jooq and ultm:

  public void run(AppConfiguration appConfig, Environment env ) {
        // Likely this is a pooled data-source factory 
        DataSourceFactory ds = appConfig.getDataSourceFactory();

        // This allows the pool to do some initialization (like connect to the db + setup initial pool-size number of connections)
        ManagedDataSource dropWizardManagedDS = ds.build(env.metrics(), "jooq");
        env.lifecycle().manage(dropWizardManagedDS);

        ULTM ultm = new ULTM(dropWizardManagedDS);

        // now any time you use DSL.using(jooq), jooq will ask the ultm-managed DS for a connection
        Configuration jooq = new DefaultConfiguration();
        jooq.set(ultm.getManagedDataSource());
}

amfleming avatar Jun 30 '16 21:06 amfleming