Search in sources :

Example 16 with DataSource

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

the class DataSources method wrapWithCircuitBreaker.

public static DataSource wrapWithCircuitBreaker(final String dataSourceIdentity, final DataSource pool, final CircuitBreaker circuitBreaker) {
    // Create wrapper
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (!circuitBreaker.isOn()) {
                Throwable throwable = circuitBreaker.lastThrowable();
                if (throwable != null) {
                    throw throwable;
                } else {
                    throw new ServiceImporterException("Circuit breaker for DataSource " + dataSourceIdentity + " is not on");
                }
            }
            try {
                Object result = method.invoke(pool, args);
                circuitBreaker.success();
                return result;
            } catch (IllegalAccessException e) {
                circuitBreaker.throwable(e);
                throw e;
            } catch (IllegalArgumentException e) {
                circuitBreaker.throwable(e);
                throw e;
            } catch (InvocationTargetException e) {
                circuitBreaker.throwable(e.getCause());
                throw e.getCause();
            }
        }
    };
    // Create proxy with circuit breaker
    return (DataSource) Proxy.newProxyInstance(DataSource.class.getClassLoader(), new Class[] { DataSource.class }, handler);
}
Also used : ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSource(javax.sql.DataSource)

Example 17 with DataSource

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

the class ExternalDataSourceTest method test.

@Test
public void test() throws SQLException {
    DataSource dataSource = module.findService(DataSource.class).get();
    Connection connection = dataSource.getConnection();
    try {
        connection.getMetaData();
    } finally {
        SQLUtil.closeQuietly(connection);
    }
}
Also used : Connection(java.sql.Connection) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 18 with DataSource

use of javax.sql.DataSource in project sonarqube by SonarSource.

the class Migration1223Test method setUp.

@Before
public void setUp() throws Exception {
    DataSource dataSource = mock(DataSource.class);
    when(database.getDataSource()).thenReturn(dataSource);
    Connection connection = mock(Connection.class);
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.getMetaData()).thenReturn(mock(DatabaseMetaData.class));
}
Also used : Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) Before(org.junit.Before)

Example 19 with DataSource

use of javax.sql.DataSource in project archaius by Netflix.

the class JDBCConfigurationSourceTest method testSimpleInMemoryJDBCDynamicPropertySource.

@Test
public void testSimpleInMemoryJDBCDynamicPropertySource() throws Throwable {
    final String dbName = "MySiteConfiguration";
    DataSource ds = createDataConfigSource(dbName);
    try {
        JDBCConfigurationSource source = new JDBCConfigurationSource(ds, "select distinct property_key, property_value from MySiteProperties", "property_key", "property_value");
        FixedDelayPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 10, false);
        DynamicConfiguration configuration = new DynamicConfiguration(source, scheduler);
        DynamicPropertyFactory.initWithConfigurationSource(configuration);
        DynamicStringProperty defaultProp = DynamicPropertyFactory.getInstance().getStringProperty("this.prop.does.not.exist.use.default", "default");
        assertEquals("default", defaultProp.get());
        DynamicStringProperty prop1 = DynamicPropertyFactory.getInstance().getStringProperty("prop1", "default");
        assertEquals("value1", prop1.get());
    } finally {
        //Clean up the data files generated by the embedded DB.
        FileUtils.deleteDirectory(new File(".", dbName));
    }
}
Also used : DynamicConfiguration(com.netflix.config.DynamicConfiguration) DynamicStringProperty(com.netflix.config.DynamicStringProperty) FixedDelayPollingScheduler(com.netflix.config.FixedDelayPollingScheduler) File(java.io.File) JDBCConfigurationSource(com.netflix.config.sources.JDBCConfigurationSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 20 with DataSource

use of javax.sql.DataSource in project OpenAttestation by OpenAttestation.

the class CreateDatabase method createTables.

/* 
    private <T extends GenericDAO> void createTable(String name, Class<T> daoClass, DBI dbi) {
        T dao = dbi.open(daoClass);
        if( !Derby.tableExists(name) ) { dao.create(); }        
        dao.close();
        
    }
    */
public void createTables() throws SQLException {
    DataSource ds = Derby.getDataSource();
    DBI dbi = new DBI(ds);
    // tag
    KvAttributeDAO tagDao = dbi.open(KvAttributeDAO.class);
    if (!Derby.tableExists("mw_tag_kvattribute")) {
        tagDao.create();
    }
    tagDao.close();
    // tag value
    //SelectionKvAttributeDAO tagValueDao = dbi.open(SelectionKvAttributeDAO.class);
    //if( !Derby.tableExists("mw_tag_selection_kvattribute") ) { tagValueDao.create(); }
    //tagValueDao.close();
    // certificate request
    CertificateRequestDAO certificateRequestDao = dbi.open(CertificateRequestDAO.class);
    if (!Derby.tableExists("mw_tag_certificate_request")) {
        certificateRequestDao.create();
    }
    certificateRequestDao.close();
    // certificate
    CertificateDAO certificateDao = dbi.open(CertificateDAO.class);
    if (!Derby.tableExists("mw_tag_certificate")) {
        certificateDao.create();
    }
    certificateDao.close();
    // certificate request
    SelectionDAO selectionDao = dbi.open(SelectionDAO.class);
    if (!Derby.tableExists("mw_tag_selection")) {
        selectionDao.create();
    }
    selectionDao.close();
    // configuration
    ConfigurationDAO configurationDao = dbi.open(ConfigurationDAO.class);
    if (!Derby.tableExists("mw_configuration")) {
        configurationDao.create();
    }
    configurationDao.close();
    // file
    FileDAO fileDao = dbi.open(FileDAO.class);
    if (!Derby.tableExists("mw_file")) {
        fileDao.create();
    }
    fileDao.close();
//TpmPasswordDAO tpmPasswordDao = dbi.open(TpmPasswordDAO.class);
//if( !Derby.tableExists("mw_host_tpm_password")) { tpmPasswordDao.create();}
//tpmPasswordDao.close();
}
Also used : DBI(org.skife.jdbi.v2.DBI) 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