If you try to upsert a large collection that contains new and updated items the operation will fail.
Failed to acquire lock for collection file X.json
java.nio.channels.ClosedChannelException: null
at java.base/sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:160) ~[?:?]
at java.base/sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1463) ~[?:?]
at java.base/java.nio.channels.FileChannel.lock(FileChannel.java:1240) ~[?:?]
at io.jsondb.io.JsonWriter.acquireLock(JsonWriter.java:101) ~[jsondb-core-1.0.115-j11.jar:?]
at io.jsondb.io.JsonWriter.updateInJsonFile(JsonWriter.java:601) ~[jsondb-core-1.0.115-j11.jar:?]
at io.jsondb.JsonDBTemplate.upsert(JsonDBTemplate.java:1257) ~[jsondb-core-1.0.115-j11.jar:?]
at io.jsondb.JsonDBTemplate.upsert(JsonDBTemplate.java:1194) ~[jsondb-core-1.0.115-j11.jar:?]
After the items get appended, the file channel will get closed, causing the update to fail since its unable to obtain the file lock.
|
if (collectionToInsert.size() > 0) { |
|
boolean insertResult = jw.appendToJsonFile(collection.values(), collectionToInsert.values()); |
|
if(insertResult) { |
|
collection.putAll(collectionToInsert); |
|
} |
|
} |
|
|
|
if (collectionToUpdate.size() > 0) { |
|
boolean updateResult = jw.updateInJsonFile(collection, collectionToUpdate); |
|
if (updateResult) { |
|
collection.putAll(collectionToUpdate); |
|
} |
|
private void releaseLock(FileLock lock) { |
|
try { |
|
if(lock != null && lock.isValid()) { |
|
lock.release(); |
|
} |
|
} catch (IOException e) { |
|
logger.error("Failed to release lock for collection file {}", collectionFile.getName(), e); |
|
} |
|
try { |
|
channel.close(); |
|
} catch (IOException e) { |
|
logger.error("Failed to close FileChannel for collection file {}", collectionFile.getName(), e); |
|
} |
|
try { |
|
raf.close(); |
|
} catch (IOException e) { |
|
logger.error("Failed to close RandomAccessFile for collection file {}", collectionFile.getName(), e); |
|
} |
|
} |
|
private FileLock acquireLock() throws IOException { |
|
try { |
|
FileLock fileLock= channel.lock(); |
|
return fileLock; |
|
} catch (IOException e) { |
|
try { |
|
channel.close(); |
|
raf.close(); |
|
} catch (IOException e1) { |
|
logger.error("Failed while closing RandomAccessFile for collection file {}", collectionFile.getName()); |
|
} |
|
throw e; |
|
} |
|
} |
If you try to upsert a large collection that contains new and updated items the operation will fail.
After the items get appended, the file channel will get closed, causing the update to fail since its unable to obtain the file lock.
jsondb-core/src/main/java/io/jsondb/JsonDBTemplate.java
Lines 1257 to 1268 in 12b2c72
jsondb-core/src/main/java/io/jsondb/io/JsonWriter.java
Lines 114 to 132 in 12b2c72
jsondb-core/src/main/java/io/jsondb/io/JsonWriter.java
Lines 99 to 112 in 12b2c72