Add missing close() calls to use of TableRowIterator, also add close() to ItemIterator (to close underlying TableRowIterator)

git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@3038 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2008-08-07 09:21:47 +00:00
parent 6e611d8750
commit 3f3d806ad5
21 changed files with 737 additions and 508 deletions

View File

@@ -259,9 +259,16 @@ public class ItemExport {
// it's a collection, so do a bunch of items
ItemIterator i = mycollection.getItems();
try
{
exportItem(c, i, destDirName, seqStart);
}
finally
{
if (i != null)
i.close();
}
}
c.complete();
}
@@ -623,6 +630,7 @@ public class ItemExport {
for (Collection collection : collections) {
// get all the items in each collection
ItemIterator iitems = collection.getItems();
try {
while (iitems.hasNext()) {
Item item = iitems.next();
// get all the bundles in the item
@@ -637,11 +645,16 @@ public class ItemExport {
}
items.add(item.getID());
}
} finally {
if (iitems != null)
iitems.close();
}
}
} else if (dso.getType() == Constants.COLLECTION) {
Collection collection = (Collection) dso;
// get all the items in the collection
ItemIterator iitems = collection.getItems();
try {
while (iitems.hasNext()) {
Item item = iitems.next();
// get all thebundles in the item
@@ -656,6 +669,10 @@ public class ItemExport {
}
items.add(item.getID());
}
} finally {
if (iitems != null)
iitems.close();
}
} else if (dso.getType() == Constants.ITEM) {
Item item = (Item) dso;
// get all the bundles in the item
@@ -699,12 +716,13 @@ public class ItemExport {
Thread go = new Thread() {
public void run() {
Context context;
ItemIterator iitems = null;
try {
// create a new dspace context
context = new Context();
// ignore auths
context.setIgnoreAuthorization(true);
ItemIterator iitems = new ItemIterator(context, items);
iitems = new ItemIterator(context, items);
String fileName = assembleFileName("item", eperson, new Date());
String workDir = getExportWorkDirectory()
@@ -747,6 +765,9 @@ public class ItemExport {
// wont throw here
}
throw new RuntimeException(e1);
} finally {
if (iitems != null)
iitems.close();
}
}

View File

@@ -420,11 +420,19 @@ public class MediaFilterManager
{
//otherwise, just find every item and process
ItemIterator i = Item.findAll(c);
try
{
while (i.hasNext() && processed < max2Process)
{
applyFiltersItem(c, i.next());
}
}
finally
{
if (i != null)
i.close();
}
}
}
public static void applyFiltersCommunity(Context c, Community community)
@@ -453,11 +461,19 @@ public class MediaFilterManager
if(!inSkipList(collection.getHandle()))
{
ItemIterator i = collection.getItems();
try
{
while (i.hasNext() && processed < max2Process)
{
applyFiltersItem(c, i.next());
}
}
finally
{
if (i != null)
i.close();
}
}
}
public static void applyFiltersItem(Context c, Item item) throws Exception

View File

@@ -178,7 +178,8 @@ public class METSExport
}
ItemIterator items = null;
try
{
if (line.hasOption('c'))
{
String handle = getHandleArg(line.getOptionValue('c'));
@@ -213,6 +214,12 @@ public class METSExport
{
writeAIP(context, items.next(), dest);
}
}
finally
{
if (items != null)
items.close();
}
context.abort();
System.exit(0);

View File

@@ -234,6 +234,8 @@ public class GenerateSitemaps
}
ItemIterator allItems = Item.findAll(c);
try
{
int itemCount = 0;
while (allItems.hasNext())
@@ -268,6 +270,12 @@ public class GenerateSitemaps
+ comms.length + ",collections=" + colls.length
+ ",items=" + itemCount));
}
}
finally
{
if (allItems != null)
allItems.close();
}
c.abort();
}

View File

@@ -186,7 +186,8 @@ public class PolicySet
Group group = Group.find(c, groupID);
ItemIterator i = collection.getItems();
try
{
if (contentType == Constants.ITEM)
{
// build list of all items in a collection
@@ -294,5 +295,11 @@ public class PolicySet
}
}
}
finally
{
if (i != null)
i.close();
}
}
}
}

View File

@@ -258,6 +258,8 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null)
{
try
{
while (tri.hasNext())
{
@@ -282,6 +284,11 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
DatabaseManager.delete(context, tr);
}
}
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++)
@@ -543,6 +550,7 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
}
finally
{
if (tri != null)
tri.close();
}
}
@@ -567,6 +575,8 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID);
if (tri != null)
{
try
{
while (tri.hasNext())
{
@@ -591,6 +601,11 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
DatabaseManager.delete(context, tr);
}
}
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < commID.length; i++)

View File

