I am not sure if this is a bug or a feature, but here it goes:
package cpssd.postgresql;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.*;
public class TicketIT {
private static Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:postgresql:postgres"); }
@Before public void createTable() throws SQLException { try (Connection c = getConnection(); Statement s = c.createStatement()) { s.executeUpdate("CREATE TABLE test(a INT)"); } }
@After public void dropTable() throws SQLException { try (Connection c = getConnection(); Statement s = c.createStatement()) { s.executeUpdate("DROP TABLE test"); } }
@Test(expected = SQLException.class) public void testSharedStatementClosesResultSet() throws SQLException { try (Connection c = getConnection(); Statement s = c.createStatement()) { try (ResultSet rs = s.executeQuery("SELECT a FROM test")) { s.executeUpdate("INSERT INTO test(a) VALUES (1)"); // Expected false, but throws SQLException: This statement has been closed. assert !rs.next(); } } }
@Test public void testUsingAnotherStatementWorks() throws SQLException { try (Connection c = getConnection(); Statement s1 = c.createStatement()) { try (ResultSet rs = s1.executeQuery("SELECT a FROM test")) { try (Statement s2 = c.createStatement()) { s2.executeUpdate("INSERT INTO test(a) VALUES (1)"); } assert !rs.next(); } } }
}