2015-03-17 20:27 GMT+01:00 <[email protected]>:
> 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:
https://github.com/jOOQ/jOOQ/issues/4144
Cheers
Lukas
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.