Skip to content

Commit

Permalink
Expose connection pool size to generic database (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnswithers authored May 24, 2021
1 parent dddad4e commit 4cb95ae
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import jenkins.model.Jenkins;
import org.apache.tools.ant.AntClassLoader;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import org.kohsuke.stapler.verb.POST;

Expand All @@ -25,6 +29,11 @@ public class GenericDatabase extends Database {
public final Secret password;
public final String url;

private Integer initialSize = DescriptorImpl.defaultInitialSize;
private Integer maxTotal = DescriptorImpl.defaultMaxTotal;
private Integer maxIdle = DescriptorImpl.defaultMaxIdle;
private Integer minIdle = DescriptorImpl.defaultMinIdle;

private transient DataSource source;

@DataBoundConstructor
Expand All @@ -35,6 +44,46 @@ public GenericDatabase(String url, String driver, String username, Secret passwo
this.password = password;
}

@Nonnull
public Integer getInitialSize() {
return initialSize;
}

@DataBoundSetter
public void setInitialSize(final Integer initialSize) {
this.initialSize = initialSize == null ? DescriptorImpl.defaultInitialSize : initialSize;
}

@Nonnull
public Integer getMaxTotal() {
return maxTotal;
}

@DataBoundSetter
public void setMaxTotal(final Integer maxTotal) {
this.maxTotal = maxTotal == null ? DescriptorImpl.defaultMaxTotal : maxTotal;
}

@Nonnull
public Integer getMaxIdle() {
return maxIdle;
}

@DataBoundSetter
public void setMaxIdle(final Integer maxIdle) {
this.maxIdle = maxIdle == null ? DescriptorImpl.defaultMaxIdle : maxIdle;
}

@Nonnull
public Integer getMinIdle() {
return minIdle;
}

@DataBoundSetter
public void setMinIdle(final Integer minIdle) {
this.minIdle = minIdle == null ? DescriptorImpl.defaultMinIdle : minIdle;
}

@Override
public synchronized DataSource getDataSource() throws SQLException {
if (source==null) {
Expand All @@ -44,6 +93,10 @@ public synchronized DataSource getDataSource() throws SQLException {
source.setUrl(url);
source.setUsername(username);
source.setPassword(Secret.toString(password));
source.setInitialSize(initialSize);
source.setMaxTotal(maxTotal);
source.setMaxIdle(maxIdle);
source.setMinIdle(minIdle);
this.source = source.createDataSource();
}
return source;
Expand All @@ -58,6 +111,11 @@ public DescriptorImpl getDescriptor() {
public static class DescriptorImpl extends DatabaseDescriptor {
private transient AntClassLoader loader;

public static final Integer defaultInitialSize = 0;
public static final Integer defaultMaxTotal = 8;
public static final Integer defaultMaxIdle = 8;
public static final Integer defaultMinIdle = 0;

@Override
public String getDisplayName() {
return "Generic";
Expand Down Expand Up @@ -86,7 +144,7 @@ private synchronized ClassLoader getClassLoader() {
@POST
public FormValidation doCheckDriver(@QueryParameter String value) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);

if (value.length()==0)
return FormValidation.ok(); // no value typed yet.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ f.entry(field:"username",title:_("User Name")) {
f.entry(field:"password",title:_("Password")) {
f.password()
}
f.advanced {
f.entry(field: "initialSize", title: _("Initial Size")) {
f.number(clazz: "number", min: 0, max: 65535, step: 1, default: "${descriptor.defaultInitialSize}")
}
f.entry(field: "maxTotal", title: _("Max Total")) {
f.number(clazz: "number", min: -1, max: 65535, step: 1, default: "${descriptor.defaultMaxTotal}")
}
f.entry(field: "maxIdle", title: _("Max Idle")) {
f.number(clazz: "number", min: -1, max: 65535, step: 1, default: "${descriptor.defaultMaxIdle}")
}
f.entry(field: "minIdle", title: _("Min Idle")) {
f.number(clazz: "number", min: 0, max: 65535, step: 1, default: "${descriptor.defaultMinIdle}")
}
}
f.block() {
f.validateButton(method:"validate",title:_("Test Connection"),with:"driver,url,username,password")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The initial number of connections that are created when the pool is started.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The minimum number of connections that can remain idle in the pool, without extra ones being created, or zero to create none.
</div>

0 comments on commit 4cb95ae

Please sign in to comment.