Search code examples
javamysqlsql-workbench-j

Java mysql AWS execute succeed and then select does not return the new value


I am working with AWS RDS (specifically mysql) and I am using SQL Workbench/J as a GUI tool. My server side code written in Java and here is my code:

Insert code:

try {
    Statement myStatement = insertConnectionObject.createStatement();
    myStatement.executeUpdate("INSERT INTO friends VALUES('buddy', '15', '123');");
    myStatement.close();
} catch(Exception ex) {
    // code for handling exceptions
} finally {
    myStatement.close();
    insertConnectionObject.close();
}

After that, I call the select code from the same table:

try {
    Statement myStatement = selectConnectionObject.createStatement();
    ResultSet returnedFriends = myStatement.executeQuery("SELECT * FROM friends;");
    //unfortunately, the returnedFriends will not return the new inserted value 'buddy'
} catch(Exception ex) {
    // code for handling exceptions
} finally {
    myStatement.close();
    insertConnectionObject.

unfortunately, the returnedFriends will not return the new inserted value 'buddy'.

If I will click the 'commit any pending database changes' button in the SQL Workbench/J GUI tool, and then run the select statement, the new value 'buddy' will return.

What have I tried until now?

  1. Use the same connection object for both insert and select.
  2. Open and close the connection after the insert command, and after every select command.
  3. disable the auto commit and try to commit manually.
  4. Inserting via code, and then selecting directly from the DB.

Solution

  • My problem was as simple and annoying as can be - apparently, I had to close the Workbench GUI when working from the code, which is kind of wired and requires probably deeper investigation from the Workbench / AWS teams.

    Anyways, after closing this interface, everything just worked.

    Thanks for the help!