1

I have a table that contains an id column, a bids column and a cost column. I am using transactions in jdbc. I set auto commit to false, executed some queries to update that table and then called the rollback method, which should undo changes made in the transaction and revert the table back to its previous state. However, the table is still being updated.

Here is my code:

public class JdbcTransactions {
public static void main(String[] args) {
    Connection myConn = null;
    Statement myStmt = null;

    try {
        myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tutorialdatabase", "student", "student");

        myConn.setAutoCommit(false);

        myStmt = myConn.createStatement();
        myStmt.executeUpdate("UPDATE items SET bids = 0 WHERE id <= 5");
        myStmt.executeUpdate("UPDATE items SET cost = cost + 100 WHERE id <= 5");

        myConn.rollback();


    }catch(SQLException e) {
        System.err.println("Something went wrong: ");
        e.printStackTrace();

    } finally {
        try {
            myStmt.close();
            myConn.close();
        }catch(SQLException e) {

        }
    }
}

}

I want to update the items table in tutorialdatabase to set the bids to 0 and cost to increment by 100 for the first 5 rows(all rows with id 1 - 5)

Nalyd
  • 95
  • 1
  • 16

1 Answers1

-1

Try to put autocommit off on db SET autocommit=0;

user3186511
  • 63
  • 2
  • 8
  • Auto commit is turned off using `myConn.setAutoCommit(false)`, you are not supposed to use database statements for that because it can leave the driver-side connection in an incorrect state. – Mark Rotteveel Jun 09 '17 at 06:42