Skip to content

Commit

Permalink
Fixes #40
Browse files Browse the repository at this point in the history
Closing database on error

(cherry picked from commit 9f7f9be)
  • Loading branch information
Christian Peter committed Jun 3, 2021
1 parent 176cf2f commit ff51b1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/sqlite/SQLiteConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ public SQLiteConnection(String url, String fileName) throws SQLException {
public SQLiteConnection(String url, String fileName, Properties prop) throws SQLException {
this.db = open(url, fileName, prop);
SQLiteConfig config = db.getConfig();
this.connectionConfig = db.getConfig().newConnectionConfig();

config.apply(this);
this.connectionConfig = config.newConnectionConfig();
try {
config.apply(this);
} catch (SQLException throwable) {
this.db.close();
throw throwable;
}
}

public SQLiteConnectionConfig getConnectionConfig() {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/sqlite/SQLiteMCPragmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,19 @@ public void crossCipherAlgorithmTest() throws IOException, SQLException {
// c.close();
}

@Test
public void closeDeleteTest() throws IOException, SQLException {
String dbfile = createFile();
String key = "key";
cipherDatabaseCreate(new SQLiteMCConfig(), dbfile, key);

Connection c = cipherDatabaseOpen(new SQLiteMCConfig(), dbfile, key);
assertTrue("Should be able to read the base db", databaseIsReadable(c));
c.close();

c = cipherDatabaseOpen(SQLiteMCRC4Config.getDefault(), dbfile, key);
assertNull("Should not be readable with RC4", c);
assertTrue("Connection must be closed, should be deleted", new File(dbfile).delete());
}

}

0 comments on commit ff51b1e

Please sign in to comment.