clean logging in pool, add row count, use lambdas

This commit is contained in:
Jörg Prante 2022-02-11 17:22:36 +01:00
parent 21556e171e
commit 83a12913cf
6 changed files with 156 additions and 230 deletions

View file

@ -152,7 +152,7 @@ public class Pool implements BagStateListener {
this.closeConnectionExecutor = createThreadPoolExecutor(maxPoolSize, poolName + " connection closer", threadFactory, new ThreadPoolExecutor.CallerRunsPolicy()); this.closeConnectionExecutor = createThreadPoolExecutor(maxPoolSize, poolName + " connection closer", threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
this.leakTaskFactory = new ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService); this.leakTaskFactory = new ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService);
this.houseKeeperTask = houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(), 100L, config.getHousekeepingPeriodMs(), TimeUnit.MILLISECONDS); this.houseKeeperTask = houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(), 100L, config.getHousekeepingPeriodMs(), TimeUnit.MILLISECONDS);
if (Boolean.getBoolean("pool.jdbc.blockUntilFilled") && config.getInitializationFailTimeout() > 1) { if (Boolean.getBoolean("org.xbib.jdbc.connection.pool.blockUntilFilled") && config.getInitializationFailTimeout() > 1) {
addConnectionExecutor.setCorePoolSize(Math.min(16, Runtime.getRuntime().availableProcessors())); addConnectionExecutor.setCorePoolSize(Math.min(16, Runtime.getRuntime().availableProcessors()));
addConnectionExecutor.setMaximumPoolSize(Math.min(16, Runtime.getRuntime().availableProcessors())); addConnectionExecutor.setMaximumPoolSize(Math.min(16, Runtime.getRuntime().availableProcessors()));
final long startTime = ClockSource.currentTime(); final long startTime = ClockSource.currentTime();
@ -424,7 +424,6 @@ public class Pool implements BagStateListener {
ds = new DriverDataSource(jdbcUrl, config.getDriverClassName(), config.getProperties(), config.getUsername(), config.getPassword()); ds = new DriverDataSource(jdbcUrl, config.getDriverClassName(), config.getProperties(), config.getUsername(), config.getPassword());
} }
} }
logger.log(Level.INFO, () -> "got data source, setting props = " + config.getProperties());
setTargetFromProperties(ds, config.getProperties()); setTargetFromProperties(ds, config.getProperties());
setLoginTimeout(ds); setLoginTimeout(ds);
this.dataSource = ds; this.dataSource = ds;

View file

@ -4,5 +4,8 @@ package org.xbib.jdbc.query;
* Interface for reading results from a database query. * Interface for reading results from a database query.
*/ */
public interface Rows extends Row { public interface Rows extends Row {
boolean next(); boolean next();
Integer rowCount();
} }

View file

