add another try-with-resource
@@ -38,25 +38,25 @@ public class PoolableTest extends AbstractTest {
/**
* Poolable Test
+ *
* @throws SQLException
* @throws ClassNotFoundException
*/
@Test
@DisplayName("Poolable Test")
public void poolableTest() throws SQLException, ClassNotFoundException {
- Connection connection = DriverManager.getConnection(connectionString);
- Statement statement = connection.createStatement();
+ try (Connection conn = DriverManager.getConnection(connectionString); Statement statement = conn.createStatement()) {
try {
// First get the default values
boolean isPoolable = ((SQLServerStatement) statement).isPoolable();
assertEquals(isPoolable, false, "SQLServerStatement should not be Poolable by default");
- PreparedStatement prepStmt = connection.prepareStatement("select 1");
+ try (PreparedStatement prepStmt = connection.prepareStatement("select 1")) {
isPoolable = ((SQLServerPreparedStatement) prepStmt).isPoolable();
assertEquals(isPoolable, true, "SQLServerPreparedStatement should be Poolable by default");
+ }
-
- CallableStatement callableStatement = connection.prepareCall("{ ? = CALL " + "ProcName" + " (?, ?, ?, ?) }");
+ try (CallableStatement callableStatement = connection.prepareCall("{ ? = CALL " + "ProcName" + " (?, ?, ?, ?) }");) {
isPoolable = ((SQLServerCallableStatement) callableStatement).isPoolable();
assertEquals(isPoolable, true, "SQLServerCallableStatement should be Poolable by default");
@@ -65,18 +65,17 @@ public class PoolableTest extends AbstractTest {
((SQLServerCallableStatement) callableStatement).setPoolable(false);
assertEquals(((SQLServerCallableStatement) callableStatement).isPoolable(), false, "set did not work");
- callableStatement.close();
+ }
((SQLServerStatement) statement).setPoolable(true);
assertEquals(((SQLServerStatement) statement).isPoolable(), true, "set did not work");
- statement.close();
-
}
catch (UnsupportedOperationException e) {
assertEquals(System.getProperty("java.specification.version"), "1.5", "PoolableTest should be supported in anything other than 1.5");
assertEquals(e.getMessage(), "This operation is not supported.", "Wrong exception message");
}
}
+ }
/**
* Clean up
@@ -85,22 +84,13 @@ public class PoolableTest extends AbstractTest {
*/
@AfterAll
public static void afterAll() throws Exception {
-
- Connection conn = DriverManager.getConnection(connectionString);
- Statement stmt = conn.createStatement();
+ try (Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement()) {
try {
Utils.dropProcedureIfExists("ProcName", stmt);
}
catch (Exception ex) {
fail(ex.toString());
}
- finally {
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
}
}
