Hello. We're using Dependency Injection (Google Guice) in our app and would like to define a method like this:
public myMethod() {TransactionModule.startTransaction();
module1.insertSomething();
module2.insertSomethingElse(); TransactionModule.endTransaction();
}
The idea is each module has its own jOOQ code that does a database insert.
As these are independent modules, it makes sense to separate them, but (1) I'd like to assert a transaction at one level above the modules since the modules themselves can't know if they're part of a transaction, and (2) I'd like to leverage JDBC Batch Statements to improve efficiency.
I posted about the JDBC Batch Statement part on the jOOQ documentation site, and Lukas (kindly) responded that I seemed to be suggesting a new feature. Specifically, I proposed there that maybe jOOQ could allow setting some kind of flag to let it know I'm still queuing up statements, but it appears to only the create.batch() method is supported now.
It looks like JDBC Batch Statements are a big performance boost (http://java.dzone.com/articles/what-you-didnt-know-about-jdbc), so it would be nice to incorporate these, but how best to do this in a Dependency Injection paradigm?
So, here are my official questions to the most excellent jOOQ community:
JDBC Batch Statements
Thank you all for the input!
So, here are my official questions to the most excellent jOOQ community:
// "Regular code":// ---------------module1.insertSomething();module2.insertSomethingElse();
// The above might generate... (each line is a statement)
// INSERT INTO something (A, B) VALUES (?, ?)// INSERT INTO something (A, B) VALUES (?, ?)// INSERT INTO something (A, B) VALUES (?, ?)// INSERT INTO something (A, B, C) VALUES (?, ?, ?)// INSERT INTO something (A, B, C) VALUES (?, ?, ?)// INSERT INTO something (A, B) VALUES (?, ?)// INSERT INTO something_else (X, Y) VALUES (?, ?)// INSERT INTO something_else (X, Y) VALUES (?, ?)// INSERT INTO something_else (X, Y) VALUES (?, ?)
// "Batch-collecting code":
BatchCollector.collectAndExecute(() -> {module1.insertSomething();module2.insertSomethingElse();});
// The above might now generate... (each line is a batch statement)
// INSERT INTO something (A, B) VALUES (?, ?) -- With 3 calls to PreparedStatement.addBatch()// INSERT INTO something (A, B, C) VALUES (?, ?, ?) -- With 2 calls to PreparedStatement.addBatch()
// INSERT INTO something (A, B) VALUES (?, ?) -- With 1 calls to PreparedStatement.addBatch() or perhaps this won't generate a batch
// INSERT INTO something_else (X, Y) VALUES (?, ?) -- With 2 calls to PreparedStatement.addBatch()
JDBC Batch Statements
- What is the best way to combine Dependency Injection where individual modules execute different database operations with JDBC Batch Statements?
Transactions
- What is the best way to use transactions with Dependency Injection, again where individual modules execute JDBC operations without knowledge they may be participating in a transaction.
// INSERT INTO something (A, B) VALUES (?, ?) - Executed through proxy DS// INSERT INTO something (A, B) VALUES (?, ?) - Executed through proxy DS// INSERT INTO something (A, B, C) VALUES (?, ?, ?) - Executed through original DS
// INSERT INTO something (A, B, C) VALUES (?, ?, ?) - Executed directly// INSERT INTO something (A, B) VALUES (?, ?) - Executed as 2 batch statements
With the work you have done in JOOQ we now have a way to express the transaction as create.transaction (ctx -> { work to do within transaction } )
So something like create.batch (ctx -> { work to do within batch } ) would be a nice logical extension.
One thing you would have to provide a lot of information to the log regarding what statements are being added to a batch and which are being executed outside of a batch, as such detail would be needed to find issues such as you detail in your example regarding the reordering of inserts.
Hi Roger,
So, are you already using this new jOOQ 3.4 interface with Java 8?
On Monday, July 21, 2014 10:59:44 AM UTC+1, Lukas Eder wrote:Hi Roger,So, are you already using this new jOOQ 3.4 interface with Java 8?
Its better to say that I'm playing with jOOQ and Java 8. The client of mine who is looking at jOOQ (via my recommendation) is more likely to start within a Java 7 world as other components of their design are currrently limited to Java 7, but that may change over the life of the project.
One thing, while it would be possible to nest batches within transactions can you explicitly make sure that a transaction can not be nested within a batch, unless you plan to correctly emulate the removal of entries within your batch based on the commiting or rollback of transactions that take place within the batch (does that make sense!).