JDBC question ?

Discussion in 'Java' started by marko, Mar 11, 2009.

  1. marko

    marko Guest

    I have one question.
    Look at this example:

    try{
    CallableStatement stmt1 = conn.prepareCall("{call ADDNEWRECORD}");
    CallableStatement stmt2 = conn.prepareCall("{call DELETERECORD}");
    stmt1.execute();
    stmt2.execute();
    }catch(SQLException e1){}
    finally{
    try{
    if(stmt1!=null || stmt2!=null) {
    stmt1.close();
    line 10
    stmt2.close();
    }
    if(null!=conn){
    conn.close();
    }
    }catch(SQLException e2){}


    where ADDNEWRECORD and DELETENEWRECORD are two stored procedures.
    My question is, what hapen's when stmt1.close() at line 10 throw exception,
    it is cought in e2 but stmt2.close() will never be reached. How can i solve
    this ?
    Can i use just one callablestatement and use define two SQL strings like
    this String sql1 = "{call ADDNEWRECORD}"; String sql2 = "{call
    DELETERECORD}", and
    than write conn.prepareCall(sq1);
    stmt1.execute();
    conn.prepareCall(sql2);
    stmt2.execute();
    and than there will be just one statement to close ?


    THANKS IN ADVANCE
    marko, Mar 11, 2009
    #1
    1. Advertising

  2. On Wed, 11 Mar 2009 15:10:00 +0100, marko wrote:

    > I have one question.

    ...../....
    > and than there will be just one statement to close ?
    >

    The most important thing to to sort out is what do you want to find in
    the database if any one of a set of related statements fail and then
    write your code to make that happen.

    Generally you'll need to think about commitment units if more than one
    update statement is involved because you need to ensure the database's
    integrity no matter what hardware and software errors may occur. This
    usually requires you to make each commitment point explicit. This has the
    additional benefit of generally making recovery after an SQL failure much
    simpler to implement.

    Your example code doesn't mention commitment control, so I assume you are
    using automatic commitment, i.e. each SQL statement commits as it
    completes, but is that what you really intend it to do?


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Mar 11, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Wansbrough

    Re: jdbc help:sun.jdbc.odbc.JdbcOdbcDriver

    Keith Wansbrough, Aug 16, 2004, in forum: Java
    Replies:
    0
    Views:
    733
    Keith Wansbrough
    Aug 16, 2004
  2. Betty
    Replies:
    1
    Views:
    9,843
    Juha Laiho
    May 21, 2005
  3. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,016
    robert
    Feb 11, 2006
  4. Bruce
    Replies:
    4
    Views:
    735
    Bjorn Abelli
    Mar 25, 2006
  5. Lew
    Replies:
    0
    Views:
    501
Loading...

Share This Page