Permalink
Browse files

fix: PgDatabaseMetaData: close statement in finally

closes #516
  • Loading branch information...
George Kankava authored and vlsi committed Jun 22, 2016
1 parent 510e6e0 commit b4c45ca20c338810da9db328d61df298acd1d00b
@@ -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;
@@ -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;
}
@@ -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;
}
@@ -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.