Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Spot the bug!!

Back to Forum: Tech Off
  • User profile image
    gaelhatchue

    The following sample is taken from the documentation of Connector/J, the jdbc driver for mysql


    InitialContext ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
    Connection conn = null;
    Statement stmt = null;

    try {
    conn = ds.getConnection();
    stmt = conn.createStatement();
    stmt.execute("SOME SQL QUERY");

    stmt.close();
    stmt = null;

    conn.close();
    conn = null;
    } finally {

    if (stmt != null) {
    try {
    stmt.close();
    } catch (sqlexception sqlex) {
    // ignore -- as we can't do anything about it here
    }

    stmt = null;
    }

    if (conn != null) {
    try {
    conn.close();
    } catch (sqlexception sqlex) {
    // ignore -- as we can't do anything about it here
    }

    conn = null;
    }
    }


  • User profile image
    gabe19

    Not sure I've got what you're after, but this seems like it would be better written with the closing and nulling (required?) in the finally block. If Java throws exceptions in the try after the finally, you will get exceptions from trying to close, but the close will be executed anyhow by the exception-swallowing finally block. Certainly seems convoluted.

  • User profile image
    gaelhatchue

    gabe19 wrote:
    Not sure I've got what you're after, but this seems like it would be better written with the closing and nulling (required?) in the finally block. If Java throws exceptions in the try after the finally, you will get exceptions from trying to close, but the close will be executed anyhow by the exception-swallowing finally block. Certainly seems convoluted.


    You got it right. This was an easy one isn't it Tongue Out . I am not sure it actually qualifies as a bug. But this is certainly poorly written exception handling code.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.