From 3b18c275eae6f7d067f30df13007c37ed5b86121 Mon Sep 17 00:00:00 2001 From: msulima Date: Sun, 20 Apr 2014 14:12:20 +0200 Subject: [PATCH] HHH-9136 - DatabaseMetadata: NPE hides exception in finally block - Protection against NPE in finally block - Little formatting fixes --- .../tool/hbm2ddl/DatabaseMetadata.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java index 1e7a833d17..8057c99d21 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java @@ -55,7 +55,7 @@ import org.jboss.logging.Logger; */ public class DatabaseMetadata { - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, DatabaseMetaData.class.getName()); + private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, DatabaseMetaData.class.getName()); private final Map tables = new HashMap(); private final Set sequences = new HashSet(); @@ -157,7 +157,9 @@ public class DatabaseMetadata { } finally { - rs.close(); + if ( rs != null ) { + rs.close(); + } } } catch (SQLException sqlException) { @@ -187,10 +189,13 @@ public class DatabaseMetadata { } } finally { - rs.close(); - statement.close(); + if ( rs != null ) { + rs.close(); + } + if ( statement != null ) { + statement.close(); + } } - } } } @@ -203,30 +208,30 @@ public class DatabaseMetadata { return false; } - public boolean isTable(Object key) throws HibernateException { - if(key instanceof String) { + public boolean isTable(Object key) throws HibernateException { + if(key instanceof String) { Table tbl = new Table((String)key); if ( getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null ) { - return true; - } else { - String[] strings = StringHelper.split(".", (String) key); - if(strings.length==3) { + return true; + } else { + String[] strings = StringHelper.split(".", (String) key); + if(strings.length==3) { tbl = new Table(strings[2]); tbl.setCatalog(strings[0]); tbl.setSchema(strings[1]); return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null; - } else if (strings.length==2) { + } else if (strings.length==2) { tbl = new Table(strings[1]); tbl.setSchema(strings[0]); return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(), tbl.isQuoted() ) != null; - } - } - } - return false; - } + } + } + } + return false; + } @Override - public String toString() { + public String toString() { return "DatabaseMetadata" + tables.keySet().toString() + sequences.toString(); } }