Обсуждение: Statement.ececuteUpdate() closes ResultSet obtained from same Statement
Statement.ececuteUpdate() closes ResultSet obtained from same Statement
От
Christian Schlichtherle
Дата:
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(); } } }
}
Вложения
Re: Statement.ececuteUpdate() closes ResultSet obtained from same Statement
От
David G Johnston
Дата:
christian wrote > I am not sure if this is a bug or a feature Feature. Use one statement for each query you need active simultaneously. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Statement-ececuteUpdate-closes-ResultSet-obtained-from-same-Statement-tp5816031p5816032.html Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.
On 08/24/2014 07:33 AM, Christian Schlichtherle wrote: > I am not sure if this is a bug or a feature, but here it goes: > @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(); > } > } > } http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html "All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists." -- Glenn Holmer (Linux registered user #16682) "After the vintage season came the aftermath -- and Cenbe."