Close read-only transaction in BgpDeployerImpl 32/98032/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 21 Oct 2021 12:47:03 +0000 (14:47 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 21 Oct 2021 12:47:03 +0000 (14:47 +0200)
We are not closing the transaction, which is rather inefficient. Fix
that through a simple try-with-resources.

Change-Id: I2cdfa734e83daef9a968031a289b6406eb031e4a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpDeployerImpl.java

index 47b9848b88eee4baf79739e764fc373a332f08e0..936439651201f79eec4e9027f9ca89fa9aacbda7 100644 (file)
@@ -69,15 +69,14 @@ public final class BgpDeployerImpl implements ClusteredDataTreeChangeListener<Bg
     @GuardedBy("this")
     private final Map<InstanceIdentifier<Bgp>, BGPClusterSingletonService> bgpCss = new HashMap<>();
     private final DataBroker dataBroker;
-    private final LoadingCache<InstanceIdentifier<PeerGroup>, Optional<PeerGroup>> peerGroups
-            = CacheBuilder.newBuilder()
-            .build(new CacheLoader<InstanceIdentifier<PeerGroup>, Optional<PeerGroup>>() {
-                @Override
-                public Optional<PeerGroup> load(final InstanceIdentifier<PeerGroup> key)
-                        throws ExecutionException, InterruptedException {
-                    return loadPeerGroup(key);
-                }
-            });
+    private final LoadingCache<InstanceIdentifier<PeerGroup>, Optional<PeerGroup>> peerGroups =
+        CacheBuilder.newBuilder().build(new CacheLoader<InstanceIdentifier<PeerGroup>, Optional<PeerGroup>>() {
+            @Override
+            public Optional<PeerGroup> load(final InstanceIdentifier<PeerGroup> key)
+                    throws ExecutionException, InterruptedException {
+                return loadPeerGroup(key);
+            }
+        });
     private final String networkInstanceName;
     private ListenerRegistration<BgpDeployerImpl> registration;
     @GuardedBy("this")
@@ -122,8 +121,11 @@ public final class BgpDeployerImpl implements ClusteredDataTreeChangeListener<Bg
             justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private Optional<PeerGroup> loadPeerGroup(final InstanceIdentifier<PeerGroup> peerGroupIid)
             throws ExecutionException, InterruptedException {
-        final ReadTransaction tr = this.dataBroker.newReadOnlyTransaction();
-        return tr.read(LogicalDatastoreType.CONFIGURATION, peerGroupIid).get();
+        final FluentFuture<Optional<PeerGroup>> future;
+        try (ReadTransaction tx = this.dataBroker.newReadOnlyTransaction()) {
+            future = tx.read(LogicalDatastoreType.CONFIGURATION, peerGroupIid);
+        }
+        return future.get();
     }
 
     @Override