clean logging in pool, add row count, use lambdas

main
Jörg Prante 3 years ago
parent 21556e171e
commit 83a12913cf

@ -152,7 +152,7 @@ public class Pool implements BagStateListener {
this.closeConnectionExecutor = createThreadPoolExecutor(maxPoolSize, poolName + " connection closer", threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
this.leakTaskFactory = new ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService);
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.setMaximumPoolSize(Math.min(16, Runtime.getRuntime().availableProcessors()));
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());
}
}
logger.log(Level.INFO, () -> "got data source, setting props = " + config.getProperties());
setTargetFromProperties(ds, config.getProperties());
setLoginTimeout(ds);
this.dataSource = ds;

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

@ -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
* (if possible) and truncate away nanoseconds.

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

@ -13,7 +13,6 @@ import java.util.logging.Logger;
/**
* 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
* to prevent SQL injection or any other bad things.
*/

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

Loading…
Cancel
Save