Skip to content

Commit 8e50775

Browse files
committed
fix(mapdb): close mapdb databases
Fixes #185
1 parent 9c7419a commit 8e50775

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/main/java/com/conveyal/gtfs/BaseGTFSCache.java

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public BaseGTFSCache(String awsRegion, String bucket, String bucketFolder, File
7676
// delete local .zip file ONLY if using s3
7777
if (bucket != null) {
7878
String id = removalNotification.getKey();
79+
// close db to avoid memory leak and mapdb corruption
80+
removalNotification.getValue().close();
7981
String[] extensions = {".zip"}; // used to include ".db", ".db.p" as well. See #119
8082
// delete local cache files (including zip) when feed removed from cache
8183
for (String type : extensions) {

src/main/java/com/conveyal/gtfs/GTFSFeed.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ public GTFSFeed clone() {
842842
}
843843

844844
protected void finalize() throws IOException {
845-
close();
845+
if (!db.isClosed()) LOG.error("MapDB database was not closed before it was garbage collected. This is a bug!");
846846
}
847847

848848
public void close () {
@@ -873,14 +873,14 @@ public GTFSFeed () {
873873
.transactionDisable()
874874
.mmapFileEnable()
875875
.asyncWriteEnable()
876-
.deleteFilesAfterClose()
877876
.compressionEnable()
878-
.make()); // TODO db.close();
877+
.closeOnJvmShutdown()
878+
.make());
879879
}
880880

881881
/** Create a GTFS feed connected to a particular DB, which will be created if it does not exist. */
882882
public GTFSFeed (String dbFile) throws IOException, ExecutionException {
883-
this(constructDB(dbFile)); // TODO db.close();
883+
this(constructDB(dbFile));
884884
}
885885

886886
// One critical point when constructing the MapDB is the instance cache type and size.
@@ -902,6 +902,7 @@ private static DB constructDB(String dbFile) {
902902
.mmapFileEnable()
903903
.asyncWriteEnable()
904904
.compressionEnable()
905+
.closeOnJvmShutdown()
905906
.make();
906907
return db;
907908
} catch (ExecutionError | IOError | Exception e) {

0 commit comments

Comments
 (0)