adoroszlai commented on a change in pull request #221: HDDS-2538. Fix issues 
found in DatabaseHelper.
URL: https://github.com/apache/hadoop-ozone/pull/221#discussion_r347784112
 
 

 ##########
 File path: 
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/audit/parser/common/DatabaseHelper.java
 ##########
 @@ -90,50 +100,54 @@ private static void loadProperties() {
   }
 
   private static boolean createAuditTable(String dbName) {
-
-    try(Connection connection = getConnection(dbName);
-        Statement st = connection.createStatement()) {
-
-      st.executeUpdate(properties.get(ParserConsts.CREATE_AUDIT_TABLE));
+    try (Connection connection = getConnection(dbName)) {
+      if (connection != null) {
+        try (Statement st = connection.createStatement()) {
+          st.executeUpdate(properties.get(ParserConsts.CREATE_AUDIT_TABLE));
+        }
+      }
     } catch (SQLException e) {
       LOG.error(e.getMessage());
       return false;
     }
     return true;
 
 Review comment:
   The case when `getConnection` returns `null` (eg. if SQLite driver JAR is 
missing) is mishandled in both old and new code:
   
   old:
   
   ```
   $ ozone auditparser adsf load asdf
   2019-11-19 07:51:50,175 ERROR common.DatabaseHelper: org.sqlite.JDBC
   java.lang.NullPointerException
        at 
org.apache.hadoop.ozone.audit.parser.common.DatabaseHelper.createAuditTable(DatabaseHelper.java:95)
        at 
org.apache.hadoop.ozone.audit.parser.common.DatabaseHelper.setup(DatabaseHelper.java:47)
        at 
org.apache.hadoop.ozone.audit.parser.handler.LoadCommandHandler.call(LoadCommandHandler.java:45)
   ```
   
   new:
   
   ```
   ozone auditparser asdf load asdf
   2019-11-19 08:13:03,723 ERROR common.DatabaseHelper: org.sqlite.JDBC
   2019-11-19 08:13:03,724 ERROR common.DatabaseHelper: org.sqlite.JDBC
   asdf has been loaded successfully
   ```
   
   I recommend fixing this by: instead of catching exceptions in 
`getConnection`, let the callers handle it (which they already do partly).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to