Navigation

RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v

 

General

Use it

Documentation

Support

Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

avatar
< Custom constraints and validation in RIFE   Programming while lying on my back (lumbar disc hernia) >
Closing several JDBC statements cleanly

It's been a while since I wrote some raw JDBC code. I didn't remember that it was so tedious to manually close a series of PreparedStatement objects and make sure that any exception was properly handled and reported.

Note that the ARM blocks or BGGA closures proposals don't make this easier since this cleanup should be done after the prepared statements have been used for a while in various other methods, it doesn't automatically have to be done at the end of a lexical scope.

This is what I came up with.

Of course, you could write an alternative implementation that creates some kind of repository for the prepared statements in a map and then provide a method that closes them all by going over the entries of the map while preserving the exceptions in a similar manner. Any other suggestions or comments for this to be done better?

private PreparedStatement psStmt1;
private PreparedStatement psStmt2;
private PreparedStatement psStmt3;
 
public void cleanup() throws SQLException {
  SQLException exception = null;
  if (psStmt1 != null) {
    try {
      psStmt1.close();
    } catch (SQLException e) {
      exception = e;
    } finally {
      psStmt1 = null;
    }
  }
 
  if (psStmt2 != null) {
    try {
      psStmt2.close();
    } catch (SQLException e) {
      if (exception != null) e.setNextException(exception);
      exception = e;
    } finally {
      psStmt2 = null;
    }
  }
 
  if (psStmt3 != null) {
    try {
      psStmt3.close();
    } catch (SQLException e) {
      if (exception != null) e.setNextException(exception);
      exception = e;
    } finally {
      psStmt3 = null;
    }
  }
 
  if (exception != null) {
    throw exception;
  }
}
posted by Geert Bevin in Java on Jan 30, 2008 6:11 PM : 5 comments [permalink]
 

Comments

Re: Closing several JDBC statements cleanly
I think I need more context. Why are you storing the prepared statements in fields? Why won't ARM blocks work?
Re: Closing several JDBC statements cleanly
These prepared statements are created beforehand and are used throughout a whole bunch of interactions with the database. For instance a prepared statement to set all the values of a certain column to null, or to get the next value of a sequence. There's no need to recreate them all the time, they don't need any parameters and are typically used until the database connection closes. Fields is just one way of storing them, could be a map, or another else that allows them to be retrieved from anywhere in that class's code. Imho ARM blocks don't work since you don't have the scenario:

method A starts
open X
use X and other stuff
make sure to close X
method A stops

It's rather:

method A starts
open X
open Y
method A stops

method B starts
use X
method B stops

method C starts
use Y
method C stops

method D starts
make sure to close X
make sure to close Y
method D stops
Re: Closing several JDBC statements cleanly
Cheers, keep up the great work at Terracotta and fix this issue with pre and code!!

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * This is one possible solution that basically iterates over the
 * PreparedStatements provided and collects all of the SQLExceptions
 * thrown during the closing of the statement. Once the statements are
 * all closed, a rollup occurs to organize the order of the exceptions
 * by calling SQLException#setNextException(SQLException).
 * @author Richard L. Burton III
 */
public class CloseableStatements {

    /**
     * This method will take an array of PreparedStatements and close
     * each of the statements in order they were provided. If any exceptions
     * are thrown during the closing of the PreparedStatements, they are then
     * rolled up using <code>rollup</tt>.
     * @param statements An array of PreparedStatement objects to close.
     * @throws SQLException The SQLException if any are thrown during the closing
     *                      of the PreparedStatement objects.
     */
    protected void close(PreparedStatement... statements) throws SQLException {
        List<SQLException> exceptions = new LinkedList<SQLException>();
        for (PreparedStatement statement : statements) {
            try {
                statement.close();
            } catch (SQLException e) {
                exceptions.add(e);
            }
        }
        SQLException exception = rollup(exceptions);
        if (exception != null)
            throw exception;
    }

    /**
     * This method rolls up the exceptions by taking each proceeding exception
     * and making it a child of the previous using the <tt>setNextException</tt>
     * method of SQLException.
     * @param exceptions A List of SQLExceptions thrown during the closing.
     * @return The final rolled up exception.
     */
    protected SQLException rollup(List<SQLException> exceptions) {
        SQLException parent = null;
        for (SQLException exception : exceptions) {
            if (parent != null)
                exception.setNextException(parent);
            parent = exception;
        }
        return parent;
    }

}


Best Regards,
Richard L. Burton III
Re: Closing several JDBC statements cleanly
Thanks Richard, this will certainly come in handy as a utility class that performs the functionalities that I wrote out in the post above.
Re: Closing several JDBC statements cleanly
iyinet webmaster forumu 2008 seo yarışması
teşekkür for blog

Add a new comment

:) ;)
=) :-)
:'( :(
:/ :D
:| :p
:o 8)
Your email address will not be displayed at anytime on any page.
Only provide your email address if you'd like updates on this entry
and it's comments by email.
Please answer this simple math question:
18 + 13 = 
 
 
  

Manage subscription

Remove email:
 

< Custom constraints and validation in RIFE   Programming while lying on my back (lumbar disc hernia) >
 
 
 
Google
rifers.org web