You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2016/12/18 22:07:00 UTC
svn commit: r1774970 -
/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java
Author: pmouawad
Date: Sun Dec 18 22:06:59 2016
New Revision: 1774970
URL: http://svn.apache.org/viewvc?rev=1774970&view=rev
Log:
Fix sonar warning on closing statement
Modified:
jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java
Modified: jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java?rev=1774970&r1=1774969&r2=1774970&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java (original)
+++ jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java Sun Dec 18 22:06:59 2016
@@ -160,13 +160,10 @@ public abstract class AbstractJDBCTestEl
*/
protected byte[] execute(Connection conn, SampleResult sample) throws SQLException, IOException, UnsupportedOperationException {
log.debug("executing jdbc");
- Statement stmt = null;
-
- try {
- // Based on query return value, get results
- String _queryType = getQueryType();
- if (SELECT.equals(_queryType)) {
- stmt = conn.createStatement();
+ // Based on query return value, get results
+ String _queryType = getQueryType();
+ if (SELECT.equals(_queryType)) {
+ try (Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(getIntegerQueryTimeout());
ResultSet rs = null;
try {
@@ -176,65 +173,64 @@ public abstract class AbstractJDBCTestEl
} finally {
close(rs);
}
- } else if (CALLABLE.equals(_queryType)) {
- try (CallableStatement cstmt = getCallableStatement(conn)) {
- int[] out = setArguments(cstmt);
- // A CallableStatement can return more than 1 ResultSets
- // plus a number of update counts.
- boolean hasResultSet = cstmt.execute();
- sample.latencyEnd();
- String sb = resultSetsToString(cstmt,hasResultSet, out);
- return sb.getBytes(ENCODING);
- }
- } else if (UPDATE.equals(_queryType)) {
- stmt = conn.createStatement();
+ }
+ } else if (CALLABLE.equals(_queryType)) {
+ try (CallableStatement cstmt = getCallableStatement(conn)) {
+ int[] out = setArguments(cstmt);
+ // A CallableStatement can return more than 1 ResultSets
+ // plus a number of update counts.
+ boolean hasResultSet = cstmt.execute();
+ sample.latencyEnd();
+ String sb = resultSetsToString(cstmt,hasResultSet, out);
+ return sb.getBytes(ENCODING);
+ }
+ } else if (UPDATE.equals(_queryType)) {
+ try (Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(getIntegerQueryTimeout());
stmt.executeUpdate(getQuery());
sample.latencyEnd();
int updateCount = stmt.getUpdateCount();
String results = updateCount + " updates";
return results.getBytes(ENCODING);
- } else if (PREPARED_SELECT.equals(_queryType)) {
- try (PreparedStatement pstmt = getPreparedStatement(conn)) {
- setArguments(pstmt);
- ResultSet rs = null;
- try {
- rs = pstmt.executeQuery();
- sample.latencyEnd();
- return getStringFromResultSet(rs).getBytes(ENCODING);
- } finally {
- close(rs);
- }
- }
- } else if (PREPARED_UPDATE.equals(_queryType)) {
- try (PreparedStatement pstmt = getPreparedStatement(conn)) {
- setArguments(pstmt);
- pstmt.executeUpdate();
+ }
+ } else if (PREPARED_SELECT.equals(_queryType)) {
+ try (PreparedStatement pstmt = getPreparedStatement(conn)) {
+ setArguments(pstmt);
+ ResultSet rs = null;
+ try {
+ rs = pstmt.executeQuery();
sample.latencyEnd();
- String sb = resultSetsToString(pstmt,false,null);
- return sb.getBytes(ENCODING);
+ return getStringFromResultSet(rs).getBytes(ENCODING);
+ } finally {
+ close(rs);
}
- } else if (ROLLBACK.equals(_queryType)){
- conn.rollback();
- sample.latencyEnd();
- return ROLLBACK.getBytes(ENCODING);
- } else if (COMMIT.equals(_queryType)){
- conn.commit();
- sample.latencyEnd();
- return COMMIT.getBytes(ENCODING);
- } else if (AUTOCOMMIT_FALSE.equals(_queryType)){
- conn.setAutoCommit(false);
- sample.latencyEnd();
- return AUTOCOMMIT_FALSE.getBytes(ENCODING);
- } else if (AUTOCOMMIT_TRUE.equals(_queryType)){
- conn.setAutoCommit(true);
+ }
+ } else if (PREPARED_UPDATE.equals(_queryType)) {
+ try (PreparedStatement pstmt = getPreparedStatement(conn)) {
+ setArguments(pstmt);
+ pstmt.executeUpdate();
sample.latencyEnd();
- return AUTOCOMMIT_TRUE.getBytes(ENCODING);
- } else { // User provided incorrect query type
- throw new UnsupportedOperationException("Unexpected query type: "+_queryType);
+ String sb = resultSetsToString(pstmt,false,null);
+ return sb.getBytes(ENCODING);
}
- } finally {
- close(stmt);
+ } else if (ROLLBACK.equals(_queryType)){
+ conn.rollback();
+ sample.latencyEnd();
+ return ROLLBACK.getBytes(ENCODING);
+ } else if (COMMIT.equals(_queryType)){
+ conn.commit();
+ sample.latencyEnd();
+ return COMMIT.getBytes(ENCODING);
+ } else if (AUTOCOMMIT_FALSE.equals(_queryType)){
+ conn.setAutoCommit(false);
+ sample.latencyEnd();
+ return AUTOCOMMIT_FALSE.getBytes(ENCODING);
+ } else if (AUTOCOMMIT_TRUE.equals(_queryType)){
+ conn.setAutoCommit(true);
+ sample.latencyEnd();
+ return AUTOCOMMIT_TRUE.getBytes(ENCODING);
+ } else { // User provided incorrect query type
+ throw new UnsupportedOperationException("Unexpected query type: "+_queryType);
}
}