Skip to content

Commit

Permalink
fix: PgDatabaseMetaData: close statement in finally
Browse files Browse the repository at this point in the history
closes #516
  • Loading branch information
George Kankava authored and vlsi committed Jun 22, 2016
1 parent 510e6e0 commit b4c45ca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
47 changes: 28 additions & 19 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.postgresql.core.Oid;
import org.postgresql.core.ServerVersion;
import org.postgresql.util.GT;
import org.postgresql.util.JdbcBlackHole;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

Expand Down Expand Up @@ -70,17 +71,21 @@ protected int getMaxIndexKeys() throws SQLException {
+ " t1.typelem=t2.oid AND t1.typname='oidvector'";
}
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (!rs.next()) {
stmt.close();
throw new PSQLException(
GT.tr(
"Unable to determine a value for MaxIndexKeys due to missing system catalog data."),
PSQLState.UNEXPECTED_ERROR);
}
INDEX_MAX_KEYS = rs.getInt(1);
rs.close();
stmt.close();
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
if (!rs.next()) {
stmt.close();
throw new PSQLException(
GT.tr(
"Unable to determine a value for MaxIndexKeys due to missing system catalog data."),
PSQLState.UNEXPECTED_ERROR);
}
INDEX_MAX_KEYS = rs.getInt(1);
} finally {
JdbcBlackHole.close(rs);
JdbcBlackHole.close(stmt);
}
}
return INDEX_MAX_KEYS;
}
Expand All @@ -95,14 +100,18 @@ protected int getMaxNameLength() throws SQLException {
sql = "SELECT typlen FROM pg_type WHERE typname='name'";
}
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (!rs.next()) {
throw new PSQLException(GT.tr("Unable to find name datatype in the system catalogs."),
PSQLState.UNEXPECTED_ERROR);
}
NAMEDATALEN = rs.getInt("typlen");
rs.close();
stmt.close();
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
if (!rs.next()) {
throw new PSQLException(GT.tr("Unable to find name datatype in the system catalogs."),
PSQLState.UNEXPECTED_ERROR);
}
NAMEDATALEN = rs.getInt("typlen");
} finally {
JdbcBlackHole.close(rs);
JdbcBlackHole.close(stmt);
}
}
return NAMEDATALEN - 1;
}
Expand Down
38 changes: 38 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/util/JdbcBlackHole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.postgresql.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcBlackHole {
public static void close(Connection con) {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
/* ignore for now */
}
}

public static void close(Statement s) {
try {
if (s != null) {
s.close();
}
} catch (SQLException e) {
/* ignore for now */
}
}

public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
/* ignore for now */
}
}
}

0 comments on commit b4c45ca

Please sign in to comment.