add isValid() to database API

This commit is contained in:
Jörg Prante 2023-08-21 18:35:58 +02:00
parent 98819d6577
commit 52097a4468
5 changed files with 42 additions and 5 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = database name = database
version = 1.4.2 version = 1.4.3
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -121,6 +121,12 @@ public interface Database extends Supplier<Database> {
*/ */
Connection underlyingConnection(); Connection underlyingConnection();
/**
* Probe the underlying connection.
* @return true if database has executed a probe query successfully, false if not
*/
boolean probe() throws SQLException;
Options options(); Options options();
/** /**

View file

@ -139,6 +139,12 @@ public class DatabaseImpl implements Database {
return connection; return connection;
} }
@Override
public boolean probe() throws SQLException {
// use a standard timeout of 5 seconds
return connection != null && connection.isValid(5);
}
@Override @Override
public Options options() { public Options options() {
return options; return options;

View file

@ -7,7 +7,6 @@ import org.xbib.jdbc.query.util.Metric;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
@ -313,14 +312,37 @@ public final class DatabaseProvider implements Supplier<Database>, Closeable {
} finally { } finally {
metric.done(); metric.done();
if (logger.isLoggable(Level.FINE)) { if (logger.isLoggable(Level.FINE)) {
StringBuilder buf = new StringBuilder("Get ").append(builder.options.flavor()).append(" database: "); StringBuilder sb = new StringBuilder("Get ").append(builder.options.flavor()).append(" database: ");
metric.printMessage(buf); metric.printMessage(sb);
logger.fine(buf.toString()); logger.fine(sb.toString());
} }
} }
return database; return database;
} }
public boolean check() {
Metric metric = new Metric(logger.isLoggable(Level.FINE));
boolean probe = false;
try {
if (database != null) {
probe = database.probe();
} else {
get();
probe = database.probe();
}
} catch (SQLException | RuntimeException e) {
metric.checkpoint("fail", e.getMessage());
} finally {
metric.done();
if (logger.isLoggable(Level.FINE)) {
StringBuilder sb = new StringBuilder("Probe ").append(builder.options.flavor()).append(" database: ");
metric.printMessage(sb);
logger.log(Level.FINE, sb.toString());
}
}
return probe;
}
public void commit() { public void commit() {
if (connection != null) { if (connection != null) {
try { try {

View file

@ -77,6 +77,9 @@ public abstract class CommonTest {
dbp = createDatabaseProvider(new OptionsOverride() { dbp = createDatabaseProvider(new OptionsOverride() {
}); });
db = dbp.get(); db = dbp.get();
if (!db.probe()) {
fail();
}
db.dropTableQuietly(TEST_TABLE_NAME); db.dropTableQuietly(TEST_TABLE_NAME);
} }