Search in sources :

Example 6 with Settings

use of org.jooq.conf.Settings 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));
    }
}
Also used : DefaultExecuteListener(org.jooq.impl.DefaultExecuteListener) Configuration(org.jooq.Configuration) Query(org.jooq.Query) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) ExecuteListenerProvider(org.jooq.ExecuteListenerProvider) ExecuteContext(org.jooq.ExecuteContext) Settings(org.jooq.conf.Settings) StopWatch(org.jooq.tools.StopWatch)

Example 7 with Settings

use of org.jooq.conf.Settings in project OpenAttestation by OpenAttestation.

the class TagJdbi method jooq.

/**
     * CODE QUALITY: All usage of this method should be in the following form:
     * <pre>
     * try(JooqContainer jc = TagJdbi.jooq()) {
     * DSLContext jooq = jc.getDslContext();
     * // code
     * }
     * </pre>
     *
     * This ensures the jooq database connection is automatically released
     * at the end of the block (either closed or returned to the pool)
     * 
     * @return
     * @throws SQLException
     * @throws IOException
     */
public static JooqContainer jooq() throws SQLException, IOException {
    // omits the schema name from generated sql ; when we connect to the database we already specify a schema so this settings avoid 
    // redundancy in the sql and allows the administrator to change the database name without breaking the application
    Settings settings = new Settings().withRenderSchema(false).withRenderNameStyle(RenderNameStyle.LOWER);
    SQLDialect dbDialect = getSqlDialect();
    // throws SQLException; Note that the DSLContext doesn't close the connection. We'll have to do that ourselves.
    Connection connection = TagJdbi.getDataSource().getConnection();
    DSLContext jooq = DSL.using(connection, dbDialect, settings);
    return new JooqContainer(jooq, connection);
}
Also used : JooqContainer(com.intel.mtwilson.jooq.util.JooqContainer) SQLDialect(org.jooq.SQLDialect) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) Settings(org.jooq.conf.Settings)

Aggregations

Settings (org.jooq.conf.Settings)7 Connection (java.sql.Connection)4 Test (org.junit.Test)4 DSLContext (org.jooq.DSLContext)3 MappedSchema (org.jooq.conf.MappedSchema)2 MappedTable (org.jooq.conf.MappedTable)2 RenderMapping (org.jooq.conf.RenderMapping)2 JooqContainer (com.intel.mtwilson.jooq.util.JooqContainer)1 DriverManager.getConnection (java.sql.DriverManager.getConnection)1 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 Configuration (org.jooq.Configuration)1 ExecuteContext (org.jooq.ExecuteContext)1 ExecuteListenerProvider (org.jooq.ExecuteListenerProvider)1 Query (org.jooq.Query)1 SQLDialect (org.jooq.SQLDialect)1 BookRecord (org.jooq.example.db.h2.tables.records.BookRecord)1 DataChangedException (org.jooq.exception.DataChangedException)1 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)1 DefaultConnectionProvider (org.jooq.impl.DefaultConnectionProvider)1