This is an automated email from the ASF dual-hosted git repository.

amanin pushed a commit to branch refactor/strict_storage_connector
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to 
refs/heads/refactor/strict_storage_connector by this push:
     new 8572c20  fix(Storage): improve closing logic of strict storage 
connector.
8572c20 is described below

commit 8572c20de958ef7ab7f60f8191aed47b30558f83
Author: Alexis Manin <[email protected]>
AuthorDate: Thu Mar 11 16:03:49 2021 +0100

    fix(Storage): improve closing logic of strict storage connector.
    
    Retain only a weak reference for the commited storage, so if caller does 
not use it anymore but forgot to close it, we still got a chance to do it 
ourself
    
    Properly delegate close operation.
---
 .../main/java/org/apache/sis/storage/StrictStorageConnector.java   | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git 
a/storage/sis-storage/src/main/java/org/apache/sis/storage/StrictStorageConnector.java
 
b/storage/sis-storage/src/main/java/org/apache/sis/storage/StrictStorageConnector.java
index af6be34..22754c7 100644
--- 
a/storage/sis-storage/src/main/java/org/apache/sis/storage/StrictStorageConnector.java
+++ 
b/storage/sis-storage/src/main/java/org/apache/sis/storage/StrictStorageConnector.java
@@ -3,6 +3,7 @@ package org.apache.sis.storage;
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
+import java.lang.ref.WeakReference;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.nio.channels.SeekableByteChannel;
@@ -72,7 +73,7 @@ public class StrictStorageConnector implements AutoCloseable {
 
     private final StorageConnector storage;
 
-    private Object committedStorage;
+    private WeakReference<Object> committedStorage;
     private volatile int concurrentFlag;
 
     public StrictStorageConnector(StorageConnector storage) {
@@ -86,7 +87,7 @@ public class StrictStorageConnector implements AutoCloseable {
             doUnderControl(() -> {
                 concurrentFlag = -1;
                 storage.closeAllExcept(view);
-                committedStorage = view;
+                committedStorage = (view == null ? null : new 
WeakReference<>(view));
                 return null;
             });
         } catch (IOException e) {
@@ -259,7 +260,7 @@ public class StrictStorageConnector implements 
AutoCloseable {
 
     @Override
     public void close() throws IOException, DataStoreException {
-        storage.closeAllExcept(committedStorage);
+        closeAllExcept(committedStorage == null ? null : 
committedStorage.get());
     }
 
     public <T> T getOption(OptionKey<T> key) {

Reply via email to