Make sure we close read-only transactions 93/19793/2
authorRobert Varga <rovarga@cisco.com>
Thu, 7 May 2015 09:45:32 +0000 (11:45 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 7 May 2015 13:50:58 +0000 (13:50 +0000)
Transactions are resources, so they should be closed when no longer in
use.

Change-Id: I45a8a89f56b61cb12addbdb3349ab408c8aa14ed
Signed-off-by: Robert Varga <rovarga@cisco.com>
(cherry picked from commit 2190b2ce0cfc448d5a9ea6af4978e4167734dad7)

bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/PeerTest.java

index cc68033f6cf5f23b2ce84e24e433731c860fedd2..86308483409b28a6094989514b537ce90215caad 100644 (file)
@@ -19,6 +19,7 @@ import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import javax.annotation.concurrent.ThreadSafe;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
@@ -242,8 +243,8 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
 
     @Override
     public long getRoutesCount(final TablesKey key) {
-        try {
-            final Optional<Tables> tableMaybe = this.dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
+        try (final ReadOnlyTransaction tx = this.dataBroker.newReadOnlyTransaction()) {
+            final Optional<Tables> tableMaybe = tx.read(LogicalDatastoreType.OPERATIONAL,
                     getInstanceIdentifier().child(LocRib.class).child(Tables.class, key)).checkedGet();
             if (tableMaybe.isPresent()) {
                 final Tables table = tableMaybe.get();
@@ -260,7 +261,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
                 }
             }
         } catch (final ReadFailedException e) {
-            //no-op
+            LOG.debug("Failed to read tables", e);
         }
         return 0;
     }
index ba40b04080e5ceb52aba6f2396bd6dfa3daf1863..71781f22d8bd059fa60704e2f634dd1572bbf3a3 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.bgp.rib.impl;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.mockito.Matchers.any;
-
 import com.google.common.base.Optional;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Lists;
@@ -231,6 +230,7 @@ public class PeerTest {
         this.peer = new ApplicationPeer(new ApplicationRibId("t"), new Ipv4Address("127.0.0.1"), this.r);
         this.r.onGlobalContextUpdated(schemaContext);
         final ReadOnlyTransaction readTx = Mockito.mock(ReadOnlyTransaction.class);
+        Mockito.doNothing().when(readTx).close();
         Mockito.doReturn(readTx).when(this.dps).newReadOnlyTransaction();
         final CheckedFuture<Optional<DataObject>, ReadFailedException> readFuture = Mockito.mock(CheckedFuture.class);
         Mockito.doReturn(Optional.<DataObject>absent()).when(readFuture).checkedGet();