Close read-only transactions 50/81950/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 May 2019 17:54:18 +0000 (19:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 May 2019 17:54:18 +0000 (19:54 +0200)
Transactions are resources just as any other, make sure we close
them as soon as we do not need them.

Change-Id: Ic11becba1a76f2aeecbf5c1e17401f6a55fd84cc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/IetfZeroTouchCallHomeServerProvider.java
netconf/messagebus-netconf/src/main/java/org/opendaylight/netconf/messagebus/eventsources/netconf/NetconfEventSourceMount.java

index 7bff5acd4a661b7c8b02ad07e83a5b8b18ef279b..035de3946c284f464d199ecc7d95f543e78f5326 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.netconf.callhome.mount;
 
 import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.io.IOException;
 import java.security.GeneralSecurityException;
@@ -209,12 +208,9 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     }
 
     private Optional<Device> readDevice(final NodeId nodeId) {
-        ReadTransaction opTx = dataBroker.newReadOnlyTransaction();
-
-        InstanceIdentifier<Device> deviceIID = buildDeviceInstanceIdentifier(nodeId);
-        ListenableFuture<Optional<Device>> devFuture = opTx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
-        try {
-            return devFuture.get();
+        try (ReadTransaction opTx = dataBroker.newReadOnlyTransaction()) {
+            InstanceIdentifier<Device> deviceIID = buildDeviceInstanceIdentifier(nodeId);
+            return opTx.read(LogicalDatastoreType.OPERATIONAL, deviceIID).get();
         } catch (InterruptedException | ExecutionException e) {
             return Optional.empty();
         }
@@ -281,11 +277,9 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     }
 
     private AllowedDevices getDevices() {
-        ReadTransaction rxTransaction = dataBroker.newReadOnlyTransaction();
-        ListenableFuture<Optional<AllowedDevices>> devicesFuture =
-                rxTransaction.read(LogicalDatastoreType.OPERATIONAL, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
-        try {
-            return devicesFuture.get().orElse(null);
+        try (ReadTransaction rxTransaction = dataBroker.newReadOnlyTransaction()) {
+            return rxTransaction.read(LogicalDatastoreType.OPERATIONAL, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES)
+                    .get().orElse(null);
         } catch (ExecutionException | InterruptedException e) {
             LOG.error("Error trying to read the whitelist devices", e);
             return null;
index d2dbec65d27e856154f65c7fb36bd83583d1eb40..c1bdc628d5ee6d597a9d3168cb409cfc146b82f5 100644 (file)
@@ -147,9 +147,11 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
         // its created under CallHomeAuthorizationProvider.
         // Will have to redesign a bit here.
         // CallHomeAuthorization.
-        ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
-        ListenableFuture<Optional<AllowedDevices>> devicesFuture = roConfigTx
-                .read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+        final ListenableFuture<Optional<AllowedDevices>> devicesFuture;
+        try (ReadTransaction roConfigTx = dataBroker.newReadOnlyTransaction()) {
+            devicesFuture = roConfigTx.read(LogicalDatastoreType.CONFIGURATION,
+                IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+        }
 
         Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
         for (DataTreeModification<AllowedDevices> change : changes) {
index b5f980172235ca81459c089ff05ef737d4af4adc..223354d615b4b6e2367d246545f3c8527a2c4946 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.netconf.messagebus.eventsources.netconf;
 
 import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.time.Instant;
 import java.time.ZoneId;
@@ -142,10 +141,10 @@ class NetconfEventSourceMount {
      * @throws InterruptedException if data read fails
      */
     List<Stream> getAvailableStreams() throws InterruptedException, ExecutionException {
-        DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction();
-        FluentFuture<Optional<NormalizedNode<?, ?>>> checkFeature = tx.read(LogicalDatastoreType.OPERATIONAL,
-            STREAMS_PATH);
-        Optional<NormalizedNode<?, ?>> streams = checkFeature.get();
+        final Optional<NormalizedNode<?, ?>> streams;
+        try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
+            streams = tx.read(LogicalDatastoreType.OPERATIONAL, STREAMS_PATH).get();
+        }
         if (streams.isPresent()) {
             Streams streams1 = (Streams) CODEC_REGISTRY.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
             return streams1.getStream();