-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LDEV-5102 - Handle ResultSet.close() in a separate thread to prevent …
…blocking in QueryLazy with MySQL
- Loading branch information
1 parent
e5fba89
commit 2c53a97
Showing
6 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
core/src/main/java/lucee/runtime/util/threading/Closer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package lucee.runtime.util.threading; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
||
import lucee.commons.io.log.Log; | ||
|
||
public class Closer { | ||
private CloserThread thread; | ||
private Log log; | ||
private final BlockingQueue<CloserJob> jobs = new LinkedBlockingQueue<>(); | ||
|
||
public Closer(Log log) { | ||
this.log = log; | ||
} | ||
|
||
public void touch() { | ||
if (thread == null || !thread.isAlive()) { | ||
synchronized (this) { | ||
if (thread == null || !thread.isAlive()) { | ||
thread = new CloserThread(jobs, log); | ||
thread.start(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void add(CloserJob job) { | ||
jobs.add(job); | ||
touch(); | ||
} | ||
|
||
private static class CloserThread extends Thread { | ||
private static final long IDLE_TIMEOUT = 10000; | ||
private static final long INTERVALL = 1000; | ||
private long lastMod; | ||
private Log log; | ||
private BlockingQueue<CloserJob> jobs; | ||
|
||
public CloserThread(BlockingQueue<CloserJob> jobs, Log log) { | ||
this.jobs = jobs; | ||
this.log = log; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (true) { | ||
CloserJob job = jobs.poll(); | ||
|
||
if (job != null) { | ||
if (log != null) log.debug("Closer", "executing job: " + job.getLablel()); | ||
|
||
long now = System.currentTimeMillis(); | ||
try { | ||
job.execute(); | ||
} | ||
catch (Exception e) { | ||
if (log != null) log.error("Closer", e); | ||
} | ||
lastMod = now; | ||
} | ||
// nothing to do ATM | ||
else { | ||
long now = System.currentTimeMillis(); | ||
if (lastMod + IDLE_TIMEOUT < now) { | ||
if (log != null) log.debug("Closer", "nothing to do, idle timeout reached, stoping observer "); | ||
break; | ||
} | ||
else if (log != null) log.debug("Closer", "nothing to do, remaining idle for another " + ((lastMod + IDLE_TIMEOUT) - now) + "ms"); | ||
} | ||
if (log != null) log.debug("Closer", "sleep for " + INTERVALL + "ms"); | ||
try { | ||
sleep(INTERVALL); | ||
} | ||
catch (InterruptedException e) { | ||
if (log != null) log.error("Closer", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
core/src/main/java/lucee/runtime/util/threading/CloserJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lucee.runtime.util.threading; | ||
|
||
import lucee.runtime.exp.PageException; | ||
|
||
public interface CloserJob { | ||
|
||
public String getLablel(); | ||
|
||
public void execute() throws PageException; | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
core/src/main/java/lucee/runtime/util/threading/StatmentClose.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package lucee.runtime.util.threading; | ||
|
||
import java.sql.Statement; | ||
|
||
import lucee.commons.db.DBUtil; | ||
import lucee.commons.io.log.Log; | ||
import lucee.runtime.db.DatasourceConnection; | ||
import lucee.runtime.db.DatasourceManagerImpl; | ||
import lucee.runtime.exp.PageException; | ||
|
||
public class StatmentClose implements CloserJob { | ||
|
||
private DatasourceManagerImpl manager; | ||
private DatasourceConnection dc; | ||
private Statement stat; | ||
private Log log; | ||
|
||
public StatmentClose(DatasourceManagerImpl manager, DatasourceConnection dc, Statement stat, Log log) { | ||
this.manager = manager; | ||
this.dc = dc; | ||
this.stat = stat; | ||
this.log = log; | ||
} | ||
|
||
@Override | ||
public String getLablel() { | ||
return "closing datasource connection resultset"; | ||
} | ||
|
||
@Override | ||
public void execute() throws PageException { | ||
// TODO add virtual threads | ||
new Thread(() -> { | ||
try { | ||
DBUtil.closeEL(stat); | ||
manager.releaseConnection(null, dc); | ||
if (log != null) log.debug("StatmentClose", "sucessfully closed resultset"); | ||
} | ||
catch (Exception e) { | ||
if (log != null) log.error("StatmentClose", e); | ||
} | ||
}).start(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters