Search in sources :

Example 11 with DataSourceFactory

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

the class HelloWorldApplication method initialize.

@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
    // Enable variable substitution with environment variables
    bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));
    bootstrap.addCommand(new RenderCommand());
    bootstrap.addBundle(new AssetsBundle());
    bootstrap.addBundle(new MigrationsBundle<HelloWorldConfiguration>() {

        @Override
        public DataSourceFactory getDataSourceFactory(HelloWorldConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    });
    bootstrap.addBundle(hibernateBundle);
    bootstrap.addBundle(new ViewBundle<HelloWorldConfiguration>() {

        @Override
        public Map<String, Map<String, String>> getViewConfiguration(HelloWorldConfiguration configuration) {
            return configuration.getViewRendererConfiguration();
        }
    });
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) RenderCommand(com.example.helloworld.cli.RenderCommand) DataSourceFactory(io.dropwizard.db.DataSourceFactory) EnvironmentVariableSubstitutor(io.dropwizard.configuration.EnvironmentVariableSubstitutor) AssetsBundle(io.dropwizard.assets.AssetsBundle) Map(java.util.Map)

Example 12 with DataSourceFactory

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

the class UnitOfWorkAwareProxyFactoryTest method setUp.

@Before
public void setUp() throws Exception {
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    when(environment.lifecycle()).thenReturn(mock(LifecycleEnvironment.class));
    when(environment.metrics()).thenReturn(new MetricRegistry());
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setUrl("jdbc:hsqldb:mem:unit-of-work-" + UUID.randomUUID().toString());
    dataSourceFactory.setUser("sa");
    dataSourceFactory.setDriverClass("org.hsqldb.jdbcDriver");
    dataSourceFactory.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    dataSourceFactory.setProperties(ImmutableMap.of("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"));
    dataSourceFactory.setInitialSize(1);
    dataSourceFactory.setMinSize(1);
    sessionFactory = new SessionFactoryFactory().build(bundle, environment, dataSourceFactory, ImmutableList.of());
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("create table user_sessions (token varchar(64) primary key, username varchar(16))").executeUpdate();
        session.createNativeQuery("insert into user_sessions values ('67ab89d', 'jeff_28')").executeUpdate();
        transaction.commit();
    }
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session) Before(org.junit.Before)

Example 13 with DataSourceFactory

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

the class CloseableLiquibaseTest method setUp.

@Before
public void setUp() throws Exception {
    DataSourceFactory factory = new DataSourceFactory();
    factory.setDriverClass(org.h2.Driver.class.getName());
    factory.setUrl("jdbc:h2:mem:DbTest-" + System.currentTimeMillis());
    factory.setUser("DbTest");
    dataSource = (ManagedPooledDataSource) factory.build(new MetricRegistry(), "DbTest");
    liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(dataSource, "migrations.xml");
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) Before(org.junit.Before)

Example 14 with DataSourceFactory

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

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    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:hsqldb:mem:DbTest-" + System.nanoTime() + "?hsqldb.translate_dti_types=false");
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.hsqldb.jdbcDriver");
    dbConfig.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, ImmutableList.of(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(new MetricRegistry());
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper()));
    config.register(new PersistenceExceptionMapper());
    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) JacksonMessageBodyProvider(io.dropwizard.jersey.jackson.JacksonMessageBodyProvider) 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 15 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)

Aggregations

DataSourceFactory (io.dropwizard.db.DataSourceFactory)24 Before (org.junit.Before)18 DBIFactory (io.dropwizard.jdbi.DBIFactory)16 DBI (org.skife.jdbi.v2.DBI)15 Handle (org.skife.jdbi.v2.Handle)15 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Environment (io.dropwizard.setup.Environment)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2 ManagedDataSource (io.dropwizard.db.ManagedDataSource)2 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)2 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 RenderCommand (com.example.helloworld.cli.RenderCommand)1 AssetsBundle (io.dropwizard.assets.AssetsBundle)1 EnvironmentVariableSubstitutor (io.dropwizard.configuration.EnvironmentVariableSubstitutor)1 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)1 DropwizardResourceConfig (io.dropwizard.jersey.DropwizardResourceConfig)1 JacksonMessageBodyProvider (io.dropwizard.jersey.jackson.JacksonMessageBodyProvider)1 EmptyOptionalExceptionMapper (io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper)1