From f3eb32e2ec59a581fdb9d97636dc0c1dac2495e1 Mon Sep 17 00:00:00 2001 From: Winston Li Date: Tue, 23 Aug 2016 19:07:23 +0100 Subject: [PATCH] Catch more exceptions in db init --- .../bridge/db/sqlite/SqliteDBStore.java | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java index 08b1d5d646..ea92393eb7 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/bridge/db/sqlite/SqliteDBStore.java @@ -24,29 +24,13 @@ public class SqliteDBStore implements DBStore { private final Connection connection; - public SqliteDBStore( - File dbFile - ) { - File parentDir = dbFile.getParentFile(); - if (!parentDir.exists() && !parentDir.mkdirs()) { - throw new DBInitException( - parentDir.getAbsolutePath() + " directory didn't exist, " + - "and unable to create. Check your permissions." - ); - } + public SqliteDBStore(File dbFile) { try { - Class.forName("org.sqlite.JDBC"); - } catch (ClassNotFoundException e) { - throw new DBInitException(e); + connection = openConnectionTo(dbFile); + createTables(); + } catch (Throwable t) { + throw new DBInitException(t); } - try { - connection = DriverManager.getConnection( - "jdbc:sqlite:" + dbFile.getAbsolutePath() - ); - } catch (SQLException e) { - throw new DBInitException("Unable to connect to DB", e); - } - createTables(); } @Override @@ -107,6 +91,28 @@ public class SqliteDBStore implements DBStore { throw new UnsupportedOperationException(); } + private Connection openConnectionTo(File dbFile) { + File parentDir = dbFile.getParentFile(); + if (!parentDir.exists() && !parentDir.mkdirs()) { + throw new DBInitException( + parentDir.getAbsolutePath() + " directory didn't exist, " + + "and unable to create. Check your permissions." + ); + } + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + throw new DBInitException(e); + } + try { + return DriverManager.getConnection( + "jdbc:sqlite:" + dbFile.getAbsolutePath() + ); + } catch (SQLException e) { + throw new DBInitException("Unable to connect to DB", e); + } + } + private void createTables() { Stream.of( new CreateProjectsTableSQLUpdate(),