Catch more exceptions in db init

This commit is contained in:
Winston Li 2016-08-23 19:07:23 +01:00 committed by Michael Mazour
parent cfc02bbcc8
commit f3eb32e2ec

View file

@ -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(),