Search in sources :

Example 11 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class TransactionLogStorageFactoryTest method assertCreateRdbTransactionLogStorageFactory.

@Test
public void assertCreateRdbTransactionLogStorageFactory() {
    DataSource dataSource = mock(DataSource.class);
    TransactionLogDataSource transactionLogDataSource = new RdbTransactionLogDataSource(dataSource);
    assertThat(TransactionLogStorageFactory.createTransactionLogStorage(transactionLogDataSource), instanceOf(TransactionLogStorage.class));
}
Also used : RdbTransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.impl.RdbTransactionLogDataSource) MemoryTransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.impl.MemoryTransactionLogDataSource) TransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.TransactionLogDataSource) RdbTransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.impl.RdbTransactionLogDataSource) MemoryTransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.impl.MemoryTransactionLogDataSource) DataSource(javax.sql.DataSource) TransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.TransactionLogDataSource) RdbTransactionLogDataSource(com.dangdang.ddframe.rdb.transaction.soft.datasource.impl.RdbTransactionLogDataSource) Test(org.junit.Test)

Example 12 with DataSource

use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.

the class Main method main.

public static void main(final String[] args) throws SQLException {
    // CHECKSTYLE:ON
    DataSource dataSource = getShardingDataSource();
    updateFailure(dataSource);
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DataSource(javax.sql.DataSource)

Example 13 with DataSource

use of javax.sql.DataSource in project qi4j-sdk by Qi4j.

the class SQLTestHelper method setUpTest.

public static void setUpTest(Module module) {
    Connection connection = null;
    try {
        DataSource ds = module.findService(DataSource.class).get();
        connection = ds.getConnection();
        Assume.assumeNotNull(connection);
    } catch (Throwable t) {
        t.printStackTrace();
        Assume.assumeNoException(t);
    } finally {
        SQLUtil.closeQuietly(connection);
    }
}
Also used : Connection(java.sql.Connection) DataSource(javax.sql.DataSource)

Example 14 with DataSource

use of javax.sql.DataSource in project qi4j-sdk by Qi4j.

the class LiquibaseServiceTest method testLiquibase.

@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
    final SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            ModuleAssembly configModule = module;
            // Create in-memory store for configurations
            new EntityTestAssembler().assemble(configModule);
            new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").withConfig(configModule, Visibility.layer).assemble(module);
            new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
            module.values(SomeValue.class);
            // Set up Liquibase service that will create the tables
            // START SNIPPET: assembly
            new LiquibaseAssembler().withConfig(configModule, Visibility.layer).assemble(module);
            // END SNIPPET: assembly
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
            module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
        }

        @Override
        public void beforeActivation(Application application) {
            application.registerActivationEventListener(new ActivationEventListener() {

                @Override
                public void onEvent(ActivationEvent event) {
                    System.out.println(event);
                }
            });
        }
    };
    Module module = assembler.module();
    // START SNIPPET: io
    // Look up the DataSource
    DataSource ds = module.findService(DataSource.class).get();
    // Instanciate Databases helper
    Databases database = new Databases(ds);
    // Assert that insertion works
    assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
    // END SNIPPET: io
    database.query("select * from test", new Databases.ResultSetVisitor() {

        @Override
        public boolean visit(ResultSet visited) throws SQLException {
            assertThat(visited.getString("id"), equalTo("someid"));
            assertThat(visited.getString("foo"), equalTo("bar"));
            return true;
        }
    });
    Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {

        @Override
        public SomeValue map(ResultSet resultSet) {
            ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
            try {
                builder.prototype().id().set(resultSet.getString("id"));
                builder.prototype().foo().set(resultSet.getString("foo"));
            } catch (SQLException e) {
                throw new IllegalArgumentException("Could not convert to SomeValue", e);
            }
            return builder.newInstance();
        }
    };
    // START SNIPPET: io
    // Select rows and load them in a List
    List<SomeValue> rows = new ArrayList<SomeValue>();
    database.query("select * from test").transferTo(map(toValue, collection(rows)));
    // Transfer all rows to System.out
    Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
Also used : C3P0DataSourceServiceAssembler(org.qi4j.library.sql.c3p0.C3P0DataSourceServiceAssembler) DataSourceAssembler(org.qi4j.library.sql.assembly.DataSourceAssembler) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) Databases(org.qi4j.library.sql.common.Databases) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ResultSet(java.sql.ResultSet) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 15 with DataSource

use of javax.sql.DataSource in project qi4j-sdk by Qi4j.

the class AbstractDataSourceServiceImporterMixin method importService.

@Override
public final synchronized DataSource importService(final ImportedServiceDescriptor importedServiceDescriptor) throws ServiceImporterException {
    PooledDataSourceType pool = pools.get(importedServiceDescriptor.identity());
    if (pool == null) {
        try {
            DataSourceConfigurationValue config = getConfiguration(importedServiceDescriptor.identity());
            if (!config.enabled().get()) {
                // Not started
                throw new ServiceImporterException("DataSource not enabled");
            }
            // Instantiate pool
            pool = setupDataSourcePool(config);
            pools.put(importedServiceDescriptor.identity(), pool);
            LOGGER.info("Starting up DataSource '" + importedServiceDescriptor.identity() + "' for: {}@{}", config.username().get(), config.url().get());
        } catch (Exception e) {
            throw new ServiceImporterException(e);
        }
        // Test the pool
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(null);
        try {
            pool.getConnection().close();
            LOGGER.info("Database for DataSource is up!");
        } catch (SQLException e) {
            LOGGER.warn("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available");
            throw new ServiceImporterException("Database for DataSource " + importedServiceDescriptor.identity() + " is not currently available", e);
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
    }
    // Check if circuitbreaker is used
    final CircuitBreaker circuitBreaker = importedServiceDescriptor.metaInfo(CircuitBreaker.class);
    if (circuitBreaker != null) {
        DataSource wrappedDataSource = DataSources.wrapWithCircuitBreaker(importedServiceDescriptor.identity(), pool, circuitBreaker);
        circuitBreakers.put(pool, circuitBreaker);
        return wrappedDataSource;
    } else {
        return pool;
    }
}
Also used : CircuitBreaker(org.qi4j.library.circuitbreaker.CircuitBreaker) SQLException(java.sql.SQLException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) SQLException(java.sql.SQLException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) DataSource(javax.sql.DataSource)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21