use of org.jooq.impl.DefaultExecuteListener in project jOOQ by jOOQ.
the class SakilaReportService method initDB.
private static void initDB() throws Exception {
final Properties properties = new Properties();
properties.load(SakilaReportService.class.getResourceAsStream("/config.properties"));
Class.forName(properties.getProperty("db.driver"));
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(properties.getProperty("db.url"));
ds.setUsername(properties.getProperty("db.username"));
ds.setPassword(properties.getProperty("db.password"));
// Some nice debug logging of formatted queries and the first 5 rows in each result set.
dsl = DSL.using(new DefaultConfiguration().set(ds).set(DefaultExecuteListenerProvider.providers(new DefaultExecuteListener() {
@Override
public void executeEnd(ExecuteContext ctx) {
Configuration config = ctx.configuration().derive();
config.settings().setRenderFormatted(true);
log.info("\n" + DSL.using(config).renderInlined(ctx.query()));
}
@Override
public void fetchEnd(ExecuteContext ctx) {
log.info("\n" + ctx.result().format(5));
}
})));
}
use of org.jooq.impl.DefaultExecuteListener in project jOOQ by jOOQ.
the class AbstractDatabase method create.
@SuppressWarnings("serial")
protected final DSLContext create(boolean muteExceptions) {
// [#3800] Make sure that faulty queries are logged in a formatted
// way to help users provide us with bug reports
final Configuration configuration = create0().configuration();
if (muteExceptions) {
return DSL.using(configuration);
} else {
final Settings newSettings = SettingsTools.clone(configuration.settings()).withRenderFormatted(true);
final ExecuteListenerProvider[] oldProviders = configuration.executeListenerProviders();
final ExecuteListenerProvider[] newProviders = new ExecuteListenerProvider[oldProviders.length + 1];
System.arraycopy(oldProviders, 0, newProviders, 0, oldProviders.length);
newProviders[oldProviders.length] = new DefaultExecuteListenerProvider(new DefaultExecuteListener() {
class SQLPerformanceWarning extends Exception {
}
@Override
public void start(ExecuteContext ctx) {
// SQLPerformanceWarning.
if (!initialised) {
DSL.using(configuration).selectOne().fetch();
initialised = true;
}
}
@Override
public void executeStart(ExecuteContext ctx) {
ctx.data("org.jooq.util.AbstractDatabase.watch", new StopWatch());
}
@Override
public void executeEnd(ExecuteContext ctx) {
StopWatch watch = (StopWatch) ctx.data("org.jooq.util.AbstractDatabase.watch");
if (watch.split() > TimeUnit.SECONDS.toNanos(5L)) {
watch.splitWarn("Slow SQL");
log.warn("Slow SQL", "jOOQ Meta executed a slow query (slower than 5 seconds)" + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()), new SQLPerformanceWarning());
}
}
@Override
public void exception(ExecuteContext ctx) {
log.warn("SQL exception", "Exception while executing meta query: " + (ctx.sqlException() != null ? ctx.sqlException().getMessage() : ctx.exception() != null ? ctx.exception().getMessage() : "No exception available") + "\n\n" + "Please report this bug here: https://github.com/jOOQ/jOOQ/issues/new\n\n" + formatted(ctx.query()));
}
private String formatted(Query query) {
return DSL.using(configuration.derive(newSettings)).renderInlined(query);
}
});
return DSL.using(configuration.derive(newProviders));
}
}
Aggregations