Skip to content

Commit

Permalink
Handle deadlock when too many connections are open (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Aug 9, 2021
1 parent f6c8330 commit 75b2468
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>database</artifactId>
<version>1.9</version>
<version>117.va2009e38b882</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@
import java.util.Map;
import java.util.Objects;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.database.Database;
import org.jenkinsci.plugins.database.GlobalDatabaseConfiguration;
import org.jenkinsci.remoting.SerializableOnlyOverRemoting;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;


@Extension
public class DatabaseTestResultStorage extends JunitTestResultStorage {
Expand Down Expand Up @@ -112,7 +109,7 @@ private static class RemotePublisherImpl implements RemotePublisher {

@Override public void publish(TestResult result, TaskListener listener) throws IOException {
try {
try (PreparedStatement statement = connectionSupplier.connection().prepareStatement("INSERT INTO caseResults (job, build, suite, package, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
try (Connection connection = connectionSupplier.connection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO caseResults (job, build, suite, package, className, testName, errorDetails, skipped, duration, stdout, stderr, stacktrace) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
int count = 0;
for (SuiteResult suiteResult : result.getSuites()) {
for (CaseResult caseResult : suiteResult.getCases()) {
Expand Down Expand Up @@ -162,7 +159,7 @@ private static class RemotePublisherImpl implements RemotePublisher {

}

static abstract class ConnectionSupplier { // TODO AutoCloseable
static abstract class ConnectionSupplier implements AutoCloseable {

private transient Connection connection;

Expand All @@ -171,14 +168,26 @@ static abstract class ConnectionSupplier { // TODO AutoCloseable
protected void initialize(Connection connection) throws SQLException {}

synchronized Connection connection() throws SQLException {
if (connection == null) {
if (connection == null || connection.isClosed()) {
Connection _connection = database().getDataSource().getConnection();
initialize(_connection);
connection = _connection;
}
return connection;
}

@Override
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
connection = null;
}

}

static class LocalConnectionSupplier extends ConnectionSupplier {
Expand Down Expand Up @@ -221,8 +230,7 @@ public TestResultStorage(String job, int build) {
}

private <T> T query(Querier<T> querier) {
try {
Connection connection = getConnectionSupplier().connection();
try (Connection connection = getConnectionSupplier().connection()) {
return querier.run(connection);
} catch (SQLException x) {
throw new RuntimeException(x);
Expand Down

0 comments on commit 75b2468

Please sign in to comment.