/** * Initialize all required jdbc elements and make the reader ready for iteration. * * Possible configuration keys : * <ol> * <li>JDBCRecordReader.TRIM_STRINGS : Whether or not read strings should be trimmed before being returned. False by default</li> * <li>JDBCRecordReader.JDBC_URL : Jdbc url to use for datastource configuration (see JDBCRecordReaderTest for examples)</li> * <li>JDBCRecordReader.JDBC_DRIVER_CLASS_NAME : Driver class to use for datasource configuration</li> * <li>JDBCRecordReader.JDBC_USERNAME && JDBC_PASSWORD : Username and password to use for datasource configuration</li> * <li>JDBCRecordReader.JDBC_RESULTSET_TYPE : ResultSet type to use (int value defined in jdbc doc)</li> * </ol> * * Url and driver class name are not mandatory. If one of them is specified, the other must be specified as well. If * they are set and there already is a DataSource set in the reader, it will be discarded and replaced with the * newly created one. * * @param conf a configuration for initialization * @param split not handled yet, will be discarded */ @Override public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException { this.setConf(conf); this.setTrimStrings(conf.getBoolean(TRIM_STRINGS, trimStrings)); this.setResultSetType(conf.getInt(JDBC_RESULTSET_TYPE, resultSetType)); String jdbcUrl = conf.get(JDBC_URL); String driverClassName = conf.get(JDBC_DRIVER_CLASS_NAME); // url and driver must be both unset or both present if (jdbcUrl == null ^ driverClassName == null) { throw new IllegalArgumentException( "Both jdbc url and driver class name must be provided in order to configure JDBCRecordReader's datasource"); } // Both set, initialiaze the datasource else if (jdbcUrl != null) { // FIXME : find a way to read wildcard properties from conf in order to fill the third argument bellow this.dataSource = new DriverDataSource(jdbcUrl, driverClassName, new Properties(), conf.get(JDBC_USERNAME), conf.get(JDBC_PASSWORD)); } this.initializeJdbc(); }
/** * Shutdown the DataSource and its associated pool. */ @Override public void close() { if (isShutdown.getAndSet(true)) { return; } if (pool != null) { try { pool.shutdown(); } catch (InterruptedException e) { LOGGER.warn("Interrupted during closing", e); } if (pool.getDataSource() instanceof DriverDataSource) { ((DriverDataSource) pool.getDataSource()).shutdown(); } } }
@Test public void driverTest1() throws SQLException { HikariConfig config = new HikariConfig(); 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); Assert.assertTrue(ds.isWrapperFor(DriverDataSource.class)); DriverDataSource unwrap = ds.unwrap(DriverDataSource.class); Assert.assertNotNull(unwrap); Connection connection = ds.getConnection(); connection.close(); }
@Test public void testDerbyDriverDataSource() throws SQLException { BQRuntime runtime = testFactory.app("-c", "classpath:HikariCPDerbyIT.yml") .autoLoadModules() .createRuntime(); DataSource ds5 = runtime.getInstance(DataSourceFactory.class).forName("derby5"); assertNotNull(ds5); assertTrue(ds5 instanceof HikariDataSource); HikariDataSource hikariDS = (HikariDataSource) ds5; assertEquals("org.apache.derby.jdbc.EmbeddedDriver", hikariDS.getDriverClassName()); HikariPool pool = (HikariPool) hikariDS.getHikariPoolMXBean(); assertTrue(pool.getUnwrappedDataSource() instanceof DriverDataSource); try (Connection c = hikariDS.getConnection()) { assertEquals("jdbc:derby:", c.getMetaData().getURL()); } }