Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

GEOT-3315 Memory leak in ContentEntry.state

  • Loading branch information...
commit f4cdb060c5b78915cea8eadaef6317bac21e8a1c 1 parent 9d7f87d
@mbarto mbarto authored
View
10 modules/library/data/src/main/java/org/geotools/data/store/ContentEntry.java
@@ -174,6 +174,16 @@ public void dispose() {
}
}
+ /**
+ * Removes a closed transaction from the state cache.
+ * @param transaction
+ */
+ public void clearTransaction(Transaction transaction) {
+ if (state.containsKey(transaction)) {
+ state.remove(transaction);
+ }
+ }
+
public String toString() {
return getTypeName();
}
View
4 modules/library/data/src/main/java/org/geotools/data/store/DiffTransactionState.java
@@ -71,6 +71,10 @@ public Diff getDiff() {
* the transaction is correct.
*/
public synchronized void setTransaction(Transaction transaction) {
+ if (this.transaction != null && transaction == null) {
+ // clear ContentEntry transaction to fix GEOT-3315
+ state.getEntry().clearTransaction(this.transaction);
+ }
this.transaction = transaction;
}
View
81 modules/library/data/src/test/java/org/geotools/data/store/ContentEntryTest.java
@@ -0,0 +1,81 @@
+/*
+ * GeoTools - The Open Source Java GIS Toolkit
+ * http://geotools.org
+ *
+ * (C) 2011, Open Source Geospatial Foundation (OSGeo)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+package org.geotools.data.store;
+
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.geotools.data.DefaultTransaction;
+import org.geotools.data.Transaction;
+import org.geotools.feature.NameImpl;
+import org.junit.Test;
+import org.opengis.feature.type.Name;
+
+/**
+ * Test the behaviour of {@link ContentEntry}.
+ * @author Mauro Bartolomeoli (mauro.bartolomeoli@geo-solutions.it)
+ */
+public class ContentEntryTest {
+
+ /**
+ * Test that the a Transaction is removed from state when the Transaction is
+ * closed.
+ */
+ @Test
+ public void transactionCacheClearedOnTransactionClose() {
+ ContentDataStore dataStore = new ContentDataStore() {
+
+ @Override
+ protected List<Name> createTypeNames() throws IOException {
+ return null;
+ }
+
+ @Override
+ protected ContentFeatureSource createFeatureSource(ContentEntry entry)
+ throws IOException {
+ return null;
+ }
+ };
+
+ Transaction transaction = new DefaultTransaction();
+
+ ContentEntry entry = new ContentEntry(dataStore, new NameImpl("test"));
+ ContentState state = entry.getState(transaction);
+ new DiffTransactionState(state);
+
+ // state is extracted from state cache
+ assertSame(state, entry.getState(transaction));
+ // and contains our transaction
+ assertSame(state.getTransaction(), transaction);
+
+ try {
+ transaction.close();
+
+ // after transaction closing, the old state has been cleared, so
+ // a new one is built and returned
+ ContentState stateForClosedTransaction = entry.getState(transaction);
+ assertNotSame(stateForClosedTransaction, state);
+ } catch (IOException e) {
+ fail("Cannot close transaction: " + e.getLocalizedMessage());
+ }
+ }
+
+}
View
3  modules/library/main/src/main/java/org/geotools/data/DefaultTransaction.java
@@ -88,6 +88,9 @@ public DefaultTransaction(String handle) {
* org.geotools.data.Transaction.State)
*/
public void putState(Object key, State state) {
+ if (stateLookup == null) {
+ return;
+ }
if (stateLookup.containsKey(key)) {
State current = (State) stateLookup.get(key);

0 comments on commit f4cdb06

Please sign in to comment.
Something went wrong with that request. Please try again.