From a881b553f61952798359e675a410d8a8821deca4 Mon Sep 17 00:00:00 2001 From: wanghe Date: Fri, 3 Nov 2017 17:09:05 +0800 Subject: [PATCH] Close statement in finally block --- .../org/trafodion/jdbc_test/BatchTest.java | 69 +++++++++++-------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/dcs/src/test/jdbc_test/src/test/java/org/trafodion/jdbc_test/BatchTest.java b/dcs/src/test/jdbc_test/src/test/java/org/trafodion/jdbc_test/BatchTest.java index f5977aeeec..b92afc9a09 100755 --- a/dcs/src/test/jdbc_test/src/test/java/org/trafodion/jdbc_test/BatchTest.java +++ b/dcs/src/test/jdbc_test/src/test/java/org/trafodion/jdbc_test/BatchTest.java @@ -159,37 +159,46 @@ public void testBatchInsertPkDuplicate() throws Exception { } int expectedRowCount = 8; - // Start to prepare and execute the batch upsert - PreparedStatement insertStmt = _conn.prepareStatement(strInsert); - for(int i=0; i < 10; ++i) { - insertStmt.setInt(1, idArray[i]); - insertStmt.setString(2, nameArray[i]); - insertStmt.addBatch(); + PreparedStatement insertStmt =null; + ResultSet rs = null; + try { + // Start to and execute the batch upsert + insertStmt = _conn.prepareStatement(strInsert); + for(int i=0; i < 10; ++i) { + insertStmt.setInt(1, idArray[i]); + insertStmt.setString(2, nameArray[i]); + insertStmt.addBatch(); + } + + try { + statusArray = insertStmt.executeBatch(); + } catch(SQLException sqle) { + assertTrue(sqle.getMessage().toUpperCase().contains("BATCH UPDATE FAILED")); + SQLException e = null; + e = sqle.getNextException(); + do { + assertTrue(e.getMessage().contains("ERROR[8102] The operation is prevented by a unique constraint")); + } while((e = e.getNextException()) != null); + } + + //assertArrayEquals(expectedStatusArray, statusArray); + + int rowCount = 0; + rs = _conn.createStatement().executeQuery(strSelect); + while(rs.next()) { + System.out.println("ID = " + rs.getString(1) + ", Name = " + rs.getString(2)); + assertEquals(expectedIdArray[rs.getRow()-1], rs.getInt(1)); + assertEquals(expectedNameArray[rs.getRow()-1], rs.getString(2)); + rowCount++; + } + rs.close(); + insertStmt.close(); + } finally { + if (rs != null) + rs.close(); + if (insertStmt != null) + insertStmt.close(); } - - try { - statusArray = insertStmt.executeBatch(); - } catch(SQLException sqle) { - assertTrue(sqle.getMessage().toUpperCase().contains("BATCH UPDATE FAILED")); - SQLException e = null; - e = sqle.getNextException(); - do { - assertTrue(e.getMessage().contains("ERROR[8102] The operation is prevented by a unique constraint")); - } while((e = e.getNextException()) != null); - } - - //assertArrayEquals(expectedStatusArray, statusArray); - - int rowCount = 0; - ResultSet rs = _conn.createStatement().executeQuery(strSelect); - while(rs.next()) { - System.out.println("ID = " + rs.getString(1) + ", Name = " + rs.getString(2)); - assertEquals(expectedIdArray[rs.getRow()-1], rs.getInt(1)); - assertEquals(expectedNameArray[rs.getRow()-1], rs.getString(2)); - rowCount++; - } - rs.close(); - insertStmt.close(); } /* Currently SQL does not have the ability to dump individual row, which