Search in sources :

Example 1 with DefaultConnectionProvider

use of org.jooq.impl.DefaultConnectionProvider in project jOOQ by jOOQ.

the class LicenseService method run.

// -------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------
/**
     * This method encapsulates a transaction and initialises a jOOQ DSLcontext.
     * This could also be achieved with Spring and DBCP for connection pooling.
     */
private String run(CtxRunnable runnable) {
    Connection c = null;
    try {
        Class.forName("org.postgresql.Driver");
        c = getConnection("jdbc:postgresql:postgres", "postgres", System.getProperty("pw", "test"));
        DSLContext ctx = DSL.using(new DefaultConfiguration().set(new DefaultConnectionProvider(c)).set(SQLDialect.POSTGRES).set(new Settings().withExecuteLogging(false)));
        return runnable.run(ctx);
    } catch (Exception e) {
        e.printStackTrace();
        Response.status(Status.SERVICE_UNAVAILABLE);
        return "Service Unavailable - Please contact support@datageekery.com for help";
    } finally {
        JDBCUtils.safeClose(c);
    }
}
Also used : DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Connection(java.sql.Connection) DriverManager.getConnection(java.sql.DriverManager.getConnection) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Settings(org.jooq.conf.Settings)

Example 2 with DefaultConnectionProvider

use of org.jooq.impl.DefaultConnectionProvider in project jOOQ by jOOQ.

the class Example_4_4_ExecuteListener method run.

@Test
public void run() {
    Tools.title("Displaying execution time using a custom ExecuteListener");
    ExecuteListener listener = new CallbackExecuteListener().onStart(ctx -> {
        ctx.data("time", System.nanoTime());
    }).onEnd(ctx -> {
        Long time = (Long) ctx.data("time");
        System.out.println("Execution time : " + ((System.nanoTime() - time) / 1000 / 1000.0) + "ms. Query : " + ctx.sql());
    });
    DSL.using(new DefaultConfiguration().set(SQLDialect.H2).set(new DefaultConnectionProvider(connection())).set(new DefaultExecuteListenerProvider(listener))).select(AUTHOR.ID).from(AUTHOR).fetch();
}
Also used : DSL(org.jooq.impl.DSL) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Tools(org.jooq.academy.tools.Tools) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Test(org.junit.Test) SQLDialect(org.jooq.SQLDialect) Tools.connection(org.jooq.academy.tools.Tools.connection) AUTHOR(org.jooq.example.db.h2.Tables.AUTHOR) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Test(org.junit.Test)

Aggregations

DefaultConfiguration (org.jooq.impl.DefaultConfiguration)2 DefaultConnectionProvider (org.jooq.impl.DefaultConnectionProvider)2 Connection (java.sql.Connection)1 DriverManager.getConnection (java.sql.DriverManager.getConnection)1 DSLContext (org.jooq.DSLContext)1 ExecuteListener (org.jooq.ExecuteListener)1 SQLDialect (org.jooq.SQLDialect)1 Tools (org.jooq.academy.tools.Tools)1 Tools.connection (org.jooq.academy.tools.Tools.connection)1 Settings (org.jooq.conf.Settings)1 AUTHOR (org.jooq.example.db.h2.Tables.AUTHOR)1 CallbackExecuteListener (org.jooq.impl.CallbackExecuteListener)1 DSL (org.jooq.impl.DSL)1 DefaultExecuteListenerProvider (org.jooq.impl.DefaultExecuteListenerProvider)1 Test (org.junit.Test)1