@@ -255,6 +255,8 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null)
{
try
{
while (tri.hasNext())
{
@@ -279,6 +281,11 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
DatabaseManager.delete(context, tr);
}
}
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++)
@@ -568,6 +575,8 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
// Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID);
if (tri != null)
{
try
{
while (tri.hasNext())
{
@@ -592,6 +601,11 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
DatabaseManager.delete(context, tr);
}
}
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database
for (int i = 0; i < commID.length; i++)

View File

@@ -1092,6 +1092,11 @@ public class Collection extends DSpaceObject
log.error("caught exception: ", e);
throw new IOException(e.getMessage());
}
finally
{
if (items != null)
items.close();
}
// Delete bitstream logo
setLogo(null);

View File

@@ -1558,6 +1558,8 @@ public class Item extends DSpaceObject
// looking for matches
TableRowIterator tri = retrieveMetadata();
if (tri != null)
{
try
{
while (tri.hasNext())
{
@@ -1626,6 +1628,11 @@ public class Item extends DSpaceObject
}
}
}
finally
{
tri.close();
}
}
// Add missing in-memory DC
for (int dcIdx = 0; dcIdx < dublinCore.size(); dcIdx++)

View File

@@ -270,4 +270,13 @@ public class ItemIterator
return null;
}
}
/**
* Dispose of this Iterator, and it's underlying resources
*/
public void close()
{
if (itemRows != null)
itemRows.close();
}
}

View File

