Search in sources :

Example 26 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class OptionalLongTest method setupTests.

@Before
public void setupTests() throws IOException {
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setDriverClass("org.h2.Driver");
    dataSourceFactory.setUrl("jdbc:h2:mem:optional-long-" + System.currentTimeMillis() + "?user=sa");
    dataSourceFactory.setInitialSize(1);
    final DBI dbi = new DBIFactory().build(env, dataSourceFactory, "test");
    try (Handle h = dbi.open()) {
        h.execute("CREATE TABLE test (id INT PRIMARY KEY, optional BIGINT)");
    }
    dao = dbi.onDemand(TestDao.class);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) DBI(org.skife.jdbi.v2.DBI) DBIFactory(io.dropwizard.jdbi.DBIFactory) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 27 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project cals-api by ca-cwds.

the class BaseCalsApiApplication method upgardeCalsNsDB.

private void upgardeCalsNsDB(CalsApiConfiguration configuration) {
    LOG.info("Upgrading CALS_NS DB...");
    DataSourceFactory calsnsDataSourceFactory = configuration.getCalsnsDataSourceFactory();
    DatabaseHelper databaseHelper = new DatabaseHelper(calsnsDataSourceFactory.getUrl(), calsnsDataSourceFactory.getUser(), calsnsDataSourceFactory.getPassword());
    try {
        databaseHelper.runScript(LIQUIBASE_CALSNS_DATABASE_CREATE_SCHEMA_XML);
        databaseHelper.runScript(LIQUIBASE_CALSNS_DATABASE_MASTER_XML, calsnsDataSourceFactory.getProperties().get(HIBERNATE_DEFAULT_SCHEMA_PROPERTY_NAME));
    } catch (Exception e) {
        LOG.error("Upgarding of CALS_NS DB is failed. ", e);
    }
    LOG.info("Finish Upgrading CALS_NS DB");
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory)

Example 28 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class AbstractMigrationTest method createConfiguration.

protected static TestMigrationConfiguration createConfiguration(String databaseUrl) {
    final DataSourceFactory dataSource = new DataSourceFactory();
    dataSource.setDriverClass("org.h2.Driver");
    dataSource.setUser("sa");
    dataSource.setUrl(databaseUrl);
    return new TestMigrationConfiguration(dataSource);
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory)

Example 29 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    dbConfig.setProperties(Collections.singletonMap("hibernate.jdbc.time_zone", "UTC"));
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    dbConfig.setUrl("jdbc:h2:mem:DbTest-" + System.nanoTime());
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.h2.Driver");
    dbConfig.setValidationQuery("SELECT 1");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, Collections.singletonList(Person.class));
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("DROP TABLE people IF EXISTS").executeUpdate();
        session.createNativeQuery("CREATE TABLE people (name varchar(100) primary key, email varchar(16), birthday timestamp with time zone)").executeUpdate();
        session.createNativeQuery("INSERT INTO people VALUES ('Coda', 'coda@example.com', '1979-01-02 00:22:00+0:00')").executeUpdate();
        transaction.commit();
    }
    final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting();
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new PersistenceExceptionMapper());
    config.register(new JacksonFeature(Jackson.newObjectMapper()));
    config.register(new DataExceptionMapper());
    config.register(new EmptyOptionalExceptionMapper());
    return config;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) EmptyOptionalExceptionMapper(io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper) JacksonFeature(io.dropwizard.jersey.jackson.JacksonFeature) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session)

Example 30 with DataSourceFactory

use of io.dropwizard.db.DataSourceFactory in project keywhiz by square.

the class ServiceDataSourceModule method readonlyDataSource.

@Provides
@Singleton
@Readonly
ManagedDataSource readonlyDataSource(Environment environment, KeywhizConfig config) {
    DataSourceFactory dataSourceFactory = config.getReadonlyDataSourceFactory();
    ManagedDataSource dataSource = dataSourceFactory.build(environment.metrics(), "db-readonly");
    environment.lifecycle().manage(dataSource);
    environment.healthChecks().register("db-readonly-health", new JooqHealthCheck(dataSource, RETURN_UNHEALTHY));
    return dataSource;
}
Also used : JooqHealthCheck(keywhiz.JooqHealthCheck) DataSourceFactory(io.dropwizard.db.DataSourceFactory) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Readonly(keywhiz.service.config.Readonly) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

DataSourceFactory (io.dropwizard.db.DataSourceFactory)31 DBIFactory (io.dropwizard.jdbi.DBIFactory)16 Before (org.junit.Before)16 DBI (org.skife.jdbi.v2.DBI)15 Handle (org.skife.jdbi.v2.Handle)15 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Provides (com.google.inject.Provides)4 Singleton (com.google.inject.Singleton)4 ManagedDataSource (io.dropwizard.db.ManagedDataSource)4 Environment (io.dropwizard.setup.Environment)4 BeforeEach (org.junit.jupiter.api.BeforeEach)3 AssetsBundle (io.dropwizard.assets.AssetsBundle)2 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)2 JooqHealthCheck (keywhiz.JooqHealthCheck)2 Readonly (keywhiz.service.config.Readonly)2 LifeCycle (org.eclipse.jetty.util.component.LifeCycle)2 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 TimedAnnotationNameStrategy (com.codahale.metrics.jdbi3.strategies.TimedAnnotationNameStrategy)1 RenderCommand (com.example.helloworld.cli.RenderCommand)1