Re-using a DSLContext vs. making new ones

52 views
Skip to first unread message

thema...@gmail.com

unread,
Mar 17, 2015, 3:32:30 PM3/17/15
to jooq...@googlegroups.com
Hi, everyone.

I've been reading the jOOQ manual and I have a couple rudimentary questions on the re-use of DSLContexts. A relevant page in the jOOQ manual is here: http://www.jooq.org/doc/3.5/manual/sql-execution/transaction-management/

The first example code block on that page is the following:

create.transaction(configuration -> {
    AuthorRecord author =
    DSL.using(configuration)
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    DSL.using(configuration)
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

    // Implicit commit executed here
});

I see here that two DSLContexts are being used; one for each query.  They are each produced using DSL.using(configuration). I'm merely wondering if there is a reason why two DSLContexts are being used rather than one - whether it is for stylistic reasons or a functional reason that I'm not seeing.  For example, will the following code behave identically?

create.transaction(configuration -> {

    DSLContext dslContext = DSL.using(configuration);

    AuthorRecord author =
    dslContext
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    dslContext
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

});

Going one step further, how about the following?

final DSLContext create = SomeArbitraryDSLContextProducer();

create.transaction(configuration -> {

    AuthorRecord author =
    create
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    create
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

});

Thanks in advance for your help!
Matt

Lukas Eder

unread,
Mar 18, 2015, 3:45:49 AM3/18/15
to jooq...@googlegroups.com
2015-03-17 20:27 GMT+01:00 <thema...@gmail.com>:
Hi, everyone.

I've been reading the jOOQ manual and I have a couple rudimentary questions on the re-use of DSLContexts. A relevant page in the jOOQ manual is here: http://www.jooq.org/doc/3.5/manual/sql-execution/transaction-management/

The first example code block on that page is the following:

create.transaction(configuration -> {
    AuthorRecord author =
    DSL.using(configuration)
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    DSL.using(configuration)
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

    // Implicit commit executed here
});

I see here that two DSLContexts are being used; one for each query.  They are each produced using DSL.using(configuration). I'm merely wondering if there is a reason why two DSLContexts are being used rather than one - whether it is for stylistic reasons or a functional reason that I'm not seeing.

Good question. For stylistic reasons, only. DSLContext does not maintain any state of its own other than referencing the Configuration that you provide it with. It just puts the Configuration in the "context" of the DSL, allowing for creating executable Query and ResultQuery instances.

In other words, there isn't even any performance impact.
 
For example, will the following code behave identically?

Yes.
 

create.transaction(configuration -> {

    DSLContext dslContext = DSL.using(configuration);

    AuthorRecord author =
    dslContext
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    dslContext
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

});

Going one step further, how about the following?

That will depend on your TransactionProvider implementation. Generally: No that might not behave the same way.
 
final DSLContext create = SomeArbitraryDSLContextProducer();

create.transaction(configuration -> {

    AuthorRecord author =
    create
       .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values("George", "Orwell")
       .returning()
       .fetchOne();

    create
       .insertInto(BOOK, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(author.getId(), "1984")
       .values(author.getId(), "Animal Farm")
       .execute();

});

Thanks in advance for your help!

Thank you for pointing this out! We'll add a short explanation behind the example's rationale to the manual:

Cheers
Lukas 
Reply all
Reply to author
Forward
0 new messages