Search in sources :

Example 6 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project mapping-benchmark by arnaudroger.

the class JooqRecordBenchmark method main.

public static void main(String[] args) throws SQLException, NamingException {
    ConnectionParam cp = new ConnectionParam();
    cp.db = DbTarget.H2;
    cp.init();
    DSLContext dsl = DSL.using(new DefaultConfiguration().set(cp.dataSource).set(SQLDialect.H2));
    dsl.selectFrom(TestSmallBenchmarkObject.TEST_SMALL_BENCHMARK_OBJECT).limit(2).forEach(System.out::println);
}
Also used : ConnectionParam(org.simpleflatmapper.db.ConnectionParam) DefaultConfiguration(org.jooq.impl.DefaultConfiguration)

Example 7 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project unipop by unipop-graph.

the class ContextManager method reloadContexts.

private void reloadContexts() throws IOException {
    SQLDialect dialect = SQLDialect.valueOf(this.conf.getString("sqlDialect"));
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(new ObjectMapper().readValue(conf.getJSONArray("address").toString(), List.class).get(0).toString());
    ds.setDriverClassName(conf.getString("driver"));
    String user = conf.optString("user");
    String password = conf.optString("password");
    if (!user.isEmpty())
        ds.setUsername(user);
    if (!password.isEmpty())
        ds.setPassword(password);
    Settings settings = new Settings();
    settings.setRenderNameStyle(RenderNameStyle.AS_IS);
    Configuration conf = new DefaultConfiguration().set(ds).set(dialect).set(settings).set(new DefaultExecuteListenerProvider(new TimingExecuterListener()));
    this.context = DSL.using(conf);
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) List(java.util.List) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) Settings(org.jooq.conf.Settings)

Example 8 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project tutorials by eugenp.

the class InitialConfiguration method configuration.

public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.set(connectionProvider());
    jooqConfiguration.set(new DefaultExecuteListenerProvider(new ExceptionTranslator()));
    return jooqConfiguration;
}
Also used : DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) ExceptionTranslator(com.baeldung.jooq.introduction.ExceptionTranslator) DefaultConfiguration(org.jooq.impl.DefaultConfiguration)

Example 9 with DefaultConfiguration

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

the class Setup method run.

// This class sets up an EntityManager and configures the jOOQ DSLContext
// ----------------------------------------------------------------------
static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception {
    Connection connection = null;
    EntityManagerFactory emf = null;
    EntityManager em = null;
    try {
        // Bootstrapping JDBC:
        Class.forName("org.h2.Driver");
        connection = new LoggingConnection(DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", ""));
        final Connection c = connection;
        // Creating an in-memory H2 database from our entities
        MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect").applySetting("javax.persistence.schema-generation-connection", connection).applySetting("javax.persistence.create-database-schemas", true).applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {

            @SuppressWarnings("rawtypes")
            @Override
            public boolean isUnwrappableAs(Class unwrapType) {
                return false;
            }

            @Override
            public <T> T unwrap(Class<T> unwrapType) {
                return null;
            }

            @Override
            public Connection getConnection() {
                return c;
            }

            @Override
            public void closeConnection(Connection conn) throws SQLException {
            }

            @Override
            public boolean supportsAggressiveRelease() {
                return true;
            }
        }).build());
        metadata.addAnnotatedClass(Actor.class);
        metadata.addAnnotatedClass(Film.class);
        metadata.addAnnotatedClass(Language.class);
        SchemaExport export = new SchemaExport();
        export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
        Map<Object, Object> props = new HashMap<>();
        DataSource ds = new SingleConnectionDataSource(connection);
        props.put("hibernate.connection.datasource", ds);
        props.put("hibernate.archive.autodetection", "");
        emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(pui(ds), props);
        em = emf.createEntityManager();
        final EntityManager e = em;
        // Run some Hibernate / jOOQ logic inside of a transaction
        em.getTransaction().begin();
        data(em);
        consumer.accept(em, new DefaultConfiguration().set(connection).set(onStart(ctx -> e.flush())).dsl());
        em.getTransaction().commit();
    } finally {
        if (em != null)
            em.close();
        if (emf != null)
            emf.close();
        if (connection != null)
            connection.close();
    }
}
Also used : LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) Language(org.jooq.example.jpa.entity.Language) Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) AvailableSettings(org.hibernate.cfg.AvailableSettings) PersistenceUnitTransactionType(jakarta.persistence.spi.PersistenceUnitTransactionType) HashMap(java.util.HashMap) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Film(org.jooq.example.jpa.entity.Film) ValidationMode(jakarta.persistence.ValidationMode) ExecuteListener.onStart(org.jooq.ExecuteListener.onStart) Title(org.jooq.example.jpa.embeddables.Title) SQLException(java.sql.SQLException) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) DataSource(javax.sql.DataSource) DSLContext(org.jooq.DSLContext) ClassTransformer(jakarta.persistence.spi.ClassTransformer) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) EnumSet(java.util.EnumSet) SharedCacheMode(jakarta.persistence.SharedCacheMode) TargetType(org.hibernate.tool.schema.TargetType) LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) Properties(java.util.Properties) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) IOException(java.io.IOException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) MetadataSources(org.hibernate.boot.MetadataSources) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Year(java.time.Year) EntityManager(jakarta.persistence.EntityManager) Actor(org.jooq.example.jpa.entity.Actor) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) Collections(java.util.Collections) DriverManager(java.sql.DriverManager) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) Connection(java.sql.Connection) LoggingConnection(org.jooq.tools.jdbc.LoggingConnection) MetadataSources(org.hibernate.boot.MetadataSources) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DataSource(javax.sql.DataSource) SingleConnectionDataSource(org.jooq.tools.jdbc.SingleConnectionDataSource) EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 10 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration 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(SQLDialect.POSTGRES).set(DefaultExecuteListenerProvider.providers(new DefaultExecuteListener() {

        @Override
        public void executeEnd(ExecuteContext ctx) {
            Configuration config = ctx.configuration().derive();
            config.settings().setRenderFormatted(true);
            log.info("\n" + config.dsl().renderInlined(ctx.query()));
        }

        @Override
        public void fetchEnd(ExecuteContext ctx) {
            log.info("\n" + ctx.result().format(5));
        }
    })));
}
Also used : DefaultExecuteListener(org.jooq.impl.DefaultExecuteListener) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) ExecuteContext(org.jooq.ExecuteContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Properties(java.util.Properties)

Aggregations

DefaultConfiguration (org.jooq.impl.DefaultConfiguration)27 Settings (org.jooq.conf.Settings)7 Connection (java.sql.Connection)6 Configuration (org.jooq.Configuration)6 DSLContext (org.jooq.DSLContext)6 ConnectionParam (org.simpleflatmapper.db.ConnectionParam)6 Test (org.junit.Test)5 DefaultExecuteListenerProvider (org.jooq.impl.DefaultExecuteListenerProvider)4 SfmRecordMapperProvider (org.simpleflatmapper.jooq.SfmRecordMapperProvider)4 Bean (org.springframework.context.annotation.Bean)4 Properties (java.util.Properties)3 SQLDialect (org.jooq.SQLDialect)3 DataSourceConnectionProvider (org.jooq.impl.DataSourceConnectionProvider)3 DefaultConnectionProvider (org.jooq.impl.DefaultConnectionProvider)3 Provides (dagger.Provides)2 List (java.util.List)2 Singleton (javax.inject.Singleton)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 ConnectionProvider (org.jooq.ConnectionProvider)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2