Skip to content

Commit d77dbb6

Browse files
authored
Merge pull request #1668 from WildMeOrg/fix/mediaasset-safeurl-connection-leak
Close MediaAsset background/URL Shepherds in finally to stop connection leaks
2 parents ae8b67c + 0b218b4 commit d77dbb6

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/main/java/org/ecocean/media/MediaAsset.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,14 @@ public URL safeURL(HttpServletRequest request) {
733733
Shepherd myShepherd = new Shepherd(context);
734734
myShepherd.setAction("MediaAsset.safeURL");
735735
myShepherd.beginDBTransaction();
736-
URL u = safeURL(myShepherd, request);
737-
myShepherd.rollbackDBTransaction();
738-
myShepherd.closeDBTransaction();
739-
return u;
736+
// try/finally so a throw from safeURL(myShepherd, request) -- e.g. a JDO lazy-load/query
737+
// failure inside bestSafeAsset() -- cannot skip the close and leak the PersistenceManager.
738+
// Those leaks are the "MediaAsset.safeURL:begin" Shepherds stuck on dbconnections.jsp.
739+
try {
740+
return safeURL(myShepherd, request);
741+
} finally {
742+
myShepherd.rollbackAndClose();
743+
}
740744
}
741745

742746
public URL safeURL() {
@@ -1191,6 +1195,10 @@ public void run() {
11911195
Shepherd myShepherd = new Shepherd(context);
11921196
myShepherd.setAction("updateStandardChildrenBackground:" + tid);
11931197
myShepherd.beginDBTransaction();
1198+
// try/finally so a throw from load/updateStandardChildren cannot skip the close and
1199+
// leak this background Shepherd. commit stays inside try; the finally rollback is a
1200+
// harmless no-op once the commit has succeeded.
1201+
try {
11941202
int ct = 0;
11951203
for (Integer id : ids) {
11961204
ct++;
@@ -1202,7 +1210,9 @@ public void run() {
12021210
"] completed " + kids.size() + " children for id=" + id);
12031211
}
12041212
myShepherd.commitDBTransaction();
1205-
myShepherd.closeDBTransaction();
1213+
} finally {
1214+
myShepherd.rollbackAndClose();
1215+
}
12061216
}
12071217
};
12081218
new Thread(rn).start();

src/main/java/org/ecocean/media/YouTubeAssetStore.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ public static void backgroundGrabAndParse(final MediaAsset otherMa,
367367
public void run() {
368368
myShepherd.setAction("YouTubeAssetStore.backgroundGrabAndParse");
369369
myShepherd.beginDBTransaction();
370+
// try/finally so a throw from load/save/metadata cannot skip the close and leak this
371+
// background Shepherd. commit stays inside try; the finally rollback is a harmless
372+
// no-op once the commit has succeeded.
373+
try {
370374
MediaAsset ma = MediaAssetFactory.load(otherMa.getIdInt(), myShepherd);
371375
System.out.println("about to grab!");
372376
boolean ok = false;
@@ -379,7 +383,9 @@ public void run() {
379383
null) ? "(null metadata)" : ma.getMetadata().getDataAsString()));
380384
MediaAssetFactory.save(ma, myShepherd);
381385
myShepherd.commitDBTransaction();
382-
myShepherd.closeDBTransaction();
386+
} finally {
387+
myShepherd.rollbackAndClose();
388+
}
383389
}
384390
};
385391
new Thread(rn).start();

0 commit comments

Comments
 (0)