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 // it's a collection, so do a bunch of items
ItemIterator i = mycollection.getItems(); ItemIterator i = mycollection.getItems();
try
{
exportItem(c, i, destDirName, seqStart); exportItem(c, i, destDirName, seqStart);
} }
finally
{
if (i != null)
i.close();
}
}
c.complete(); c.complete();
} }
@@ -623,6 +630,7 @@ public class ItemExport {
for (Collection collection : collections) { for (Collection collection : collections) {
// get all the items in each collection // get all the items in each collection
ItemIterator iitems = collection.getItems(); ItemIterator iitems = collection.getItems();
try {
while (iitems.hasNext()) { while (iitems.hasNext()) {
Item item = iitems.next(); Item item = iitems.next();
// get all the bundles in the item // get all the bundles in the item
@@ -637,11 +645,16 @@ public class ItemExport {
} }
items.add(item.getID()); items.add(item.getID());
} }
} finally {
if (iitems != null)
iitems.close();
}
} }
} else if (dso.getType() == Constants.COLLECTION) { } else if (dso.getType() == Constants.COLLECTION) {
Collection collection = (Collection) dso; Collection collection = (Collection) dso;
// get all the items in the collection // get all the items in the collection
ItemIterator iitems = collection.getItems(); ItemIterator iitems = collection.getItems();
try {
while (iitems.hasNext()) { while (iitems.hasNext()) {
Item item = iitems.next(); Item item = iitems.next();
// get all thebundles in the item // get all thebundles in the item
@@ -656,6 +669,10 @@ public class ItemExport {
} }
items.add(item.getID()); items.add(item.getID());
} }
} finally {
if (iitems != null)
iitems.close();
}
} else if (dso.getType() == Constants.ITEM) { } else if (dso.getType() == Constants.ITEM) {
Item item = (Item) dso; Item item = (Item) dso;
// get all the bundles in the item // get all the bundles in the item
@@ -699,12 +716,13 @@ public class ItemExport {
Thread go = new Thread() { Thread go = new Thread() {
public void run() { public void run() {
Context context; Context context;
ItemIterator iitems = null;
try { try {
// create a new dspace context // create a new dspace context
context = new Context(); context = new Context();
// ignore auths // ignore auths
context.setIgnoreAuthorization(true); context.setIgnoreAuthorization(true);
ItemIterator iitems = new ItemIterator(context, items); iitems = new ItemIterator(context, items);
String fileName = assembleFileName("item", eperson, new Date()); String fileName = assembleFileName("item", eperson, new Date());
String workDir = getExportWorkDirectory() String workDir = getExportWorkDirectory()
@@ -747,6 +765,9 @@ public class ItemExport {
// wont throw here // wont throw here
} }
throw new RuntimeException(e1); 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 //otherwise, just find every item and process
ItemIterator i = Item.findAll(c); ItemIterator i = Item.findAll(c);
try
{
while (i.hasNext() && processed < max2Process) while (i.hasNext() && processed < max2Process)
{ {
applyFiltersItem(c, i.next()); applyFiltersItem(c, i.next());
} }
} }
finally
{
if (i != null)
i.close();
}
}
} }
public static void applyFiltersCommunity(Context c, Community community) public static void applyFiltersCommunity(Context c, Community community)
@@ -453,11 +461,19 @@ public class MediaFilterManager
if(!inSkipList(collection.getHandle())) if(!inSkipList(collection.getHandle()))
{ {
ItemIterator i = collection.getItems(); ItemIterator i = collection.getItems();
try
{
while (i.hasNext() && processed < max2Process) while (i.hasNext() && processed < max2Process)
{ {
applyFiltersItem(c, i.next()); applyFiltersItem(c, i.next());
} }
} }
finally
{
if (i != null)
i.close();
}
}
} }
public static void applyFiltersItem(Context c, Item item) throws Exception public static void applyFiltersItem(Context c, Item item) throws Exception

View File

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

View File

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

View File

@@ -186,7 +186,8 @@ public class PolicySet
Group group = Group.find(c, groupID); Group group = Group.find(c, groupID);
ItemIterator i = collection.getItems(); ItemIterator i = collection.getItems();
try
{
if (contentType == Constants.ITEM) if (contentType == Constants.ITEM)
{ {
// build list of all items in a collection // 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 // Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID); TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null) if (tri != null)
{
try
{ {
while (tri.hasNext()) while (tri.hasNext())
{ {
@@ -282,6 +284,11 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
DatabaseManager.delete(context, tr); DatabaseManager.delete(context, tr);
} }
} }
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database // Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++) for (int i = 0; i < distinctIDs.length; i++)
@@ -543,6 +550,7 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
} }
finally finally
{ {
if (tri != null)
tri.close(); tri.close();
} }
} }
@@ -567,6 +575,8 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
// Find all existing mappings for this item // Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID); TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID);
if (tri != null) if (tri != null)
{
try
{ {
while (tri.hasNext()) while (tri.hasNext())
{ {
@@ -591,6 +601,11 @@ public class BrowseCreateDAOOracle implements BrowseCreateDAO
DatabaseManager.delete(context, tr); DatabaseManager.delete(context, tr);
} }
} }
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database // Any remaining mappings need to be added to the database
for (int i = 0; i < commID.length; i++) 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 // Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID); TableRowIterator tri = DatabaseManager.queryTable(context, table, "SELECT * FROM " + table + " WHERE item_id=?", itemID);
if (tri != null) if (tri != null)
{
try
{ {
while (tri.hasNext()) while (tri.hasNext())
{ {
@@ -279,6 +281,11 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
DatabaseManager.delete(context, tr); DatabaseManager.delete(context, tr);
} }
} }
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database // Any remaining mappings need to be added to the database
for (int i = 0; i < distinctIDs.length; i++) for (int i = 0; i < distinctIDs.length; i++)
@@ -568,6 +575,8 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
// Find all existing mappings for this item // Find all existing mappings for this item
TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID); TableRowIterator tri = DatabaseManager.queryTable(context, "Communities2Item", "SELECT * FROM Communities2Item WHERE item_id=?", itemID);
if (tri != null) if (tri != null)
{
try
{ {
while (tri.hasNext()) while (tri.hasNext())
{ {
@@ -592,6 +601,11 @@ public class BrowseCreateDAOPostgres implements BrowseCreateDAO
DatabaseManager.delete(context, tr); DatabaseManager.delete(context, tr);
} }
} }
finally
{
tri.close();
}
}
// Any remaining mappings need to be added to the database // Any remaining mappings need to be added to the database
for (int i = 0; i < commID.length; i++) for (int i = 0; i < commID.length; i++)

View File

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

View File

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

View File

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

View File

@@ -776,6 +776,8 @@ public class Group extends DSpaceObject
context, "epersongroup", context, "epersongroup",
"SELECT * FROM epersongroup ORDER BY "+s); "SELECT * FROM epersongroup ORDER BY "+s);
try
{
List gRows = rows.toList(); List gRows = rows.toList();
Group[] groups = new Group[gRows.size()]; Group[] groups = new Group[gRows.size()];
@@ -800,6 +802,12 @@ public class Group extends DSpaceObject
return groups; return groups;
} }
finally
{
if (rows != null)
rows.close();
}
}
/** /**
@@ -897,6 +905,8 @@ public class Group extends DSpaceObject
TableRowIterator rows = TableRowIterator rows =
DatabaseManager.query(context, dbquery, paramArr); DatabaseManager.query(context, dbquery, paramArr);
try
{
List groupRows = rows.toList(); List groupRows = rows.toList();
Group[] groups = new Group[groupRows.size()]; Group[] groups = new Group[groupRows.size()];
@@ -919,6 +929,12 @@ public class Group extends DSpaceObject
} }
return groups; return groups;
} }
finally
{
if (rows != null)
rows.close();
}
}
/** /**
* Returns the total number of groups returned by a specific query, without the overhead * 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 * @param force
*/ */
public static void updateIndex(Context context, boolean force) { public static void updateIndex(Context context, boolean force) {
try try
{ {
ItemIterator items = null;
for(ItemIterator i = Item.findAll(context);i.hasNext();) try
{ {
Item item = (Item) i.next(); for(items = Item.findAll(context);items.hasNext();)
{
Item item = (Item) items.next();
indexContent(context,item,force); indexContent(context,item,force);
item.decache(); item.decache();
} }
}
finally
{
if (items != null)
items.close();
}
Collection[] collections = Collection.findAll(context); Collection[] collections = Collection.findAll(context);
for (int i = 0; i < collections.length; i++) for (int i = 0; i < collections.length; i++)
@@ -603,7 +610,6 @@ public class DSIndexer
{ {
log.error(e.getMessage(), e); 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 = ? ", "AND collection2item.collection_id=handle.resource_id AND collection2item.item_id = ? ",
Constants.COLLECTION, itemInfo.itemID); Constants.COLLECTION, itemInfo.itemID);
try
{
// Chuck 'em in the itemInfo object // Chuck 'em in the itemInfo object
itemInfo.collectionHandles = new LinkedList(); itemInfo.collectionHandles = new LinkedList();
@@ -350,8 +352,13 @@ public class Harvest
TableRow r = colRows.next(); TableRow r = colRows.next();
itemInfo.collectionHandles.add(r.getStringColumn("handle")); itemInfo.collectionHandles.add(r.getStringColumn("handle"));
} }
}
finally
{
if (colRows != null)
colRows.close(); colRows.close();
} }
}
/** /**

View File

@@ -162,6 +162,8 @@ public class DatabaseManager
} }
PreparedStatement statement = context.getDBConnection().prepareStatement(query); PreparedStatement statement = context.getDBConnection().prepareStatement(query);
try
{
loadParameters(statement,parameters); loadParameters(statement,parameters);
TableRowIterator retTRI = new TableRowIterator(statement.executeQuery(), TableRowIterator retTRI = new TableRowIterator(statement.executeQuery(),
@@ -170,6 +172,14 @@ public class DatabaseManager
retTRI.setStatement(statement); retTRI.setStatement(statement);
return retTRI; 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. * Return an iterator with the results of the query.
@@ -204,14 +214,23 @@ public class DatabaseManager
} }
PreparedStatement statement = context.getDBConnection().prepareStatement(query); PreparedStatement statement = context.getDBConnection().prepareStatement(query);
try
{
loadParameters(statement,parameters); loadParameters(statement,parameters);
TableRowIterator retTRI = new TableRowIterator(statement.executeQuery()); TableRowIterator retTRI = new TableRowIterator(statement.executeQuery());
retTRI.setStatement(statement); retTRI.setStatement(statement);
return retTRI; 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 * Return an iterator with the results of executing statement. The table

View File

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

View File

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

View File

@@ -160,12 +160,21 @@ class DAVCollection extends DAVDSpaceObject
{ {
Vector result = new Vector(); Vector result = new Vector();
ItemIterator ii = this.collection.getItems(); ItemIterator ii = this.collection.getItems();
try
{
while (ii.hasNext()) while (ii.hasNext())
{ {
Item item = ii.next(); Item item = ii.next();
result.add(new DAVItem(this.context, this.request, this.response, result.add(new DAVItem(this.context, this.request, this.response,
makeChildPath(item), item)); makeChildPath(item), item));
} }
}
finally
{
if (ii != null)
ii.close();
}
return (DAVResource[]) result.toArray(new DAVResource[result.size()]); 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 // get all items from that collection
ItemIterator iterator = collection.getItems(); ItemIterator iterator = collection.getItems();
try
{
while (iterator.hasNext()) while (iterator.hasNext())
{ {
Item item = iterator.next(); Item item = iterator.next();
@@ -203,6 +204,12 @@ public class BrowseItemForm extends AbstractDSpaceTransformer {
if (! item.isOwningCollection(collection)) if (! item.isOwningCollection(collection))
items.add(item); items.add(item);
} }
}
finally
{
if (iterator != null)
iterator.close();
}
return items; return items;
} }

View File

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