@Provides @Singleton static DSLContext dbContext( DataSource dataSource, @ForDatabase ListeningExecutorService dbExecutor) { Configuration configuration = new DefaultConfiguration() .set(dbExecutor) .set(SQLDialect.MYSQL) .set(new Settings().withRenderSchema(false)) .set(new DataSourceConnectionProvider(dataSource)) .set(DatabaseUtil.sfmRecordMapperProvider()); DSLContext ctx = DSL.using(configuration); // Eagerly trigger JOOQ classinit for better startup performance. ctx.select().from("curio_server_framework_init").getSQL(); return ctx; }
/** * Can we re-use DSLContext as a Spring bean (singleton)? Yes, the Spring tutorial of * Jooq also does it that way, but only if we do not change anything about the * config after the init (which we don't do anyways) and if the ConnectionProvider * does not store any shared state (we use DataSourceConnectionProvider of Jooq, so no problem). * * Some sources and discussion: * - http://www.jooq.org/doc/3.6/manual/getting-started/tutorials/jooq-with-spring/ * - http://jooq-user.narkive.com/2fvuLodn/dslcontext-and-threads * - https://groups.google.com/forum/#!topic/jooq-user/VK7KQcjj3Co * - http://stackoverflow.com/questions/32848865/jooq-dslcontext-correct-autowiring-with-spring */ @Bean public DSLContext dslContext() { initDataSource(); Settings settings = new Settings() // Normally, the records are "attached" to the Configuration that created (i.e. fetch/insert) them. // This means that they hold an internal reference to the same database connection that was used. // The idea behind this is to make CRUD easier for potential subsequent store/refresh/delete // operations. We do not use or need that. .withAttachRecords(false) // To log or not to log the sql queries, that is the question .withExecuteLogging(CONFIG.getDb().isSqlLogging()); // Configuration for JOOQ org.jooq.Configuration conf = new DefaultConfiguration() .set(SQLDialect.MYSQL) .set(new DataSourceConnectionProvider(dataSource)) .set(settings); return DSL.using(conf); }
@Override public void run(T dwConfiguration, Environment environment) throws Exception { final DataSourceFactory dbConfig = getDataSourceFactory(dwConfiguration); ManagedDataSource dataSource = dbConfig.build(environment.metrics(), "jooq"); this.configuration = new DefaultConfiguration(); this.configuration.set(new DataSourceConnectionProvider(dataSource)); configure(this.configuration); environment.jersey().register(JooqTransactionalApplicationListener.class); environment.jersey().register( new ConfigurationFactoryProvider.Binder(this.configuration, dataSource, multiTenantConnectionProvider)); environment.lifecycle().manage(dataSource); if (multiTenantConnectionProvider != null) { environment.lifecycle().manage(multiTenantConnectionProvider); } environment.healthChecks().register("jooq", new JooqHealthCheck( DSL.using(this.configuration.derive(new DefaultConnectionProvider(dataSource.getConnection()))), dbConfig.getValidationQuery())); }
@Provides @Singleton Configuration jooqConfigs(Config config) { HikariConfig hikaryConfig = new HikariConfig(); hikaryConfig.setJdbcUrl(config.get(ConfigConverters.ofString(), "db.jdbcUrl")); hikaryConfig.setUsername(config.get(ConfigConverters.ofString(), "db.username")); hikaryConfig.setPassword(config.get(ConfigConverters.ofString(), "db.password")); HikariDataSource dataSource = new HikariDataSource(hikaryConfig); Configuration jooqConfiguration = new DefaultConfiguration(); jooqConfiguration.set(new DataSourceConnectionProvider(dataSource)); jooqConfiguration.set(DATABASE_DIALECT); return jooqConfiguration; }
private void setupDatabase() { System.setProperty("org.jooq.no-logo", "true"); dataSource = dataSourceFactory.createDataSource("cattle"); DataSourceConnectionProvider dscp = new DataSourceConnectionProvider(dataSource); ThreadLocalTransactionProvider tp = new ThreadLocalTransactionProvider(dscp, false); LoggerListener logger = new LoggerListener(); logger.setMaxLength(1000); ExecuteListener[] listeners = new ExecuteListener[] { logger, new StopWatchListener() }; Settings settings = dbSetting("cattle"); jooqConfig = new DefaultConfiguration() .set(getSQLDialect("cattle")) .set(settings) .set(dscp) .set(tp) .set(DefaultExecuteListenerProvider.providers(listeners)); }
@Override public void configure(final Env env, final Config conf, final Binder binder) { Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name)); HikariDataSource ds = (HikariDataSource) env.get(dskey) .orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey)); Configuration jooqconf = new DefaultConfiguration(); ConnectionProvider dscp = new DataSourceConnectionProvider(ds); jooqconf.set(JDBCUtils.dialect(ds.getDataSourceProperties().getProperty("url"))); jooqconf.set(dscp); jooqconf.set(new DefaultTransactionProvider(dscp)); if (callback != null) { callback.accept(jooqconf, conf); } ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(Configuration.class, name, k -> binder.bind(k).toInstance(jooqconf)); Provider<DSLContext> dsl = () -> DSL.using(jooqconf); serviceKey.generate(DSLContext.class, name, k -> binder.bind(k).toProvider(dsl)); }
@Test(timeout = 4000) public void test22() throws Throwable { WorkspaceSnapshot workspaceSnapshot0 = new WorkspaceSnapshot(); Jdbc3SimpleDataSource jdbc3SimpleDataSource0 = new Jdbc3SimpleDataSource(); DataSourceConnectionProvider dataSourceConnectionProvider0 = new DataSourceConnectionProvider(jdbc3SimpleDataSource0); SQLDialect sQLDialect0 = SQLDialect.FIREBIRD; Settings settings0 = new Settings(); DefaultDSLContext defaultDSLContext0 = new DefaultDSLContext(dataSourceConnectionProvider0, sQLDialect0, settings0); // Undeclared exception! try { workspaceSnapshot0.load(defaultDSLContext0); fail("Expecting exception: NullPointerException"); } catch (NullPointerException e) { // // no message in exception (getMessage() returned null) // assertThrownBy("com.chiralbehaviors.CoRE.workspace.WorkspaceSnapshot", e); } }
@Inject DSLContextProviderImpl(DataSource dataSource, SQLDialect sqlDialect) { // ThreadLocalTransactionProvider handles all of the ThreadLocal semantics for us this.configuration = new DefaultConfiguration() .set(sqlDialect) .set(new ThreadLocalTransactionProvider(new DataSourceConnectionProvider(dataSource))); }
@Bean public DSLContext getDSLContext() { HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setJdbcUrl(environment.getProperty("spring.datasource.url")); hikariDataSource.setUsername(environment.getProperty("spring.datasource.username")); hikariDataSource.setPassword(environment.getProperty("spring.datasource.password")); ConnectionProvider connectionProvider = new DataSourceConnectionProvider(hikariDataSource); org.jooq.Configuration configuration = new DefaultConfiguration().set(connectionProvider).set(SQLDialect.MYSQL); return DSL.using(configuration); }
public static DefaultConfiguration defaultConfigFromDataSource(DataSource ds) { DataSourceConnectionProvider dcp = new DataSourceConnectionProvider(ds); DefaultConfiguration jooqConfig = new DefaultConfiguration(); jooqConfig.set(SQLDialect.MYSQL); jooqConfig.set(dcp); return jooqConfig; }
@Bean @ConditionalOnMissingBean(DataSourceConnectionProvider.class) public DataSourceConnectionProvider dataSourceConnectionProvider( DataSource dataSource) { return new DataSourceConnectionProvider( new TransactionAwareDataSourceProxy(dataSource)); }
@Override public DSLContext getDSL() { Configuration conf = new DefaultConfiguration() .set(SQLDialect.MYSQL) .set(new DataSourceConnectionProvider(this.dataSource)) .set(new DefaultExecuteListenerProvider(jooqLogger)) .set(settings) .set(new DefaultVisitListenerProvider(new TablePrefixer(getTablePrefix()))); return DSL.using(conf); }
public Configuration build(Environment environment, PooledDataSourceFactory factory, String name) throws ClassNotFoundException { final Settings settings = buildSettings(); final ManagedDataSource dataSource = factory.build(environment.metrics(), name); final SQLDialect dialect = determineDialect(factory, dataSource); final ConnectionProvider connectionProvider = new DataSourceConnectionProvider(dataSource); final Configuration config = new DefaultConfiguration() .set(settings) .set(dialect) .set(connectionProvider); environment.lifecycle().manage(dataSource); return config; }
@Bean(name="presets-jooq-configuration") @Inject public DefaultConfiguration configuration(@Named("presets-connection-provider") DataSourceConnectionProvider provider) { DefaultConfiguration jooqConfiguration = new DefaultConfiguration(); jooqConfiguration.set(provider); // jooqConfiguration.set(new DefaultExecuteListenerProvider( // jooqToSpringExceptionTransformer() // )); String sqlDialectName = environment.getProperty(MEV_PRESETS_PROPERTY_PREFIX+"jooq.sql.dialect"); SQLDialect dialect = SQLDialect.valueOf(sqlDialectName); jooqConfiguration.set(dialect); return jooqConfiguration; }
@Produces @ApplicationScoped public ConnectionProvider produceConnectionProvider() { JooqConnectionProvider.log.info("Producing a connection provider"); return new DataSourceConnectionProvider(ds); }
@Produces @ApplicationScoped public ConnectionProvider produceConnectionProvider() { log.info("Producing a connection provider"); return new DataSourceConnectionProvider(ds); }
@Inject public ConnectionFactory(DataSource credentials) { provider = new DataSourceConnectionProvider(credentials); }
@Override protected Application configure() { final MetricRegistry metricRegistry = new MetricRegistry(); final DataSourceFactory dbConfig = new DataSourceFactory(); final Environment environment = mock(Environment.class); final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class); when(environment.lifecycle()).thenReturn(lifecycleEnvironment); when(environment.metrics()).thenReturn(metricRegistry); String url = "jdbc:hsqldb:mem:dwtest" + System.nanoTime(); Map<String, String> props = new HashMap<String, String>(); props.put("username", "sa"); props.put("password", ""); props.put("url", url); try { HSQLDBInit.initPublic(props); } catch (Exception e) { throw new RuntimeException(e); } dbConfig.setUrl(props.get("url")); dbConfig.setUser(props.get("user")); dbConfig.setDriverClass("org.hsqldb.jdbcDriver"); dbConfig.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS"); final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting(new MetricRegistry()); DataSource dataSource = dbConfig.build(metricRegistry, "jooq"); config.register(JooqTransactionalApplicationListener.class); Configuration configuration = new DefaultConfiguration().set(SQLDialect.HSQLDB); configuration.set(new DataSourceConnectionProvider(dataSource)); config.register(new ConfigurationFactoryProvider.Binder(configuration, dataSource, new TestTenantConnectionProvider(dbConfig, metricRegistry, url))); config.register(ExampleResource.class); config.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper(), Validation.buildDefaultValidatorFactory().getValidator())); return config; }
@Bean public DataSourceConnectionProvider connectionProvider() { return new DataSourceConnectionProvider(dataSource()); }
@Bean public DataSourceConnectionProvider dataSourceConnectionProvider() { return new DataSourceConnectionProvider(dataSourceProxy()); }
@Test public void buildsConfigurationUsingDataSourceFactory() throws Exception { Configuration config = factory.build(environment, dataSourceFactory); DataSourceConnectionProvider provider = (DataSourceConnectionProvider) config.connectionProvider(); assertThat(provider.dataSource()).is(managedDataSource); }
@Test public void buildsConfigurationUsingDataSourceFactoryAndName() throws Exception { Configuration config = factory.build(environment, dataSourceFactory, DATASOURCE_NAME); DataSourceConnectionProvider provider = (DataSourceConnectionProvider) config.connectionProvider(); assertThat(provider.dataSource()).is(managedDataSource); }
@Bean(name="presets-connection-provider") @Inject public DataSourceConnectionProvider connectionProvider(@Named("presets-transaction-aware-datasrouce") TransactionAwareDataSourceProxy proxy) { return new DataSourceConnectionProvider(proxy); }
@Bean public DataSourceConnectionProvider connectionProvider() { return new DataSourceConnectionProvider(transactionAwareDataSource()); }