From 3ad914d73f06346d4b465c5fd5d7983e1d5e5f3c 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 4afef45428..4f8122d6bf 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 @@ -53,7 +53,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(); @@ -148,7 +148,9 @@ public class DatabaseMetadata { } finally { - rs.close(); + if ( rs != null ) { + rs.close(); + } } } catch (SQLException sqlException) { @@ -179,10 +181,13 @@ public class DatabaseMetadata { } } finally { - rs.close(); - statement.close(); + if ( rs != null ) { + rs.close(); + } + if ( statement != null ) { + statement.close(); + } } - } } } @@ -195,30 +200,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(); } }