mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-22 15:49:53 +01:00
Integrate merge request feedback
This commit is contained in:
@@ -89,7 +89,6 @@ class H2Database extends JdbcDatabase {
|
|||||||
c = createConnection();
|
c = createConnection();
|
||||||
super.closeAllConnections();
|
super.closeAllConnections();
|
||||||
setDirty(c, false);
|
setDirty(c, false);
|
||||||
c.commit();
|
|
||||||
c.close();
|
c.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(c, LOG, WARNING);
|
tryToClose(c, LOG, WARNING);
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
super.closeAllConnections();
|
super.closeAllConnections();
|
||||||
c = createConnection();
|
c = createConnection();
|
||||||
setDirty(c, false);
|
setDirty(c, false);
|
||||||
c.commit();
|
|
||||||
s = c.createStatement();
|
s = c.createStatement();
|
||||||
s.executeQuery("SHUTDOWN");
|
s.executeQuery("SHUTDOWN");
|
||||||
s.close();
|
s.close();
|
||||||
|
|||||||
@@ -355,13 +355,13 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
@GuardedBy("connectionsLock")
|
@GuardedBy("connectionsLock")
|
||||||
private boolean closed = false;
|
private boolean closed = false;
|
||||||
|
|
||||||
private boolean wasDirtyOnInitialisation = false;
|
private volatile boolean wasDirtyOnInitialisation = false;
|
||||||
|
|
||||||
protected abstract Connection createConnection()
|
protected abstract Connection createConnection()
|
||||||
throws DbException, SQLException;
|
throws DbException, SQLException;
|
||||||
|
|
||||||
// Used exclusively during open to compact the database after schema
|
// Used exclusively during open to compact the database after schema
|
||||||
// migrations and after DatabaseConstants#MAX_COMPACTION_INTERVAL_MS has
|
// migrations or after DatabaseConstants#MAX_COMPACTION_INTERVAL_MS has
|
||||||
// elapsed
|
// elapsed
|
||||||
protected abstract void compactAndClose() throws DbException;
|
protected abstract void compactAndClose() throws DbException;
|
||||||
|
|
||||||
|
|||||||
@@ -2354,37 +2354,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShutdownGracefully() throws Exception {
|
public void testShutdownGracefully() throws Exception {
|
||||||
CountDownLatch closing = new CountDownLatch(1);
|
|
||||||
CountDownLatch closed = new CountDownLatch(1);
|
|
||||||
AtomicBoolean transactionFinished = new AtomicBoolean(false);
|
|
||||||
AtomicBoolean error = new AtomicBoolean(false);
|
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
|
db.close();
|
||||||
// Start a transaction
|
|
||||||
Connection txn = db.startTransaction();
|
|
||||||
// In another thread, close the database
|
|
||||||
Thread close = new Thread(() -> {
|
|
||||||
try {
|
|
||||||
closing.countDown();
|
|
||||||
db.close();
|
|
||||||
if (!transactionFinished.get()) error.set(true);
|
|
||||||
closed.countDown();
|
|
||||||
} catch (Exception e) {
|
|
||||||
error.set(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
close.start();
|
|
||||||
closing.await();
|
|
||||||
// Do whatever the transaction needs to do
|
|
||||||
Thread.sleep(10);
|
|
||||||
transactionFinished.set(true);
|
|
||||||
// Abort the transaction
|
|
||||||
db.abortTransaction(txn);
|
|
||||||
// The other thread should now terminate
|
|
||||||
assertTrue(closed.await(5, SECONDS));
|
|
||||||
// Check that the other thread didn't encounter an error
|
|
||||||
assertFalse(error.get());
|
|
||||||
|
|
||||||
open(true);
|
open(true);
|
||||||
assertFalse(db.wasDirtyOnInitialisation());
|
assertFalse(db.wasDirtyOnInitialisation());
|
||||||
}
|
}
|
||||||
@@ -2393,6 +2364,35 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
public void testShutdownDirty() throws Exception {
|
public void testShutdownDirty() throws Exception {
|
||||||
Database<Connection> db = open(false);
|
Database<Connection> db = open(false);
|
||||||
|
|
||||||
|
// We want to simulate a dirty shutdown here which would normally be
|
||||||
|
// caused by an empty battery or by force closing the Android app.
|
||||||
|
// As there is no obvious way to simulate this, we're artificially
|
||||||
|
// causing an SqlException during close() here by unloading the JDBC
|
||||||
|
// drivers.
|
||||||
|
List<String> unloadedDrivers = unloadDrivers();
|
||||||
|
|
||||||
|
try {
|
||||||
|
db.close();
|
||||||
|
fail();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// continue
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reloading drivers to continue so that we're able to work with the
|
||||||
|
// database again.
|
||||||
|
reloadDrivers(unloadedDrivers);
|
||||||
|
|
||||||
|
db = open(true);
|
||||||
|
assertTrue(db.wasDirtyOnInitialisation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShutdownDirtyThenGracefully() throws Exception {
|
||||||
|
Database<Connection> db = open(false);
|
||||||
|
|
||||||
|
// Simulating a dirty shutdown here, look at #testShutdownDirty for
|
||||||
|
// details.
|
||||||
List<String> unloadedDrivers = unloadDrivers();
|
List<String> unloadedDrivers = unloadDrivers();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -2406,6 +2406,10 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
|
|
||||||
db = open(true);
|
db = open(true);
|
||||||
assertTrue(db.wasDirtyOnInitialisation());
|
assertTrue(db.wasDirtyOnInitialisation());
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
db = open(true);
|
||||||
|
assertFalse(db.wasDirtyOnInitialisation());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Database<Connection> open(boolean resume) throws Exception {
|
private Database<Connection> open(boolean resume) throws Exception {
|
||||||
@@ -2473,6 +2477,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
unloaded.add(d.getClass().getName());
|
unloaded.add(d.getClass().getName());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return unloaded;
|
return unloaded;
|
||||||
|
|||||||
Reference in New Issue
Block a user