Hi, everyone.
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