add isValid() to database API

main 1.4.3
Jörg Prante 1 year ago
parent 98819d6577
commit 52097a4468

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

@ -121,6 +121,12 @@ public interface Database extends Supplier<Database> {
*/
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();
/**

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

@ -7,7 +7,6 @@ import org.xbib.jdbc.query.util.Metric;
import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@ -313,14 +312,37 @@ public final class DatabaseProvider implements Supplier<Database>, Closeable {
} finally {
metric.done();
if (logger.isLoggable(Level.FINE)) {
StringBuilder buf = new StringBuilder("Get ").append(builder.options.flavor()).append(" database: ");
metric.printMessage(buf);
logger.fine(buf.toString());
StringBuilder sb = new StringBuilder("Get ").append(builder.options.flavor()).append(" database: ");
metric.printMessage(sb);
logger.fine(sb.toString());
}
}
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() {
if (connection != null) {
try {

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

Loading…
Cancel
Save