Search in sources :

Example 1 with DriverDataSource

use of com.zaxxer.hikari.util.DriverDataSource in project HikariCP by brettwooldridge.

the class JdbcDriverTest method driverTest1.

@Test
public void driverTest1() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
    config.setJdbcUrl("jdbc:stub");
    config.addDataSourceProperty("user", "bart");
    config.addDataSourceProperty("password", "simpson");
    ds = new HikariDataSource(config);
    assertTrue(ds.isWrapperFor(DriverDataSource.class));
    DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
    assertNotNull(unwrap);
    try (Connection connection = ds.getConnection()) {
    // test that getConnection() succeeds
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 2 with DriverDataSource

use of com.zaxxer.hikari.util.DriverDataSource in project HikariCP by brettwooldridge.

the class PoolBase method initializeDataSource.

// ***********************************************************************
//                          Private methods
// ***********************************************************************
/**
    * Create/initialize the underlying DataSource.
    */
private void initializeDataSource() {
    final String jdbcUrl = config.getJdbcUrl();
    final String username = config.getUsername();
    final String password = config.getPassword();
    final String dsClassName = config.getDataSourceClassName();
    final String driverClassName = config.getDriverClassName();
    final String dataSourceJNDI = config.getDataSourceJNDI();
    final Properties dataSourceProperties = config.getDataSourceProperties();
    DataSource dataSource = config.getDataSource();
    if (dsClassName != null && dataSource == null) {
        dataSource = createInstance(dsClassName, DataSource.class);
        PropertyElf.setTargetFromProperties(dataSource, dataSourceProperties);
    } else if (jdbcUrl != null && dataSource == null) {
        dataSource = new DriverDataSource(jdbcUrl, driverClassName, dataSourceProperties, username, password);
    } else if (dataSourceJNDI != null && dataSource == null) {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup(dataSourceJNDI);
        } catch (NamingException e) {
            throw new PoolInitializationException(e);
        }
    }
    if (dataSource != null) {
        setLoginTimeout(dataSource);
        createNetworkTimeoutExecutor(dataSource, dsClassName, jdbcUrl);
    }
    this.dataSource = dataSource;
}
Also used : DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) NamingException(javax.naming.NamingException) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)

Aggregations

DriverDataSource (com.zaxxer.hikari.util.DriverDataSource)2 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)1 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)1 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1