Fix DB connection pool verification
So far, given no validation query was specified, a broken connection could be returned from the connection pool and this caused a 500 error when trying, for example, to read a change from the database. Specify a default validation query that works for several of the DBMS supported by Gerrit (H2, PostgreSQL, MySQL), and specific validation queries for other supported databases (Apache Derby, DB2, Oracle). Validation queries were verified for open source databases (H2, MySQL, PostgreSQL and Derby). Change-Id: Ia215288d70b11893b7d1403611e109a5a951f090
This commit is contained in:
@@ -21,6 +21,7 @@ import java.io.InputStream;
|
||||
|
||||
public abstract class BaseDataSourceType implements DataSourceType {
|
||||
|
||||
private static final String DEFAULT_VALIDATION_QUERY = "select 1";
|
||||
private final String driver;
|
||||
|
||||
protected BaseDataSourceType(String driver) {
|
||||
@@ -37,6 +38,11 @@ public abstract class BaseDataSourceType implements DataSourceType {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return DEFAULT_VALIDATION_QUERY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptRunner getIndexScript() throws IOException {
|
||||
return getScriptRunner("index_generic.sql");
|
||||
|
||||
@@ -43,4 +43,9 @@ public class DB2 extends BaseDataSourceType {
|
||||
b.append(dbc.required("database"));
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return "SELECT 1 FROM SYSIBM.SYSDUMMY1";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +126,9 @@ public class DataSourceProvider implements Provider<DataSource>,
|
||||
ds.setMaxWait(ConfigUtil.getTimeUnit(cfg, "database", null,
|
||||
"poolmaxwait", MILLISECONDS.convert(30, SECONDS), MILLISECONDS));
|
||||
ds.setInitialSize(ds.getMinIdle());
|
||||
ds.setValidationQuery(dst.getValidationQuery());
|
||||
ds.setValidationQueryTimeout(5);
|
||||
|
||||
return intercept(interceptor, ds);
|
||||
|
||||
} else {
|
||||
|
||||
@@ -24,6 +24,8 @@ public interface DataSourceType {
|
||||
|
||||
public String getUrl();
|
||||
|
||||
public String getValidationQuery();
|
||||
|
||||
public boolean usePool();
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,4 +41,9 @@ class Derby extends BaseDataSourceType {
|
||||
}
|
||||
return "jdbc:derby:" + site.resolve(database).toString() + ";create=true";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return "values 1";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,9 @@ public class Oracle extends BaseDataSourceType {
|
||||
b.append(dbc.required("instance"));
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return "select 1 from dual";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user