You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Mikhail Krivoshein <in...@mikkri.com> on 2004/06/04 19:18:09 UTC

[DbUtils] QueryRunner.executeAction(s) - Inversion of Control pattern applied

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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [DbUtils] QueryRunner.executeAction(s) - Inversion of Control pattern applied

Posted by David Graham <gr...@yahoo.com>.
--- Mikhail Krivoshein <in...@mikkri.com> wrote:
> Hello Devid,
> 
> >I realize IoC is a popular buzzword these days but this is actually a
> >simple application of the GoF Strategy pattern, not IoC.
> >  
> >
> Looks like you are right that IoC is a buzzword.
> But I just reread GoF strategy pattern description (from book) and can 
> say that
> this is not Strategy pattern.

Call it whatever you like but you're composing behavior by passing in a
variable algorithm for handling ResultSets.  That's the very definition of
Strategy.

David

> 
> Anyway I appreciate your feedback. Please, write your opinion about my 
> last comment.
> 
> Best regards,
> Mikhail Krivoshein
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 



	
		
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [DbUtils] QueryRunner.executeAction(s) - Inversion of Control pattern applied

Posted by Mikhail Krivoshein <in...@mikkri.com>.
Hello Devid,

>I realize IoC is a popular buzzword these days but this is actually a
>simple application of the GoF Strategy pattern, not IoC.
>  
>
Looks like you are right that IoC is a buzzword.
But I just reread GoF strategy pattern description (from book) and can 
say that
this is not Strategy pattern.

Anyway I appreciate your feedback. Please, write your opinion about my 
last comment.

Best regards,
Mikhail Krivoshein



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [DbUtils] QueryRunner.executeAction(s) - Inversion of Control pattern applied

Posted by David Graham <gr...@yahoo.com>.
--- Mikhail Krivoshein <in...@mikkri.com> wrote:
> 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?

Then you probably need to use straight JDBC.  DbUtils isn't meant to hide
JDBC, just make the simple cases easier to code.  Complex transaction
logic is not something DbUtils is designed to handle.

> 
> 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).

I realize IoC is a popular buzzword these days but this is actually a
simple application of the GoF Strategy pattern, not IoC.

> 
> 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.

IMO, you've replaced easy to understand JDBC transaction code with a
proprietary transaction API in DbUtils.  What happens when one of the
steps in the transaction is a query?  What do we do with the ResultSet? 
How do we handle errors?  I assume you want to unlock the table regardless
of a failure but we have no clear way of implementing that.  There are too
many variations on what the transaction needs to do to hide them all
behind a DbUtils API.

David

> 
> Looking forward for comments and suggestions.
> 
> Best regards,
> Mikhail Krivoshein
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org