Author: markt Date: Tue Mar 25 22:58:38 2014 New Revision: 1581579 URL: http://svn.apache.org/r1581579 Log: try with resources
Modified: tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java Modified: tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java?rev=1581579&r1=1581578&r2=1581579&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java (original) +++ tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java Tue Mar 25 22:58:38 2014 @@ -408,37 +408,20 @@ public class DataSourceRealm extends Rea protected String getPassword(Connection dbConnection, String username) { - ResultSet rs = null; - PreparedStatement stmt = null; String dbCredentials = null; - try { - stmt = credentials(dbConnection, username); - rs = stmt.executeQuery(); + try (PreparedStatement stmt = credentials(dbConnection, username); + ResultSet rs = stmt.executeQuery()) { if (rs.next()) { dbCredentials = rs.getString(1); } return (dbCredentials != null) ? dbCredentials.trim() : null; - } catch(SQLException e) { + } catch (SQLException e) { containerLog.error( sm.getString("dataSourceRealm.getPassword.exception", username), e); - } finally { - try { - if (rs != null) { - rs.close(); - } - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - containerLog.error( - sm.getString("dataSourceRealm.getPassword.exception", - username), e); - - } } return null; @@ -499,13 +482,10 @@ public class DataSourceRealm extends Rea return null; } - ResultSet rs = null; - PreparedStatement stmt = null; ArrayList<String> list = null; - try { - stmt = roles(dbConnection, username); - rs = stmt.executeQuery(); + try (PreparedStatement stmt = roles(dbConnection, username); + ResultSet rs = stmt.executeQuery()) { list = new ArrayList<>(); while (rs.next()) { @@ -519,20 +499,6 @@ public class DataSourceRealm extends Rea containerLog.error( sm.getString("dataSourceRealm.getRoles.exception", username), e); } - finally { - try { - if (rs != null) { - rs.close(); - } - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - containerLog.error( - sm.getString("dataSourceRealm.getRoles.exception", - username), e); - } - } return null; } Modified: tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java?rev=1581579&r1=1581578&r2=1581579&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java (original) +++ tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java Tue Mar 25 22:58:38 2014 @@ -507,8 +507,6 @@ public class JDBCRealm // Look up the user's credentials String dbCredentials = null; - PreparedStatement stmt = null; - ResultSet rs = null; // Number of tries is the number of attempts to connect to the database // during this login attempt (if we need to open the database) @@ -524,32 +522,23 @@ public class JDBCRealm // Ensure that we have an open database connection open(); - stmt = credentials(dbConnection, username); - rs = stmt.executeQuery(); - if (rs.next()) { - dbCredentials = rs.getString(1); - } - - dbConnection.commit(); + try (PreparedStatement stmt = credentials(dbConnection, username); + ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + dbCredentials = rs.getString(1); + } - if (dbCredentials != null) { - dbCredentials = dbCredentials.trim(); - } + dbConnection.commit(); - return dbCredentials; + if (dbCredentials != null) { + dbCredentials = dbCredentials.trim(); + } + return dbCredentials; + } } catch (SQLException e) { // Log the problem for posterity containerLog.error(sm.getString("jdbcRealm.exception"), e); - } finally { - if (rs != null) { - try { - rs.close(); - } catch(SQLException e) { - containerLog.warn(sm.getString( - "jdbcRealm.abnormalCloseResultSet")); - } - } } // Close the connection so that it gets reopened next time @@ -560,7 +549,7 @@ public class JDBCRealm numberOfTries--; } - return (null); + return null; } @@ -588,9 +577,6 @@ public class JDBCRealm return null; } - PreparedStatement stmt = null; - ResultSet rs = null; - // Number of tries is the number of attempts to connect to the database // during this login attempt (if we need to open the database) // This needs rewritten wuth better pooling support, the existing code @@ -602,46 +588,32 @@ public class JDBCRealm int numberOfTries = 2; while (numberOfTries>0) { try { - // Ensure that we have an open database connection open(); - try { + try (PreparedStatement stmt = roles(dbConnection, username); + ResultSet rs = stmt.executeQuery()) { // Accumulate the user's roles ArrayList<String> roleList = new ArrayList<>(); - stmt = roles(dbConnection, username); - rs = stmt.executeQuery(); + while (rs.next()) { String role = rs.getString(1); if (null!=role) { roleList.add(role.trim()); } } - rs.close(); - rs = null; - - return (roleList); + return roleList; } finally { - if (rs!=null) { - try { - rs.close(); - } catch(SQLException e) { - containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet")); - } - } dbConnection.commit(); } - } catch (SQLException e) { - // Log the problem for posterity containerLog.error(sm.getString("jdbcRealm.exception"), e); // Close the connection so that it gets reopened next time if (dbConnection != null) close(dbConnection); - } numberOfTries--; --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org