I create geotools
' SimpleFeatureCollection
from a shape file on HDD once. I then call its .features()
method a ton of times. So far I believed that this was good practice, but it appears not to be. After calling the feature method one too many times, I receive
Exception in thread "main" java.lang.Error: Maximum lock count exceeded
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.fullTryAcquireShared(ReentrantReadWriteLock.java:528)
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryAcquireShared(ReentrantReadWriteLock.java:488)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireShared(AbstractQueuedSynchronizer.java:1282)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lock(ReentrantReadWriteLock.java:727)
at org.geotools.data.shapefile.files.ShpFiles.acquireRead(ShpFiles.java:358)
at org.geotools.data.shapefile.files.ShpFiles.getReadChannel(ShpFiles.java:789)
at org.geotools.data.shapefile.shp.ShapefileReader.<init>(ShapefileReader.java:253)
at org.geotools.data.shapefile.ShapefileSetManager.openShapeReader(ShapefileSetManager.java:51)
at org.geotools.data.shapefile.ShapefileFeatureSource.getReaderInternal(ShapefileFeatureSource.java:263)
at org.geotools.data.shapefile.ShapefileFeatureStore.getReaderInternal(ShapefileFeatureStore.java:124)
at org.geotools.data.store.ContentFeatureSource.getReader(ContentFeatureSource.java:563)
at org.geotools.data.store.ContentFeatureCollection.features(ContentFeatureCollection.java:165)
How can I avoid this from happening? What is good coding practice here? Should I use the shape file to create the SimpleFeatureCollection
every time before calling its .features()
method? Any insight would be greatly appreciated.
As the javadocs make clear you must close a FeatureIterator
after use or resources will become exhausted or leaked. You need to use code like this:
FeatureIterator i = featureCollection.features()
try {
while( i.hasNext() ){
SimpleFeature feature = i.next();
}
}
finally {
i.close();
}