Skip to content

Commit

Permalink
GEOT-3315 Memory leak in ContentEntry.state
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarto committed Apr 9, 2013
1 parent 9d7f87d commit f4cdb06
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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;
}

Expand Down
@@ -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());
}
}

}
Expand Up @@ -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);

Expand Down

0 comments on commit f4cdb06

Please sign in to comment.