One of the problems I had with the JOOQ transaction approach is that I
could not enforce that users only make SQL calls through a transaction. I
solved it by making this little class.

class Transactor {

    private final DSLContext ctx;

    Transactor(DSLContext ctx) {
        this.ctx = ctx;
    }

    <T> T apply(Function<DSLContext, T> sql) {
        ctx.transactionResult(config ->
            sql.apply(DSL.using(config)));
    }

    void accept(Consumer<DSLContext> sql) {
        ctx.transaction(config ->
            sql.accept(DSL.using(config)));
    }
}


So instead of a DAO having a DSLContext instance variable they have a
Transactor instance variable. This gives them no access to the DSLContext
so no risk of doing non-transacted updates or inserts (we always have auto
commit off). Maybe there is a better way but this worked out pretty well.
Then we just have it used like so:

public OltpCredentialsDao(DSLContext dslContext) {
    this.transactor = new Transactor(dslContext);
}

Optional<CredentialsRecord> findByUUID(UUID id) {
    return transactor.apply(sql ->
        Optional.ofNullable(
            sql.selectFrom(CREDENTIALS)
                .where(CREDENTIALS.UUID.eq(id.toString()))
                .fetchAny()));
}


You can always call a method inside the apply or accept and pass the
DSLContext but that will be a transacted arg.

Just FWIW

On Fri, Feb 23, 2018 at 2:58 PM, Ed Erwin <enwi...@gmail.com> wrote:

>
>
> On Friday, February 23, 2018 at 1:57:09 AM UTC-8, Lukas Eder wrote:
>>
>> Hi Ed,
>>
>>
>>> Also, is there any difference between calling DSL.using(configuration)
>>> compared to configuration.dsl() ?
>>>
>>
>> No. The latter was introduced merely for convenience:
>> https://github.com/jOOQ/jOOQ/issues/6372
>>
>>
> Thanks.  Your comments do make me feel more comfortable passing around the
> configuration object and using "configuration.dsl()".
>
> Based on looking at the code, I know that "DSL.using(configuration)" will
> always produce a new instance of DefaultDSLContext.  It was not clear to me
> that "configuration.dsl()" would always produce an equivalent object.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "jOOQ User Group" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/jooq-user/FeXUH-yTUQc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> jooq-user+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jooq-user+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to