Menu

#1740 Reused statement invalidated

version 2.5.x
open-fixed
None
5
2026-04-07
2025-10-06
No

When the same query is used in multiple PreparedStatements and the database changes inbetween some statements get invalid.

When StatementManager.compile() finds the same statement with a different compile timestamp it simply removes the old statement instead of trying to recompile it. This leaves the first statement in an invalid state even though it could be recompiled without issues.

I don't see an option to create the statements as independent besides ammending the SQL with an unique comment.

    @Test
    public void testConnectionIsValid() throws SQLException {
        assertNotNull(connection);

        Statement stmt = connection.createStatement();
        stmt.execute("CREATE TABLE journal(message VARCHAR(255))");

        PreparedStatement statement1 = connection.prepareStatement("INSERT INTO journal VALUES (?)");
        stmt.execute("CREATE TABLE test1(Id INTEGER)");

        PreparedStatement statement2 = connection.prepareStatement("INSERT INTO journal VALUES (?)");
        stmt.execute("CREATE TABLE test2(Id INTEGER)");

        statement1.setString(1, "finished writing 1");
        statement1.execute();
        statement1.close();

        statement2.setString(1, "finished writing 2");
        statement2.execute();
        statement2.close();

        stmt.close();
    }

This happens since version 2.5.2

1 Attachments

Discussion

  • Fred Toussi

    Fred Toussi - 2025-10-06
    • assigned_to: Fred Toussi
     
  • Fred Toussi

    Fred Toussi - 2025-10-06

    Thanks for reporting. Please note there have been a lot of changes since version 2.5.2. Please check with version 2.7.4 (either Java 8 or Java 11 jar).

     
  • Alexander Gottwald

    It's happening with 2.7.4 and every version sind 2.5.2

     
  • Fred Toussi

    Fred Toussi - 2025-10-08

    Thank you. I will check this later.

     
  • Fred Toussi

    Fred Toussi - 2026-04-07
    • status: open --> open-fixed
     
  • Fred Toussi

    Fred Toussi - 2026-04-07

    Fixed and committed to SVN (revision 6840)

     
    👍
    1

Log in to post a comment.

MongoDB Logo MongoDB