JDBC: Does call to rollback() method have effect only if call to commit() method does not succeed?

Saleh Feek picture Saleh Feek · Feb 22, 2013 · Viewed 21.3k times · Source

I am new to Java JDBC, and developed a small database application. I am learning from O'Reilly - Database Programming with JDBC and Java 2nd Edition.

Does con.rollback() have effect only ifcon.commit does not succeed?

I expected that calling con.rollback() has its effect even if con.commit() succeeded. In other words, utilizing it as an "Undo" action.

I tried calling con.rollback() after con.commit() succeeded, but it is not working as expected. So is it alright/expected?

This example is from the book I mentioned above:

The call to con.rollback() is commented out. It is near the end before con.close(). I tried uncommenting it and running it. However, con.rollback() doesn't roll things back after con.commit() succeeded.

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UpdateLogic
{

    public static void main(String args[])
    {
        Connection con = null;

        try
        {
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver).newInstance();
            String url = "jdbc:mysql://localhost:3306/Company";
            Statement s;
            con = DriverManager.getConnection(url, "root", "");
            con.setAutoCommit(false); // make sure auto commit is off!
            s = con.createStatement();// create the first statement
            s.executeUpdate("INSERT INTO employee VALUES ('1', 'employee 1', '22','00-1234' )");

            s.close(); // close the first statement
            s = con.createStatement(); // create the second statement
            s.executeUpdate("INSERT INTO employee VALUES ('2', 'employee 2', '21','00_4321' )");

            con.commit(); // commit the two statements
            System.out.println("Insert succeeded.");
            s.close(); // close the second statement
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex)
        {
            Logger.getLogger(UpdateLogic.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e)
        {
            if (con != null)
            {
                try
                {
                    con.rollback();
                } // rollback on error
                catch (SQLException i)
                {
                }
            }
            e.printStackTrace();
        } finally
        {
            if (con != null)
            {
                try
                {
                  //con.rollback();
                    con.close();
                } catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

Answer

jtahlborn picture jtahlborn · Feb 22, 2013

When you call commit(), you complete/close the current transaction. Thus, since rollback() undoes any changes in the current transaction (as per the javadoc), it will effectively do nothing.