use thread context class loader

This commit is contained in:
Jörg Prante 2024-03-25 17:09:36 +01:00
parent c2b05476f2
commit 6c064531ab
11 changed files with 160 additions and 97 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = database name = database
version = 2.0.2 version = 2.0.3

View file

@ -118,7 +118,8 @@ public class Pool implements BagStateListener {
* *
* @param config the config * @param config the config
*/ */
public Pool(PoolConfig config) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { public Pool(PoolConfig config) throws ClassNotFoundException,
NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
config.validate(); config.validate();
this.config = config; this.config = config;
logger.log(Level.INFO, () -> "starting new pool: " + config.getPoolName()); logger.log(Level.INFO, () -> "starting new pool: " + config.getPoolName());
@ -146,18 +147,26 @@ public class Pool implements BagStateListener {
checkFailFast(initializationTimeout); checkFailFast(initializationTimeout);
} }
ThreadFactory threadFactory = config.getThreadFactory(); ThreadFactory threadFactory = config.getThreadFactory();
ClassLoader contextClassLoader = config.getContextClassLoader();
int maxPoolSize = config.getMaximumPoolSize(); int maxPoolSize = config.getMaximumPoolSize();
LinkedBlockingQueue<Runnable> addConnectionQueue = new LinkedBlockingQueue<>(maxPoolSize); LinkedBlockingQueue<Runnable> addConnectionQueue = new LinkedBlockingQueue<>(maxPoolSize);
this.addConnectionQueueReadOnlyView = Collections.unmodifiableCollection(addConnectionQueue); this.addConnectionQueueReadOnlyView = Collections.unmodifiableCollection(addConnectionQueue);
this.addConnectionExecutor = createThreadPoolExecutor(addConnectionQueue, this.addConnectionExecutor = createThreadPoolExecutor(addConnectionQueue,
poolName + " connection adder", threadFactory, new ThreadPoolExecutor.DiscardOldestPolicy()); poolName + " connection adder",
threadFactory,
contextClassLoader,
new ThreadPoolExecutor.DiscardOldestPolicy());
this.closeConnectionExecutor = createThreadPoolExecutor(maxPoolSize, this.closeConnectionExecutor = createThreadPoolExecutor(maxPoolSize,
poolName + " connection closer", threadFactory, new ThreadPoolExecutor.CallerRunsPolicy()); poolName + " connection closer",
threadFactory,
contextClassLoader,
new ThreadPoolExecutor.CallerRunsPolicy());
this.leakTaskFactory = new ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService); this.leakTaskFactory = new ProxyLeakTaskFactory(config.getLeakDetectionThreshold(), houseKeepingExecutorService);
this.houseKeeperTask = houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(this), 100L, config.getHousekeepingPeriodMs(), TimeUnit.MILLISECONDS); this.houseKeeperTask = houseKeepingExecutorService.scheduleWithFixedDelay(new HouseKeeper(this), 100L, config.getHousekeepingPeriodMs(), TimeUnit.MILLISECONDS);
if (Boolean.getBoolean("org.xbib.jdbc.connection.pool.blockUntilFilled") && config.getInitializationFailTimeout() > 1) { if (Boolean.getBoolean("org.xbib.jdbc.connection.pool.blockUntilFilled") && config.getInitializationFailTimeout() > 1) {
addConnectionExecutor.setCorePoolSize(Math.min(16, Runtime.getRuntime().availableProcessors())); int procs = Math.min(16, Runtime.getRuntime().availableProcessors());
addConnectionExecutor.setMaximumPoolSize(Math.min(16, Runtime.getRuntime().availableProcessors())); addConnectionExecutor.setCorePoolSize(procs);
addConnectionExecutor.setMaximumPoolSize(procs);
final long startTime = ClockSource.currentTime(); final long startTime = ClockSource.currentTime();
while (ClockSource.elapsedMillis(startTime) < config.getInitializationFailTimeout() && getTotalConnections() < config.getMinimumIdle()) { while (ClockSource.elapsedMillis(startTime) < config.getInitializationFailTimeout() && getTotalConnections() < config.getMinimumIdle()) {
quietlySleep(TimeUnit.MILLISECONDS.toMillis(100)); quietlySleep(TimeUnit.MILLISECONDS.toMillis(100));
@ -305,8 +314,11 @@ public class Pool implements BagStateListener {
addConnectionExecutor.awaitTermination(getLoginTimeout(), TimeUnit.SECONDS); addConnectionExecutor.awaitTermination(getLoginTimeout(), TimeUnit.SECONDS);
destroyHouseKeepingExecutorService(); destroyHouseKeepingExecutorService();
bag.close(); bag.close();
final ExecutorService assassinExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), poolName + " connection assassinator", final ExecutorService assassinExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(),
config.getThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); poolName + " connection assassinator",
config.getThreadFactory(),
config.getContextClassLoader(),
new ThreadPoolExecutor.CallerRunsPolicy());
try { try {
final long start = ClockSource.currentTime(); final long start = ClockSource.currentTime();
do { do {
@ -415,6 +427,7 @@ public class Pool implements BagStateListener {
if (ds == null) { if (ds == null) {
String dsClassName = config.getDataSourceClassName(); String dsClassName = config.getDataSourceClassName();
if (dsClassName != null) { if (dsClassName != null) {
// we must use the system class loader
Class<?> clazz = Class.forName(dsClassName, true, ClassLoader.getSystemClassLoader()); Class<?> clazz = Class.forName(dsClassName, true, ClassLoader.getSystemClassLoader());
ds = (DataSource) clazz.getDeclaredConstructor().newInstance(); ds = (DataSource) clazz.getDeclaredConstructor().newInstance();
} else if (url != null) { } else if (url != null) {
@ -816,7 +829,8 @@ public class Pool implements BagStateListener {
private ScheduledExecutorService initializeHouseKeepingExecutorService() { private ScheduledExecutorService initializeHouseKeepingExecutorService() {
if (config.getScheduledExecutor() == null) { if (config.getScheduledExecutor() == null) {
ThreadFactory threadFactory = Optional.ofNullable(config.getThreadFactory()).orElseGet(() -> ThreadFactory threadFactory = Optional.ofNullable(config.getThreadFactory()).orElseGet(() ->
new DefaultThreadFactory(poolName + "-housekeeper", true)); new DefaultThreadFactory(poolName + "-housekeeper",
Thread.currentThread().getContextClassLoader(), true));
ScheduledThreadPoolExecutor executor = ScheduledThreadPoolExecutor executor =
new ScheduledThreadPoolExecutor(1, threadFactory, new ScheduledThreadPoolExecutor(1, threadFactory,
new ThreadPoolExecutor.DiscardPolicy()); new ThreadPoolExecutor.DiscardPolicy());
@ -869,16 +883,18 @@ public class Pool implements BagStateListener {
* *
* @param queueSize the queue size * @param queueSize the queue size
* @param threadName the thread name * @param threadName the thread name
* @param threadFactory an optional ThreadFactory * @param threadFactory an optional ThreadFactory, if null, a DefaultThreadFactory will be used
* @param contextClassLoader the context class loader if the thread factory is null
* @param policy the RejectedExecutionHandler policy * @param policy the RejectedExecutionHandler policy
* @return a ThreadPoolExecutor * @return a ThreadPoolExecutor
*/ */
private ThreadPoolExecutor createThreadPoolExecutor(int queueSize, private ThreadPoolExecutor createThreadPoolExecutor(int queueSize,
String threadName, String threadName,
ThreadFactory threadFactory, ThreadFactory threadFactory,
ClassLoader contextClassLoader,
RejectedExecutionHandler policy) { RejectedExecutionHandler policy) {
if (threadFactory == null) { if (threadFactory == null) {
threadFactory = new DefaultThreadFactory(threadName, true); threadFactory = new DefaultThreadFactory(threadName, contextClassLoader, true);
} }
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize); LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize);
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1,
@ -899,9 +915,10 @@ public class Pool implements BagStateListener {
private ThreadPoolExecutor createThreadPoolExecutor(BlockingQueue<Runnable> queue, private ThreadPoolExecutor createThreadPoolExecutor(BlockingQueue<Runnable> queue,
String threadName, String threadName,
ThreadFactory threadFactory, ThreadFactory threadFactory,
ClassLoader contextClassLoader,
RejectedExecutionHandler policy) { RejectedExecutionHandler policy) {
if (threadFactory == null) { if (threadFactory == null) {
threadFactory = new DefaultThreadFactory(threadName, true); threadFactory = new DefaultThreadFactory(threadName, contextClassLoader, true);
} }
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1,
5, TimeUnit.SECONDS, queue, threadFactory, policy); 5, TimeUnit.SECONDS, queue, threadFactory, policy);
@ -977,7 +994,7 @@ public class Pool implements BagStateListener {
* *
* @return true if we should create a connection, false if the need has disappeared * @return true if we should create a connection, false if the need has disappeared
*/ */
synchronized boolean shouldCreateAnotherConnection() { public synchronized boolean shouldCreateAnotherConnection() {
return getTotalConnections() < config.getMaximumPoolSize() && return getTotalConnections() < config.getMaximumPoolSize() &&
(bag.getWaitingThreadCount() > 0 || getIdleConnections() < config.getMinimumIdle()); (bag.getWaitingThreadCount() > 0 || getIdleConnections() < config.getMinimumIdle());
} }

View file

@ -1,5 +1,6 @@
package org.xbib.jdbc.connection.pool; package org.xbib.jdbc.connection.pool;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection; import java.sql.Connection;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -74,6 +75,8 @@ public class PoolConfig {
private ThreadFactory threadFactory; private ThreadFactory threadFactory;
private ClassLoader contextClassLoader;
private ScheduledExecutorService scheduledExecutor; private ScheduledExecutorService scheduledExecutor;
/** /**
@ -297,15 +300,6 @@ public class PoolConfig {
this.dataSource = dataSource; this.dataSource = dataSource;
} }
/**
* Get the name of the JDBC {@link DataSource} class used to create Connections.
*
* @return the fully qualified name of the JDBC {@link DataSource} class
*/
public String getDataSourceClassName() {
return dataSourceClassName;
}
/** /**
* Set the fully qualified class name of the JDBC {@link DataSource} that will be used create Connections. * Set the fully qualified class name of the JDBC {@link DataSource} that will be used create Connections.
* *
@ -315,6 +309,15 @@ public class PoolConfig {
this.dataSourceClassName = className; this.dataSourceClassName = className;
} }
/**
* Get the name of the JDBC {@link DataSource} class used to create Connections.
*
* @return the fully qualified name of the JDBC {@link DataSource} class
*/
public String getDataSourceClassName() {
return dataSourceClassName;
}
public Properties getProperties() { public Properties getProperties() {
return properties; return properties;
} }
@ -324,23 +327,47 @@ public class PoolConfig {
} }
public void setDriverClassName(String driverClassName) { public void setDriverClassName(String driverClassName) {
Class<?> driverClass = attemptFromContextLoader(driverClassName); Class<?> driverClass = null;
ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
if (threadContextClassLoader != null) {
try { try {
if (driverClass == null) { driverClass = threadContextClassLoader.loadClass(driverClassName);
driverClass = this.getClass().getClassLoader().loadClass(driverClassName); if (driverClass != null) {
logger.log(Level.FINE, () -> "driver class found in the PoolConfig class classloader: " + driverClassName + " " + this.getClass().getClassLoader()); logger.log(Level.FINE, () -> "driver class found in thread context class loader:" + driverClassName);
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, "failed to load driver class from PoolConfig class classloader: " + driverClassName + " " + this.getClass().getClassLoader()); logger.log(Level.FINE, () -> "failed to load driver class from thread context class loader" + driverClassName);
}
}
try {
if (driverClass == null) {
driverClass = getClass().getClassLoader().loadClass(driverClassName);
if (driverClass != null) {
logger.log(Level.FINE, () -> "driver class found in the class classloader: " + driverClassName);
}
}
} catch (ClassNotFoundException e) {
logger.log(Level.FINE, () -> "failed to load driver class from class classloader: " + driverClassName);
}
try {
if (driverClass == null) {
driverClass = ClassLoader.getSystemClassLoader().loadClass(driverClassName);
if (driverClass != null) {
logger.log(Level.FINE, () -> "driver class found in the system class classloader: " + driverClassName);
}
}
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, () -> "failed to load driver class from system class classloader: " + driverClassName);
} }
if (driverClass == null) { if (driverClass == null) {
throw new RuntimeException("failed to load driver class " + driverClassName + " in either of PoolConfig class loader or Thread context classloader"); throw new IllegalStateException("failed to load driver class " + driverClassName);
} }
try { try {
driverClass.getConstructor().newInstance(); driverClass.getConstructor().newInstance();
this.driverClassName = driverClassName; this.driverClassName = driverClassName;
} catch (Exception e) { } catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to instantiate class " + driverClassName, e); throw new IllegalStateException("failed to instantiate class " + driverClassName, e);
} }
} }
@ -549,15 +576,6 @@ public class PoolConfig {
return housekeepingPeriodMs; return housekeepingPeriodMs;
} }
/**
* Get the thread factory used to create threads.
*
* @return the thread factory (may be null, in which case the default thread factory is used)
*/
public ThreadFactory getThreadFactory() {
return threadFactory;
}
/** /**
* Set the thread factory to be used to create threads. * Set the thread factory to be used to create threads.
* *
@ -567,20 +585,21 @@ public class PoolConfig {
this.threadFactory = threadFactory; this.threadFactory = threadFactory;
} }
private Class<?> attemptFromContextLoader(String driverClassName) { /**
ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader(); * Get the thread factory used to create threads.
if (threadContextClassLoader != null) { *
try { * @return the thread factory (may be null, in which case the default thread factory is used)
Class<?> driverClass = threadContextClassLoader.loadClass(driverClassName); */
logger.log(Level.FINE, "Driver class found in Thread context class loader:" + public ThreadFactory getThreadFactory() {
driverClassName + " " + threadContextClassLoader); return threadFactory;
return driverClass;
} catch (ClassNotFoundException e) {
logger.log(Level.FINE, "Driver class not found in Thread context class loader, trying classloader: " +
driverClassName + " " + threadContextClassLoader + " " + this.getClass().getClassLoader());
} }
public void setContextClassLoader(ClassLoader contextClassLoader) {
this.contextClassLoader = contextClassLoader;
} }
return null;
public ClassLoader getContextClassLoader() {
return contextClassLoader != null ? contextClassLoader : Thread.currentThread().getContextClassLoader();
} }
@SuppressWarnings("StatementWithEmptyBody") @SuppressWarnings("StatementWithEmptyBody")

View file

@ -31,7 +31,8 @@ public class PoolEntryCreator implements Callable<Boolean> {
final PoolEntry poolEntry = pool.createPoolEntry(); final PoolEntry poolEntry = pool.createPoolEntry();
if (poolEntry != null) { if (poolEntry != null) {
pool.bag.add(poolEntry); pool.bag.add(poolEntry);
logger.log(Level.FINE, () -> MessageFormat.format("{0}: added connection {1} ", pool.poolName, poolEntry.getConnection())); logger.log(Level.FINE, () -> MessageFormat.format("{0}: added connection {1} ",
pool.poolName, poolEntry.getConnection()));
if (loggingPrefix != null) { if (loggingPrefix != null) {
pool.logPoolState(loggingPrefix); pool.logPoolState(loggingPrefix);
} }

View file

@ -6,10 +6,15 @@ public class DefaultThreadFactory implements ThreadFactory {
private final String threadName; private final String threadName;
private final ClassLoader contextClassLoader;
private final boolean daemon; private final boolean daemon;
public DefaultThreadFactory(String threadName, boolean daemon) { public DefaultThreadFactory(String threadName,
ClassLoader contextClassLoader,
boolean daemon) {
this.threadName = threadName; this.threadName = threadName;
this.contextClassLoader = contextClassLoader;
this.daemon = daemon; this.daemon = daemon;
} }
@ -17,6 +22,7 @@ public class DefaultThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
Thread thread = new Thread(r, threadName); Thread thread = new Thread(r, threadName);
thread.setDaemon(daemon); thread.setDaemon(daemon);
thread.setContextClassLoader(contextClassLoader);
return thread; return thread;
} }
} }

View file

@ -17,16 +17,21 @@ public class DriverDataSource implements DataSource {
private static final Logger logger = Logger.getLogger(DriverDataSource.class.getName()); private static final Logger logger = Logger.getLogger(DriverDataSource.class.getName());
static {
DriverManager.setLogWriter(new PrintWriter(new LoggingOutputStream(logger)));
DriverManager.drivers().forEach(d -> logger.log(Level.INFO, "found driver " + d));
}
private static final String PASSWORD = "password"; private static final String PASSWORD = "password";
private static final String USER = "user"; private static final String USER = "user";
private static final String AUTOCOMMIT = "autocommit"; private static final String AUTOCOMMIT = "autocommit";
private String jdbcUrl;
private final Properties driverProperties; private final Properties driverProperties;
private String jdbcUrl;
private Driver driver; private Driver driver;
public DriverDataSource(String jdbcUrl, public DriverDataSource(String jdbcUrl,
@ -39,7 +44,9 @@ public class DriverDataSource implements DataSource {
for (Entry<Object, Object> entry : properties.entrySet()) { for (Entry<Object, Object> entry : properties.entrySet()) {
driverProperties.setProperty(entry.getKey().toString(), entry.getValue().toString()); driverProperties.setProperty(entry.getKey().toString(), entry.getValue().toString());
} }
logger.log(Level.INFO, "DriverManager looking for JDBC URL " + jdbcUrl); if (jdbcUrl != null) {
logger.log(Level.INFO, () -> "DriverManager looking for JDBC URL " + jdbcUrl);
}
if (username != null) { if (username != null) {
setUser(username); setUser(username);
} }
@ -85,18 +92,19 @@ public class DriverDataSource implements DataSource {
} }
} }
} }
if (driver == null && jdbcUrl != null) {
final String sanitizedUrl = jdbcUrl.replaceAll("([?&;][^&#;=]*[pP]assword=)[^&#;]*", "$1<masked>"); final String sanitizedUrl = jdbcUrl.replaceAll("([?&;][^&#;=]*[pP]assword=)[^&#;]*", "$1<masked>");
try { try {
if (driver == null) {
driver = DriverManager.getDriver(jdbcUrl); driver = DriverManager.getDriver(jdbcUrl);
logger.log(Level.FINE, "Loaded driver with class name for jdbcUrl " + driver.getClass().getName() + " " + sanitizedUrl); logger.log(Level.FINE, "Loaded driver with class name for jdbcUrl " + driver.getClass().getName() + " " + sanitizedUrl);
} else if (!driver.acceptsURL(jdbcUrl)) { if (!driver.acceptsURL(sanitizedUrl)) {
throw new RuntimeException("Driver " + driverClassName + " claims to not accept jdbcUrl " + sanitizedUrl); throw new RuntimeException("Driver " + driverClassName + " claims to not accept jdbcUrl " + sanitizedUrl);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException("Failed to get driver instance for jdbcUrl " + sanitizedUrl, e); throw new RuntimeException("Failed to get driver instance for jdbcUrl " + sanitizedUrl, e);
} }
} }
}
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {

View file

@ -0,0 +1,26 @@
package org.xbib.jdbc.connection.pool.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingOutputStream extends ByteArrayOutputStream {
private final Logger logger;
public LoggingOutputStream(Logger logger) {
super();
this.logger = logger;
}
@Override
public void flush() throws IOException {
super.flush();
String s = new String(buf, 0, count);
if (!s.isEmpty()) {
logger.log(Level.FINE, s);
}
reset();
}
}

View file

@ -17,8 +17,10 @@ public class HouseKeeperCleanupTest {
private ScheduledThreadPoolExecutor executor; private ScheduledThreadPoolExecutor executor;
@BeforeEach @BeforeEach
public void before() throws Exception { public void before() {
ThreadFactory threadFactory = new DefaultThreadFactory("global-housekeeper", true); ThreadFactory threadFactory = new DefaultThreadFactory("global-housekeeper",
Thread.currentThread().getContextClassLoader(),
true);
executor = new ScheduledThreadPoolExecutor(1, threadFactory, executor = new ScheduledThreadPoolExecutor(1, threadFactory,
new ThreadPoolExecutor.DiscardPolicy()); new ThreadPoolExecutor.DiscardPolicy());
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);

View file

@ -27,7 +27,6 @@ public class MariaDBTest extends CommonTest {
static MariaDBContainer<?> mariaDBContainer; static MariaDBContainer<?> mariaDBContainer;
static { static {
// mariadb 10.3.6
mariaDBContainer = new MariaDBContainer<>("mariadb") mariaDBContainer = new MariaDBContainer<>("mariadb")
.withDatabaseName("testDB") .withDatabaseName("testDB")
.withUsername("testUser") .withUsername("testUser")
@ -45,7 +44,7 @@ public class MariaDBTest extends CommonTest {
} }
@Override @Override
protected DatabaseProvider createDatabaseProvider(OptionsOverride options) throws Exception { protected DatabaseProvider createDatabaseProvider(OptionsOverride options) {
Config config = ConfigSupplier.of() Config config = ConfigSupplier.of()
.property("database.url", mariaDBContainer.getJdbcUrl()) .property("database.url", mariaDBContainer.getJdbcUrl())
.property("database.user", "testUser") .property("database.user", "testUser")
@ -95,12 +94,14 @@ public class MariaDBTest extends CommonTest {
} }
@Disabled("MariaDB temporarily disabled") @Disabled("MariaDB temporarily disabled")
@Test
@Override @Override
public void intervals() { public void intervals() {
super.intervals(); super.intervals();
} }
@Disabled("MariaDB temporarily disabled") @Disabled("MariaDB temporarily disabled")
@Test
@Override @Override
public void metadataColumnTypes() { public void metadataColumnTypes() {
super.metadataColumnTypes(); super.metadataColumnTypes();
@ -251,5 +252,4 @@ public class MariaDBTest extends CommonTest {
+ " str_varchar, str_fixed, str_lob, bin_blob, boolean_flag, date_millis, local_date from dbtest2 order by 1") + " str_varchar, str_fixed, str_lob, bin_blob, boolean_flag, date_millis, local_date from dbtest2 order by 1")
.queryMany(SqlArgs::readRow)); .queryMany(SqlArgs::readRow));
} }
} }

View file

@ -18,13 +18,8 @@ import java.util.logging.Logger;
* to prevent SQL injection or any other bad things. * to prevent SQL injection or any other bad things.
*/ */
public class DebugSql { public class DebugSql {
public static final String PARAM_SQL_SEPARATOR = "\tParamSql:\t";
public static String printDebugOnlySqlString(String sql, Object[] args, Options options) { public static final String PARAM_SQL_SEPARATOR = "\tParamSql:\t";
StringBuilder buf = new StringBuilder();
printSql(buf, sql, args, false, true, options);
return buf.toString();
}
public static void printSql(StringBuilder buf, String sql, Object[] args, Options options) { public static void printSql(StringBuilder buf, String sql, Object[] args, Options options) {
printSql(buf, sql, args, true, options.isLogParameters(), options); printSql(buf, sql, args, true, options.isLogParameters(), options);
@ -65,8 +60,7 @@ public class DebugSql {
for (int i = 0; i < argsToPrint.length; i++) { for (int i = 0; i < argsToPrint.length; i++) {
buf.append(removeTabs(sqlParts[i])); buf.append(removeTabs(sqlParts[i]));
Object argToPrint = argsToPrint[i]; Object argToPrint = argsToPrint[i];
if (argToPrint instanceof String) { if (argToPrint instanceof String argToPrintString) {
String argToPrintString = (String) argToPrint;
int maxLength = options.maxStringLengthParam(); int maxLength = options.maxStringLengthParam();
if (argToPrintString.length() > maxLength && maxLength > 0) { if (argToPrintString.length() > maxLength && maxLength > 0) {
buf.append("'").append(argToPrintString, 0, maxLength).append("...'"); buf.append("'").append(argToPrintString, 0, maxLength).append("...'");
@ -77,16 +71,12 @@ public class DebugSql {
} }
} else if (argToPrint instanceof SqlNull || argToPrint == null) { } else if (argToPrint instanceof SqlNull || argToPrint == null) {
buf.append("null"); buf.append("null");
} else if (argToPrint instanceof java.sql.Timestamp) { } else if (argToPrint instanceof java.sql.Timestamp timestamp) {
java.sql.Timestamp timestamp = (java.sql.Timestamp) argToPrint;
LocalDateTime localDateTime = timestamp.toLocalDateTime(); LocalDateTime localDateTime = timestamp.toLocalDateTime();
buf.append(localDateTime.toString()); buf.append(localDateTime.toString());
//buf.append(options.flavor().dateAsSqlFunction((Timestamp) argToPrint, options.calendarForTimestamps())); } else if (argToPrint instanceof java.sql.Date date) {
} else if (argToPrint instanceof java.sql.Date) {
java.sql.Date date = (java.sql.Date) argToPrint;
LocalDate localDate = date.toLocalDate(); LocalDate localDate = date.toLocalDate();
buf.append(localDate.toString()); buf.append(localDate.toString());
//buf.append(options.flavor().localDateAsSqlFunction((java.sql.Date) argToPrint));
} else if (argToPrint instanceof Number) { } else if (argToPrint instanceof Number) {
buf.append(argToPrint); buf.append(argToPrint);
} else if (argToPrint instanceof Boolean) { } else if (argToPrint instanceof Boolean) {
@ -144,25 +134,22 @@ public class DebugSql {
} }
public static void logSuccess(String sqlType, Logger log, Metric metric, String sql, Object[] args, Options options) { public static void logSuccess(String sqlType, Logger log, Metric metric, String sql, Object[] args, Options options) {
if (log.isLoggable(Level.FINE)) { if (log.isLoggable(Level.FINEST)) {
String msg = logMiddle('\t', sqlType, metric, null, sql, args, options); log.log(Level.FINEST, () -> logMiddle('\t', sqlType, metric, null, sql, args, options));
log.fine(msg);
} }
} }
public static void logWarning(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args, public static void logWarning(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args,
Options options, Throwable t) { Options options, Throwable t) {
if (log.isLoggable(Level.WARNING)) { if (log.isLoggable(Level.WARNING)) {
String msg = logMiddle(' ', sqlType, metric, errorCode, sql, args, options); log.log(Level.WARNING, logMiddle(' ', sqlType, metric, errorCode, sql, args, options), t);
log.log(Level.WARNING, msg, t);
} }
} }
public static void logError(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args, public static void logError(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args,
Options options, Throwable t) { Options options, Throwable t) {
if (log.isLoggable(Level.SEVERE)) { if (log.isLoggable(Level.SEVERE)) {
String msg = logMiddle(' ', sqlType, metric, errorCode, sql, args, options); log.log(Level.SEVERE, logMiddle(' ', sqlType, metric, errorCode, sql, args, options), t);
log.log(Level.SEVERE, msg, t);
} }
} }

View file

@ -2,12 +2,12 @@ dependencyResolutionManagement {
versionCatalogs { versionCatalogs {
libs { libs {
version('gradle', '8.5') version('gradle', '8.5')
library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.3') library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.3.3')
library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('23.2.0.0') library('oracle', 'com.oracle.database.jdbc', 'ojdbc11').version('23.3.0.23.09')
library('postgresql', 'org.postgresql', 'postgresql').version('42.6.0') library('postgresql', 'org.postgresql', 'postgresql').version('42.7.3')
} }
testLibs { testLibs {
version('junit', '5.10.1') version('junit', '5.10.2')
version('testcontainers', '1.19.1') version('testcontainers', '1.19.1')
library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit') library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit') library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit')
@ -18,9 +18,6 @@ dependencyResolutionManagement {
library('derby', 'org.apache.derby', 'derby').version('10.16.1.1') library('derby', 'org.apache.derby', 'derby').version('10.16.1.1')
library('hsqldb', 'org.hsqldb', 'hsqldb').version('2.7.1') library('hsqldb', 'org.hsqldb', 'hsqldb').version('2.7.1')
library('h2', 'com.h2database', 'h2').version('2.1.214') library('h2', 'com.h2database', 'h2').version('2.1.214')
library('mariadb', 'org.mariadb.jdbc', 'mariadb-java-client').version('3.1.3')
library('oracle', 'com.oracle.database.jdbc','ojdbc11').version('23.2.0.0')
library('postgresql', 'org.postgresql', 'postgresql').version('42.6.0')
library('mockito-core', 'org.mockito', 'mockito-core').version('5.3.1') library('mockito-core', 'org.mockito', 'mockito-core').version('5.3.1')
library('testcontainers', 'org.testcontainers', 'testcontainers').versionRef('testcontainers') library('testcontainers', 'org.testcontainers', 'testcontainers').versionRef('testcontainers')
library('testcontainers-junit-jupiter', 'org.testcontainers', 'junit-jupiter').versionRef('testcontainers') library('testcontainers-junit-jupiter', 'org.testcontainers', 'junit-jupiter').versionRef('testcontainers')