From 2fb797a197dc21f4f75f24313a65270ee7104b87 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Thu, 1 Dec 2011 20:05:45 +0000
Subject: [PATCH] Throw an exception rather than blocking if a transaction is
 started after the database has closed.

---
 components/net/sf/briar/db/H2Database.java   |  1 +
 components/net/sf/briar/db/JdbcDatabase.java | 14 ++++----------
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/components/net/sf/briar/db/H2Database.java b/components/net/sf/briar/db/H2Database.java
index c28fc615a9..ab51d7cc3c 100644
--- a/components/net/sf/briar/db/H2Database.java
+++ b/components/net/sf/briar/db/H2Database.java
@@ -55,6 +55,7 @@ class H2Database extends JdbcDatabase {
 	}
 
 	public void close() throws DbException {
+		// H2 will close the database when the last connection closes
 		try {
 			super.closeAllConnections();
 		} catch(SQLException e) {
diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java
index c9d40d7d5d..9018ddb7d7 100644
--- a/components/net/sf/briar/db/JdbcDatabase.java
+++ b/components/net/sf/briar/db/JdbcDatabase.java
@@ -1,6 +1,7 @@
 package net.sf.briar.db;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
@@ -297,8 +298,8 @@ abstract class JdbcDatabase implements Database<Connection> {
 	protected void open(boolean resume, File dir, String driverClass)
 	throws DbException, IOException {
 		if(resume) {
-			if(!dir.exists()) throw new DbException();
-			if(!dir.isDirectory()) throw new DbException();
+			if(!dir.exists()) throw new FileNotFoundException();
+			if(!dir.isDirectory()) throw new FileNotFoundException();
 		} else {
 			if(dir.exists()) FileUtils.delete(dir);
 		}
@@ -388,14 +389,7 @@ abstract class JdbcDatabase implements Database<Connection> {
 	public Connection startTransaction() throws DbException {
 		Connection txn = null;
 		synchronized(connections) {
-			// If the database has been closed, don't return
-			while(closed) {
-				try {
-					connections.wait();
-				} catch(InterruptedException e) {
-					Thread.currentThread().interrupt();
-				}
-			}
+			if(closed) throw new DbException();
 			txn = connections.poll();
 		}
 		try {
-- 
GitLab