@@ -314,7 +314,8 @@ public class EPerson extends DSpaceObject
// Get all the epeople that match the query
TableRowIterator rows = DatabaseManager.query(context, dbquery, paramArr);
try
{
List epeopleRows = rows.toList();
EPerson[] epeople = new EPerson[epeopleRows.size()];
@@ -338,6 +339,12 @@ public class EPerson extends DSpaceObject
return epeople;
}
finally
{
if (rows != null)
rows.close();
}
}
/**
* Returns the total number of epeople returned by a specific query, without the overhead
@@ -428,6 +435,8 @@ public class EPerson extends DSpaceObject
TableRowIterator rows = DatabaseManager.query(context,
"SELECT * FROM eperson ORDER BY "+s);
try
{
List epeopleRows = rows.toList();
EPerson[] epeople = new EPerson[epeopleRows.size()];
@@ -452,6 +461,12 @@ public class EPerson extends DSpaceObject
return epeople;
}
finally
{
if (rows != null)
rows.close();
}
}
/**
* Create a new eperson

View File

@@ -776,6 +776,8 @@ public class Group extends DSpaceObject
context, "epersongroup",
"SELECT * FROM epersongroup ORDER BY "+s);
try
{
List gRows = rows.toList();
Group[] groups = new Group[gRows.size()];
@@ -800,6 +802,12 @@ public class Group extends DSpaceObject
return groups;
}
finally
{
if (rows != null)
rows.close();
}
}
/**
@@ -897,6 +905,8 @@ public class Group extends DSpaceObject
TableRowIterator rows =
DatabaseManager.query(context, dbquery, paramArr);
try
{
List groupRows = rows.toList();
Group[] groups = new Group[groupRows.size()];
@@ -919,6 +929,12 @@ public class Group extends DSpaceObject
}
return groups;
}
finally
{
if (rows != null)
rows.close();
}
}
/**
* Returns the total number of groups returned by a specific query, without the overhead

View File

@@ -570,16 +570,23 @@ public class DSIndexer
* @param force
*/
public static void updateIndex(Context context, boolean force) {
try
{
for(ItemIterator i = Item.findAll(context);i.hasNext();)
ItemIterator items = null;
try
{
Item item = (Item) i.next();
for(items = Item.findAll(context);items.hasNext();)
{
Item item = (Item) items.next();
indexContent(context,item,force);
item.decache();
}
}
finally
{
if (items != null)
items.close();
}
Collection[] collections = Collection.findAll(context);
for (int i = 0; i < collections.length; i++)
@@ -603,7 +610,6 @@ public class DSIndexer
{
log.error(e.getMessage(), e);
}
}
/**

View File

@@ -342,6 +342,8 @@ public class Harvest
"AND collection2item.collection_id=handle.resource_id AND collection2item.item_id = ? ",
Constants.COLLECTION, itemInfo.itemID);
try
{
// Chuck 'em in the itemInfo object
itemInfo.collectionHandles = new LinkedList();
@@ -350,8 +352,13 @@ public class Harvest
TableRow r = colRows.next();
itemInfo.collectionHandles.add(r.getStringColumn("handle"));
}
}
finally
{
if (colRows != null)
colRows.close();
}
}
/**

View File

@@ -162,6 +162,8 @@ public class DatabaseManager
}
PreparedStatement statement = context.getDBConnection().prepareStatement(query);
try
{
loadParameters(statement,parameters);
TableRowIterator retTRI = new TableRowIterator(statement.executeQuery(),
@@ -170,6 +172,14 @@ public class DatabaseManager
retTRI.setStatement(statement);
return retTRI;
}
catch (SQLException sqle)
{
if (statement != null)
try { statement.close(); } catch (SQLException s) { }
throw sqle;
}
}
/**
* Return an iterator with the results of the query.
@@ -204,14 +214,23 @@ public class DatabaseManager
}
PreparedStatement statement = context.getDBConnection().prepareStatement(query);
try
{
loadParameters(statement,parameters);
TableRowIterator retTRI = new TableRowIterator(statement.executeQuery());
retTRI.setStatement(statement);
return retTRI;
}
catch (SQLException sqle)
{
if (statement != null)
try { statement.close(); } catch (SQLException s) { }
throw sqle;
}
}
/**
* Return an iterator with the results of executing statement. The table

View File

@@ -197,13 +197,18 @@ public class TableRowIterator
{
List resultsList = new ArrayList();
try
{
while (hasNext())
{
resultsList.add(next());
}
}
finally
{
// Close the connection after converting it to a list.
this.close();
}
return resultsList;
}

View File

@@ -766,16 +766,23 @@ public class MyDSpaceServlet extends DSpaceServlet
throws ServletException, IOException, SQLException,
AuthorizeException
{
// Turn the iterator into a list
List subList = new LinkedList();
ItemIterator subs = Item.findBySubmitter(context, context
.getCurrentUser());
// Turn the iterator into a list
List subList = new LinkedList();
try
{
while (subs.hasNext())
{
subList.add(subs.next());
}
}
finally
{
if (subs != null)
subs.close();
}
Item[] items = new Item[subList.size()];

View File

@@ -126,7 +126,8 @@ public class ItemMapServlet extends DSpaceServlet
// get all items from that collection, add them to a hash
ItemIterator i = myCollection.getItems();
try
{
// iterate through the items in this collection, and count how many
// are native, and how many are imports, and which collections they
// came from
@@ -168,6 +169,12 @@ public class ItemMapServlet extends DSpaceServlet
// store the item
myItems.put(myKey, myItem);
}
}
finally
{
if (i != null)
i.close();
}
// remove this collection's entry because we already have a native
// count
@@ -381,6 +388,8 @@ public class ItemMapServlet extends DSpaceServlet
// FIXME: oh god this is so annoying - what an API /Richard
// we need to deduplicate against existing items in this collection
ItemIterator itr = myCollection.getItems();
try
{
ArrayList idslist = new ArrayList();
while (itr.hasNext())
{
@@ -399,7 +408,12 @@ public class ItemMapServlet extends DSpaceServlet
}
}
}
}
finally
{
if (itr != null)
itr.close();
}
}
catch (BrowseException e)
{
@@ -424,9 +438,10 @@ public class ItemMapServlet extends DSpaceServlet
// now find all imported items from that collection
// seemingly inefficient, but database should have this query cached
ItemIterator i = myCollection.getItems();
Map items = new HashMap();
ItemIterator i = myCollection.getItems();
try
{
while (i.hasNext())
{
Item myItem = i.next();
@@ -437,6 +452,12 @@ public class ItemMapServlet extends DSpaceServlet
items.put(myKey, myItem);
}
}
}
finally
{
if (i != null)
i.close();
}
request.setAttribute("collection", myCollection);
request.setAttribute("browsetext", targetCollection

View File

@@ -160,12 +160,21 @@ class DAVCollection extends DAVDSpaceObject
{
Vector result = new Vector();
ItemIterator ii = this.collection.getItems();
try
{
while (ii.hasNext())
{
Item item = ii.next();
result.add(new DAVItem(this.context, this.request, this.response,
makeChildPath(item), item));
}
}
finally
{
if (ii != null)
ii.close();
}
return (DAVResource[]) result.toArray(new DAVResource[result.size()]);
}

View File

@@ -195,7 +195,8 @@ public class BrowseItemForm extends AbstractDSpaceTransformer {
// get all items from that collection
ItemIterator iterator = collection.getItems();
try
{
while (iterator.hasNext())
{
Item item = iterator.next();
@@ -203,6 +204,12 @@ public class BrowseItemForm extends AbstractDSpaceTransformer {
if (! item.isOwningCollection(collection))
items.add(item);
}
}
finally
{
if (iterator != null)
iterator.close();
}
return items;
}

View File

@@ -157,6 +157,8 @@ public class MapperMain extends AbstractDSpaceTransformer {
// get all items from that collection
ItemIterator iterator = collection.getItems();
try
{
// iterate through the items in this collection, and count how many
// are native, and how many are imports, and which collections they
// came from
@@ -169,6 +171,12 @@ public class MapperMain extends AbstractDSpaceTransformer {
else
count_import++;
}
}
finally
{
if (iterator != null)
iterator.close();
}
int[] counts = new int[2];
counts[0] = count_native;