From 90c323e82b30c0d4ddc9edf89ab648812368be99 Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Fri, 31 May 2013 15:31:15 +0100 Subject: [PATCH] Set H2's write delay to 0 and flush data to disk after every commit. This should improve durability in the case of a crash (see http://www.h2database.com/html/advanced.html#durability_problems). The performance penalty for H2DatabaseTest is roughly 10%. --- briar-core/src/net/sf/briar/db/H2Database.java | 10 +++++++--- briar-core/src/net/sf/briar/db/JdbcDatabase.java | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/briar-core/src/net/sf/briar/db/H2Database.java b/briar-core/src/net/sf/briar/db/H2Database.java index 600b0375b8..4455ee89c7 100644 --- a/briar-core/src/net/sf/briar/db/H2Database.java +++ b/briar-core/src/net/sf/briar/db/H2Database.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.sql.Statement; import java.util.Properties; import net.sf.briar.api.clock.Clock; @@ -31,8 +32,8 @@ class H2Database extends JdbcDatabase { super(HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE, clock); this.config = config; String path = new File(config.getDatabaseDirectory(), "db").getPath(); - url = "jdbc:h2:split:" + path - + ";CIPHER=AES;MULTI_THREADED=1;DB_CLOSE_ON_EXIT=false"; + url = "jdbc:h2:split:" + path + ";CIPHER=AES;MULTI_THREADED=1" + + ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false"; } public boolean open() throws DbException, IOException { @@ -73,7 +74,6 @@ class H2Database extends JdbcDatabase { } else return f.length(); } - @Override protected Connection createConnection() throws SQLException { byte[] key = config.getEncryptionKey(); if(key == null) throw new IllegalStateException(); @@ -101,4 +101,8 @@ class H2Database extends JdbcDatabase { for(int i = 0; i < hex.length; i++) hex[i] = 0; return combined; } + + protected void flushBuffersToDisk(Statement s) throws SQLException { + s.execute("CHECKPOINT SYNC"); + } } diff --git a/briar-core/src/net/sf/briar/db/JdbcDatabase.java b/briar-core/src/net/sf/briar/db/JdbcDatabase.java index 41202a4c94..bf70e237fe 100644 --- a/briar-core/src/net/sf/briar/db/JdbcDatabase.java +++ b/briar-core/src/net/sf/briar/db/JdbcDatabase.java @@ -371,6 +371,7 @@ abstract class JdbcDatabase implements Database<Connection> { private boolean closed = false; // Locking: connections protected abstract Connection createConnection() throws SQLException; + protected abstract void flushBuffersToDisk(Statement s) throws SQLException; JdbcDatabase(String hashType, String binaryType, String counterType, String secretType, Clock clock) { @@ -509,9 +510,14 @@ abstract class JdbcDatabase implements Database<Connection> { } public void commitTransaction(Connection txn) throws DbException { + Statement s = null; try { txn.commit(); + s = txn.createStatement(); + flushBuffersToDisk(s); + s.close(); } catch(SQLException e) { + tryToClose(s); throw new DbException(e); } synchronized(connections) { -- GitLab