From f1d68efbd516ea37ed12a0d2da430b59b8e418f9 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 30 Jan 2007 03:19:58 +0000 Subject: More cursor closing paranoia. --- .../java/com/amazon/carbonado/layout/Layout.java | 14 ++++-- .../carbonado/repo/indexed/IndexedRepository.java | 10 ++-- .../carbonado/repo/indexed/ManagedIndex.java | 53 ++++++++++++---------- .../carbonado/repo/sleepycat/BDBRepository.java | 20 ++++---- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/amazon/carbonado/layout/Layout.java b/src/main/java/com/amazon/carbonado/layout/Layout.java index 62e29f2..1c6b63d 100644 --- a/src/main/java/com/amazon/carbonado/layout/Layout.java +++ b/src/main/java/com/amazon/carbonado/layout/Layout.java @@ -231,13 +231,17 @@ public class Layout { .orderBy("ordinal") .fetch(); - List list = new ArrayList(); + try { + List list = new ArrayList(); - while (cursor.hasNext()) { - list.add(new LayoutProperty(cursor.next())); - } + while (cursor.hasNext()) { + list.add(new LayoutProperty(cursor.next())); + } - mAllProperties = Collections.unmodifiableList(list); + mAllProperties = Collections.unmodifiableList(list); + } finally { + cursor.close(); + } } return mAllProperties; diff --git a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepository.java b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepository.java index b9380fc..8516628 100644 --- a/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/indexed/IndexedRepository.java @@ -159,9 +159,13 @@ class IndexedRepository implements Repository, mRepository.storageFor(StoredIndexInfo.class) .query().fetch(); - while (cursor.hasNext()) { - StoredIndexInfo info = cursor.next(); - names.remove(info.getIndexName()); + try { + while (cursor.hasNext()) { + StoredIndexInfo info = cursor.next(); + names.remove(info.getIndexName()); + } + } finally { + cursor.close(); } return names.toArray(new String[names.size()]); diff --git a/src/main/java/com/amazon/carbonado/repo/indexed/ManagedIndex.java b/src/main/java/com/amazon/carbonado/repo/indexed/ManagedIndex.java index bb17067..ebdea0b 100644 --- a/src/main/java/com/amazon/carbonado/repo/indexed/ManagedIndex.java +++ b/src/main/java/com/amazon/carbonado/repo/indexed/ManagedIndex.java @@ -302,37 +302,40 @@ class ManagedIndex implements IndexEntryAccessor { Transaction txn = repo.enterTopTransaction(IsolationLevel.NONE); try { Cursor cursor = masterStorage.query().fetch(); - if (!cursor.hasNext()) { - // Nothing exists in master, so nothing to populate. - cursor.close(); - return; - } + try { + if (!cursor.hasNext()) { + // Nothing exists in master, so nothing to populate. + return; + } - Log log = LogFactory.getLog(IndexedStorage.class); - if (log.isInfoEnabled()) { - StringBuilder b = new StringBuilder(); - b.append("Populating index on "); - b.append(masterStorage.getStorableType().getName()); - b.append(": "); - try { - mIndex.appendTo(b); - } catch (java.io.IOException e) { - // Not gonna happen. + Log log = LogFactory.getLog(IndexedStorage.class); + if (log.isInfoEnabled()) { + StringBuilder b = new StringBuilder(); + b.append("Populating index on "); + b.append(masterStorage.getStorableType().getName()); + b.append(": "); + try { + mIndex.appendTo(b); + } catch (java.io.IOException e) { + // Not gonna happen. + } + log.info(b.toString()); } - log.info(b.toString()); - } - // Preload and sort all index entries for improved performance. + // Preload and sort all index entries for improved performance. - buffer = new MergeSortBuffer(mIndexEntryStorage); - c = mGenerator.getComparator(); - buffer.prepare(c); + buffer = new MergeSortBuffer(mIndexEntryStorage); + c = mGenerator.getComparator(); + buffer.prepare(c); - while (cursor.hasNext()) { - buffer.add(makeIndexEntry(cursor.next())); - } + while (cursor.hasNext()) { + buffer.add(makeIndexEntry(cursor.next())); + } - // No need to commit transaction because no changes should have been made. + // No need to commit transaction because no changes should have been made. + } finally { + cursor.close(); + } } finally { txn.exit(); } diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java index 7792536..6fc7cdd 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBRepository.java @@ -245,16 +245,20 @@ abstract class BDBRepository metaRepo.storageFor(StoredDatabaseInfo.class) .query().orderBy("databaseName").fetch(); - ArrayList names = new ArrayList(); - while (cursor.hasNext()) { - StoredDatabaseInfo info = cursor.next(); - // Ordinary user types support evolution. - if (info.getEvolutionStrategy() != StoredDatabaseInfo.EVOLUTION_NONE) { - names.add(info.getDatabaseName()); + try { + ArrayList names = new ArrayList(); + while (cursor.hasNext()) { + StoredDatabaseInfo info = cursor.next(); + // Ordinary user types support evolution. + if (info.getEvolutionStrategy() != StoredDatabaseInfo.EVOLUTION_NONE) { + names.add(info.getDatabaseName()); + } } - } - return names.toArray(new String[names.size()]); + return names.toArray(new String[names.size()]); + } finally { + cursor.close(); + } } public boolean isSupported(Class type) { -- cgit v1.2.3