commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Petr Hollay">
Subject AW: [DBUtils] Proposal - logging
Date Tue, 01 Jun 2004 13:59:54 GMT
Hello all,

> That's why it's called "Quietly" ;-).
I know :-) I just wanted to introduce a problem.

> closeQuietly() isn't rocket science
It is not. Of course one can use close() and catch exception, but then the
only one advantage of using close() is check for null, and this is not so
much ;-)

We are using our JdbcHelper class, which does exactly what I described -
closeConnection() with logging and without throwing exceptions. 

It is the following pattern:

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcHelper.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, someId);

            rs = stmt.executeQuery();
            return JdbcHelper.getDataObject(rs, mappingSelectObjectById);

        } catch (Exception e) {
            String msg = "Error getting row with ID=" + someId;
            log.error(msg, e);
            throw new SomeException(msg, e);
        } finally {
            JdbcHelper.closeConnection(conn, stmt, rs);

with closeConnection() which 
1) checks all parameters for null - as closeQuietly(),
2) closes all parameters - as closeQuietly(),
3) catches and logs SQLExceptions - since usually nobody is really
interested in those - except of Application Administrator :-))

Resulting code is easy to write and still you get all information you need.

I don't want to do any pressure on you; I just see this pattern used quite
frequently in different applications and frameworks, often together with
some "JdbcHelper", "SqlUtil" etc. class :-)

I thing it might be useful especially to applications which are already
using Commons-Logging anyway - especially OpenSource apps.

And since you can easily filter out this added logging by setting higher
priority for this Log, it'd be "backward compatible".

What do you think?

Best regards

-----Urspr√ľngliche Nachricht-----
Von: David Graham [] 
Gesendet: Dienstag, 1. Juni 2004 13:42
An: Jakarta Commons Developers List
Betreff: Re: [DBUtils] Proposal - logging

--- Petr Hollay <> wrote:
> Hello all,
> one question to DbUtils class: methods closeQuietly() don't throw
> SQLExceptions.

That's why it's called "Quietly" ;-).

> Would you consider to add dependency on Commons-Logging and add
> log.error()
> (or maybe warn() ) logging into catch parts of those methods?

DbUtils is a one jar distro by design and choice.  Adding an unneeded
dependency is a bad thing.

> Idea behind is that in production environment it might be useful to be
> informed that something went wrong when closing JDBC
> Connection/Statement/ResultSet.

You can use the close() methods instead if you want to be notified of
errors and log them.  The implementation of closeQuietly() isn't rocket
science; you could code them in under 5 minutes :-).  Note that DbUtils
itself never uses closeQuietly() so you will always be notified of errors
closing resources.


> I didn't find any discussion on this topic in mail archive.
> Best regards
> Petr

Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger. 

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message