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
public Boolean process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getBooleanOrNull(); return rs.getBooleanOrNull();
} }
return null; return null;
}
}); });
} }
@ -259,36 +255,28 @@ public class SqlSelectImpl implements SqlSelect {
@Override @Override
public Long queryLongOrNull() { public Long queryLongOrNull() {
return queryWithTimeout(new RowsHandler<Long>() { return queryWithTimeout(rs -> {
@Override
public Long process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getLongOrNull(1); 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
public Long process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getLongOrZero(1); 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
public List<Long> process(Rows rs) throws Exception {
List<Long> result = new ArrayList<>(); List<Long> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Long value = rs.getLongOrNull(1); Long value = rs.getLongOrNull(1);
@ -297,43 +285,34 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public Integer queryIntegerOrNull() { public Integer queryIntegerOrNull() {
return queryWithTimeout(new RowsHandler<Integer>() { return queryWithTimeout(rs -> {
@Override
public Integer process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getIntegerOrNull(1); 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
public Integer process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getIntegerOrZero(1); 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
public List<Integer> process(Rows rs) throws Exception {
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Integer value = rs.getIntegerOrNull(1); Integer value = rs.getIntegerOrNull(1);
@ -342,43 +321,34 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public Float queryFloatOrNull() { public Float queryFloatOrNull() {
return queryWithTimeout(new RowsHandler<Float>() { return queryWithTimeout(rs -> {
@Override
public Float process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getFloatOrNull(1); 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
public Float process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getFloatOrZero(1); 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
public List<Float> process(Rows rs) throws Exception {
List<Float> result = new ArrayList<>(); List<Float> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Float value = rs.getFloatOrNull(1); Float value = rs.getFloatOrNull(1);
@ -387,43 +357,33 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public Double queryDoubleOrNull() { public Double queryDoubleOrNull() {
return queryWithTimeout(new RowsHandler<Double>() { return queryWithTimeout(rs -> {
@Override
public Double process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getDoubleOrNull(1); 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
public Double process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getDoubleOrZero(1); 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
public List<Double> process(Rows rs) throws Exception {
List<Double> result = new ArrayList<>(); List<Double> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Double value = rs.getDoubleOrNull(1); Double value = rs.getDoubleOrNull(1);
@ -432,44 +392,34 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public BigDecimal queryBigDecimalOrNull() { public BigDecimal queryBigDecimalOrNull() {
return queryWithTimeout(new RowsHandler<BigDecimal>() { return queryWithTimeout(rs -> {
@Override
public BigDecimal process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getBigDecimalOrNull(1); 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
public BigDecimal process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getBigDecimalOrZero(1); 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
public List<BigDecimal> process(Rows rs) throws Exception {
List<BigDecimal> result = new ArrayList<>(); List<BigDecimal> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
BigDecimal value = rs.getBigDecimalOrNull(1); BigDecimal value = rs.getBigDecimalOrNull(1);
@ -478,43 +428,33 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public String queryStringOrNull() { public String queryStringOrNull() {
return queryWithTimeout(new RowsHandler<String>() { return queryWithTimeout(rs -> {
@Override
public String process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getStringOrNull(1); 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
public String process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getStringOrEmpty(1); 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
public List<String> process(Rows rs) throws Exception {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
String value = rs.getStringOrNull(1); String value = rs.getStringOrNull(1);
@ -523,30 +463,22 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@Override @Override
public Date queryDateOrNull() { public Date queryDateOrNull() {
return queryWithTimeout(new RowsHandler<Date>() { return queryWithTimeout(rs -> {
@Override
public Date process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getDateOrNull(1); 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
public List<Date> process(Rows rs) throws Exception {
List<Date> result = new ArrayList<>(); List<Date> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
Date value = rs.getDateOrNull(1); Date value = rs.getDateOrNull(1);
@ -555,32 +487,24 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
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
public LocalDate process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rs.getLocalDateOrNull(1); 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
public List<LocalDate> process(Rows rs) throws Exception {
List<LocalDate> result = new ArrayList<>(); List<LocalDate> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
LocalDate value = rs.getLocalDateOrNull(1); LocalDate value = rs.getLocalDateOrNull(1);
@ -589,7 +513,6 @@ public class SqlSelectImpl implements SqlSelect {
} }
} }
return result; return result;
}
}); });
} }
@ -600,9 +523,7 @@ 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
public T process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
T result = rowHandler.process(rs); T result = rowHandler.process(rs);
if (rs.next()) { if (rs.next()) {
@ -611,7 +532,6 @@ public class SqlSelectImpl implements SqlSelect {
return result; return result;
} }
return null; 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
public T process(Rows rs) throws Exception {
if (rs.next()) { if (rs.next()) {
return rowHandler.process(rs); 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
public List<T> process(Rows rs) throws Exception {
List<T> result = new ArrayList<>(); List<T> result = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
T row = rowHandler.process(rs); T row = rowHandler.process(rs);
if (row != null) { if (row != null) {
result.add(row); 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();