Hello all,
Today I found one disadvantage of DbUtils QueryRunner class.
Why I use it? Because it makes database connections management very straightforward
and simplifies queries execution. But what happens if you need to execute something
really complex?
I have need to execute four statements as one database action:
=============================================================
try {
runner.update(conn, "LOCK TABLES keyword WRITE");
runner.update(
conn,
"DELETE FROM keyword WHERE service_id = ?",
serviceIdInt);
runner.batch(
conn,
"INSERT INTO keyword (keyword, service_id) VALUES (?,?)",
data);
} finally {
runner.update(conn, "UNLOCK TABLES");
}
=============================================================
Problem is that I need to use the same database connection to execute all of them.
So I'm forced to manage database connection by myself. Oops. That is exactly why
I'm started to use DbUtils package.
Lets make one step back. How QueryRunner is usually used? Developer call its methods,
such as query() and pass ResultSetHandler that gathers data from query results.
This is very close to Inversion of Control design pattern.
So my proposal is to make one step forward and implement pure IoC style methods
void executeAction(DatabaseAction action) and void executeActions(DatabaseAction[] actions).
My example will looks like this after such change:
=============================================================
runner.executeActions(
new DatabaseAction[] {
runner.newUpdateAction("LOCK TABLES keyword WRITE"),
runner.newUpdateAction("DELETE FROM keyword WHERE service_id = ?",
serviceIdInt),
runner.newUpdateAction("INSERT INTO keyword (keyword, service_id) VALUES (?,?)",
data),
runner.newUpdateAction("UNLOCK TABLES");
});
=============================================================
I consider this code very straighforward.
Looking forward for comments and suggestions.
Best regards, Mikhail Krivoshein
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