@ -717,6 +717,16 @@ class RowsAdaptor implements Rows {
} }
} }
@Override
public Integer rowCount() {
try {
//rs.last();
return rs.getRow();
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
/** /**
* Make sure the Timestamp will return getTime() accurate to the millisecond * Make sure the Timestamp will return getTime() accurate to the millisecond
* (if possible) and truncate away nanoseconds. * (if possible) and truncate away nanoseconds.

View file

@ -230,17 +230,13 @@ public class SqlSelectImpl implements SqlSelect {
return this; return this;
} }
@Override @Override
public Boolean queryBooleanOrNull() { public Boolean queryBooleanOrNull() {
return queryWithTimeout(new RowsHandler<Boolean>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Boolean process(Rows rs) throws Exception { return rs.getBooleanOrNull();
if (rs.next()) {
return rs.getBooleanOrNull();
}
return null;
} }
return null;
}); });
} }
@ -259,337 +255,264 @@ public class SqlSelectImpl implements SqlSelect {
@Override @Override
public Long queryLongOrNull() { public Long queryLongOrNull() {
return queryWithTimeout(new RowsHandler<Long>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Long process(Rows rs) throws Exception { return rs.getLongOrNull(1);
if (rs.next()) {
return rs.getLongOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public long queryLongOrZero() { public long queryLongOrZero() {
return queryWithTimeout(new RowsHandler<Long>() { Long l = queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Long process(Rows rs) throws Exception { return rs.getLongOrZero(1);
if (rs.next()) {
return rs.getLongOrZero(1);
}
return 0L;
} }
return 0L;
}); });
return l != null ? l : 0L;
} }
@Override @Override
public List<Long> queryLongs() { public List<Long> queryLongs() {
return queryWithTimeout(new RowsHandler<List<Long>>() { return queryWithTimeout(rs -> {
@Override List<Long> result = new ArrayList<>();
public List<Long> process(Rows rs) throws Exception { while (rs.next()) {
List<Long> result = new ArrayList<>(); Long value = rs.getLongOrNull(1);
while (rs.next()) { if (value != null) {
Long value = rs.getLongOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public Integer queryIntegerOrNull() { public Integer queryIntegerOrNull() {
return queryWithTimeout(new RowsHandler<Integer>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Integer process(Rows rs) throws Exception { return rs.getIntegerOrNull(1);
if (rs.next()) {
return rs.getIntegerOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public int queryIntegerOrZero() { public int queryIntegerOrZero() {
return queryWithTimeout(new RowsHandler<Integer>() { Integer i = queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Integer process(Rows rs) throws Exception { return rs.getIntegerOrZero(1);
if (rs.next()) {
return rs.getIntegerOrZero(1);
}
return 0;
} }
return 0;
}); });
return i != null ? i : 0;
} }
@Override @Override
public List<Integer> queryIntegers() { public List<Integer> queryIntegers() {
return queryWithTimeout(new RowsHandler<List<Integer>>() { return queryWithTimeout(rs -> {
@Override List<Integer> result = new ArrayList<>();
public List<Integer> process(Rows rs) throws Exception { while (rs.next()) {
List<Integer> result = new ArrayList<>(); Integer value = rs.getIntegerOrNull(1);
while (rs.next()) { if (value != null) {
Integer value = rs.getIntegerOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public Float queryFloatOrNull() { public Float queryFloatOrNull() {
return queryWithTimeout(new RowsHandler<Float>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Float process(Rows rs) throws Exception { return rs.getFloatOrNull(1);
if (rs.next()) {
return rs.getFloatOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public float queryFloatOrZero() { public float queryFloatOrZero() {
return queryWithTimeout(new RowsHandler<Float>() { Float f = queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Float process(Rows rs) throws Exception { return rs.getFloatOrZero(1);
if (rs.next()) {
return rs.getFloatOrZero(1);
}
return 0f;
} }
return 0f;
}); });
return f!= null ? f : 0f;
} }
@Override @Override
public List<Float> queryFloats() { public List<Float> queryFloats() {
return queryWithTimeout(new RowsHandler<List<Float>>() { return queryWithTimeout(rs -> {
@Override List<Float> result = new ArrayList<>();
public List<Float> process(Rows rs) throws Exception { while (rs.next()) {
List<Float> result = new ArrayList<>(); Float value = rs.getFloatOrNull(1);
while (rs.next()) { if (value != null) {
Float value = rs.getFloatOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public Double queryDoubleOrNull() { public Double queryDoubleOrNull() {
return queryWithTimeout(new RowsHandler<Double>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Double process(Rows rs) throws Exception { return rs.getDoubleOrNull(1);
if (rs.next()) {
return rs.getDoubleOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public double queryDoubleOrZero() { public double queryDoubleOrZero() {
return queryWithTimeout(new RowsHandler<Double>() { Double d = queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Double process(Rows rs) throws Exception { return rs.getDoubleOrZero(1);
if (rs.next()) {
return rs.getDoubleOrZero(1);
}
return 0d;
} }
return 0d;
}); });
return d != null ? d : 0d;
} }
@Override @Override
public List<Double> queryDoubles() { public List<Double> queryDoubles() {
return queryWithTimeout(new RowsHandler<List<Double>>() { return queryWithTimeout(rs -> {
@Override List<Double> result = new ArrayList<>();
public List<Double> process(Rows rs) throws Exception { while (rs.next()) {
List<Double> result = new ArrayList<>(); Double value = rs.getDoubleOrNull(1);
while (rs.next()) { if (value != null) {
Double value = rs.getDoubleOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public BigDecimal queryBigDecimalOrNull() { public BigDecimal queryBigDecimalOrNull() {
return queryWithTimeout(new RowsHandler<BigDecimal>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public BigDecimal process(Rows rs) throws Exception { return rs.getBigDecimalOrNull(1);
if (rs.next()) {
return rs.getBigDecimalOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public BigDecimal queryBigDecimalOrZero() { public BigDecimal queryBigDecimalOrZero() {
return queryWithTimeout(new RowsHandler<BigDecimal>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public BigDecimal process(Rows rs) throws Exception { return rs.getBigDecimalOrZero(1);
if (rs.next()) {
return rs.getBigDecimalOrZero(1);
}
return new BigDecimal(0);
} }
return new BigDecimal(0);
}); });
} }
@Override @Override
public List<BigDecimal> queryBigDecimals() { public List<BigDecimal> queryBigDecimals() {
return queryWithTimeout(new RowsHandler<List<BigDecimal>>() { return queryWithTimeout(rs -> {
@Override List<BigDecimal> result = new ArrayList<>();
public List<BigDecimal> process(Rows rs) throws Exception { while (rs.next()) {
List<BigDecimal> result = new ArrayList<>(); BigDecimal value = rs.getBigDecimalOrNull(1);
while (rs.next()) { if (value != null) {
BigDecimal value = rs.getBigDecimalOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public String queryStringOrNull() { public String queryStringOrNull() {
return queryWithTimeout(new RowsHandler<String>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public String process(Rows rs) throws Exception { return rs.getStringOrNull(1);
if (rs.next()) {
return rs.getStringOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public String queryStringOrEmpty() { public String queryStringOrEmpty() {
return queryWithTimeout(new RowsHandler<String>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public String process(Rows rs) throws Exception { return rs.getStringOrEmpty(1);
if (rs.next()) {
return rs.getStringOrEmpty(1);
}
return "";
} }
return "";
}); });
} }
@Override @Override
public List<String> queryStrings() { public List<String> queryStrings() {
return queryWithTimeout(new RowsHandler<List<String>>() { return queryWithTimeout(rs -> {
@Override List<String> result = new ArrayList<>();
public List<String> process(Rows rs) throws Exception { while (rs.next()) {
List<String> result = new ArrayList<>(); String value = rs.getStringOrNull(1);
while (rs.next()) { if (value != null) {
String value = rs.getStringOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public Date queryDateOrNull() { public Date queryDateOrNull() {
return queryWithTimeout(new RowsHandler<Date>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public Date process(Rows rs) throws Exception { return rs.getDateOrNull(1);
if (rs.next()) {
return rs.getDateOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public List<Date> queryDates() { public List<Date> queryDates() {
return queryWithTimeout(new RowsHandler<List<Date>>() { return queryWithTimeout(rs -> {
@Override List<Date> result = new ArrayList<>();
public List<Date> process(Rows rs) throws Exception { while (rs.next()) {
List<Date> result = new ArrayList<>(); Date value = rs.getDateOrNull(1);
while (rs.next()) { if (value != null) {
Date value = rs.getDateOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@Override @Override
public LocalDate queryLocalDateOrNull() { public LocalDate queryLocalDateOrNull() {
// Date without time // Date without time
return queryWithTimeout(new RowsHandler<LocalDate>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public LocalDate process(Rows rs) throws Exception { return rs.getLocalDateOrNull(1);
if (rs.next()) {
return rs.getLocalDateOrNull(1);
}
return null;
} }
return null;
}); });
} }
@Override @Override
public List<LocalDate> queryLocalDates() { public List<LocalDate> queryLocalDates() {
// Date without time // Date without time
return queryWithTimeout(new RowsHandler<List<LocalDate>>() { return queryWithTimeout(rs -> {
@Override List<LocalDate> result = new ArrayList<>();
public List<LocalDate> process(Rows rs) throws Exception { while (rs.next()) {
List<LocalDate> result = new ArrayList<>(); LocalDate value = rs.getLocalDateOrNull(1);
while (rs.next()) { if (value != null) {
LocalDate value = rs.getLocalDateOrNull(1); result.add(value);
if (value != null) {
result.add(value);
}
} }
return result;
} }
return result;
}); });
} }
@ -600,18 +523,15 @@ public class SqlSelectImpl implements SqlSelect {
@Override @Override
public <T> T queryOneOrNull(final RowHandler<T> rowHandler) { public <T> T queryOneOrNull(final RowHandler<T> rowHandler) {
return queryWithTimeout(new RowsHandler<T>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public T process(Rows rs) throws Exception { T result = rowHandler.process(rs);
if (rs.next()) { if (rs.next()) {
T result = rowHandler.process(rs); throw new ConstraintViolationException("Expected exactly one row to be returned but found multiple");
if (rs.next()) {
throw new ConstraintViolationException("Expected exactly one row to be returned but found multiple");
}
return result;
} }
return null; return result;
} }
return null;
}); });
} }
@ -626,14 +546,11 @@ public class SqlSelectImpl implements SqlSelect {
@Override @Override
public <T> T queryFirstOrNull(final RowHandler<T> rowHandler) { public <T> T queryFirstOrNull(final RowHandler<T> rowHandler) {
return queryWithTimeout(new RowsHandler<T>() { return queryWithTimeout(rs -> {
@Override if (rs.next()) {
public T process(Rows rs) throws Exception { return rowHandler.process(rs);
if (rs.next()) {
return rowHandler.process(rs);
}
return null;
} }
return null;
}); });
} }
@ -648,20 +565,15 @@ public class SqlSelectImpl implements SqlSelect {
@Override @Override
public <T> List<T> queryMany(final RowHandler<T> rowHandler) { public <T> List<T> queryMany(final RowHandler<T> rowHandler) {
return queryWithTimeout(new RowsHandler<List<T>>() { return queryWithTimeout(rs -> {
@Override List<T> result = new ArrayList<>();
public List<T> process(Rows rs) throws Exception { while (rs.next()) {
List<T> result = new ArrayList<>(); T row = rowHandler.process(rs);
if (row != null) {
while (rs.next()) { result.add(row);
T row = rowHandler.process(rs);
if (row != null) {
result.add(row);
}
} }
return result;
} }
return result;
}); });
} }
@ -757,7 +669,7 @@ public class SqlSelectImpl implements SqlSelect {
if (isSuccess) { if (isSuccess) {
DebugSql.logSuccess("Query", log, metric, executeSql, parameters, options); DebugSql.logSuccess("Query", log, metric, executeSql, parameters, options);
} else if (isWarn) { } else if (isWarn) {
DebugSql.logWarning("Query", log, metric, "QueryTimedOutException", executeSql, parameters, options, logEx); DebugSql.logWarning("Query", log, metric, "QueryTimedOutException", executeSql, parameters, options, null);
} else { } else {
DebugSql.logError("Query", log, metric, errorCode, executeSql, parameters, options, logEx); DebugSql.logError("Query", log, metric, errorCode, executeSql, parameters, options, logEx);
} }

View file

@ -13,7 +13,6 @@ import java.util.logging.Logger;
/** /**
* Convenience class to substitute real values into a database query for debugging, logging, etc. * Convenience class to substitute real values into a database query for debugging, logging, etc.
* <p/>
* WARNING!!! Never execute this SQL without manual inspection because this class does NOTHING * WARNING!!! Never execute this SQL without manual inspection because this class does NOTHING
* to prevent SQL injection or any other bad things. * to prevent SQL injection or any other bad things.
*/ */

View file

@ -48,6 +48,10 @@ public class RowStub {
return !rows.isEmpty() && ++row < rows.size(); return !rows.isEmpty() && ++row < rows.size();
} }
@Override
public Integer rowCount() {
return 0;
}
@Override @Override
public String[] getColumnLabels() { public String[] getColumnLabels() {
@ -55,7 +59,6 @@ public class RowStub {
return columnNames; return columnNames;
} }
@Override @Override
public ResultSetMetaData getMetadata() { public ResultSetMetaData getMetadata() {
requireColumnNames(); requireColumnNames();