List of usage examples for org.apache.commons.dbcp2 BasicDataSource setDriverClassLoader
public synchronized void setDriverClassLoader(ClassLoader driverClassLoader)
Sets the class loader to be used to load the JDBC driver.
Note: this method currently has no effect once the pool has been initialized.
From source file:org.apache.druid.metadata.storage.derby.DerbyConnector.java
@Inject public DerbyConnector(MetadataStorage storage, Supplier<MetadataStorageConnectorConfig> config, Supplier<MetadataStorageTablesConfig> dbTables) { super(config, dbTables); final BasicDataSource datasource = getDatasource(); datasource.setDriverClassLoader(getClass().getClassLoader()); datasource.setDriverClassName("org.apache.derby.jdbc.ClientDriver"); this.dbi = new DBI(datasource); this.storage = storage; log.info("Derby connector instantiated with metadata storage [%s].", this.storage.getClass().getName()); }
From source file:org.apache.druid.metadata.storage.mysql.MySQLConnector.java
@Inject public MySQLConnector(Supplier<MetadataStorageConnectorConfig> config, Supplier<MetadataStorageTablesConfig> dbTables, MySQLConnectorConfig connectorConfig) { super(config, dbTables); final BasicDataSource datasource = getDatasource(); // MySQL driver is classloader isolated as part of the extension // so we need to help JDBC find the driver datasource.setDriverClassLoader(getClass().getClassLoader()); datasource.setDriverClassName("com.mysql.jdbc.Driver"); datasource.addConnectionProperty("useSSL", String.valueOf(connectorConfig.isUseSSL())); if (connectorConfig.isUseSSL()) { log.info("SSL is enabled on this MySQL connection. "); datasource.addConnectionProperty("verifyServerCertificate", String.valueOf(connectorConfig.isVerifyServerCertificate())); if (connectorConfig.isVerifyServerCertificate()) { log.info("Server certificate verification is enabled. "); if (connectorConfig.getTrustCertificateKeyStoreUrl() != null) { datasource.addConnectionProperty("trustCertificateKeyStoreUrl", new File(connectorConfig.getTrustCertificateKeyStoreUrl()).toURI().toString()); }/* ww w .ja v a2 s . com*/ if (connectorConfig.getTrustCertificateKeyStoreType() != null) { datasource.addConnectionProperty("trustCertificateKeyStoreType", connectorConfig.getTrustCertificateKeyStoreType()); } if (connectorConfig.getTrustCertificateKeyStorePassword() == null) { log.warn( "Trust store password is empty. Ensure that the trust store has been configured with an empty password."); } else { datasource.addConnectionProperty("trustCertificateKeyStorePassword", connectorConfig.getTrustCertificateKeyStorePassword()); } } if (connectorConfig.getClientCertificateKeyStoreUrl() != null) { datasource.addConnectionProperty("clientCertificateKeyStoreUrl", new File(connectorConfig.getClientCertificateKeyStoreUrl()).toURI().toString()); } if (connectorConfig.getClientCertificateKeyStoreType() != null) { datasource.addConnectionProperty("clientCertificateKeyStoreType", connectorConfig.getClientCertificateKeyStoreType()); } if (connectorConfig.getClientCertificateKeyStorePassword() != null) { datasource.addConnectionProperty("clientCertificateKeyStorePassword", connectorConfig.getClientCertificateKeyStorePassword()); } Joiner joiner = Joiner.on(",").skipNulls(); if (connectorConfig.getEnabledSSLCipherSuites() != null) { datasource.addConnectionProperty("enabledSSLCipherSuites", joiner.join(connectorConfig.getEnabledSSLCipherSuites())); } if (connectorConfig.getEnabledTLSProtocols() != null) { datasource.addConnectionProperty("enabledTLSProtocols", joiner.join(connectorConfig.getEnabledTLSProtocols())); } } // use double-quotes for quoting columns, so we can write SQL that works with most databases datasource.setConnectionInitSqls(ImmutableList.of("SET sql_mode='ANSI_QUOTES'")); this.dbi = new DBI(datasource); log.info("Configured MySQL as metadata storage"); }
From source file:org.apache.druid.metadata.storage.postgresql.PostgreSQLConnector.java
@Inject public PostgreSQLConnector(Supplier<MetadataStorageConnectorConfig> config, Supplier<MetadataStorageTablesConfig> dbTables, PostgreSQLConnectorConfig connectorConfig) { super(config, dbTables); final BasicDataSource datasource = getDatasource(); // PostgreSQL driver is classloader isolated as part of the extension // so we need to help JDBC find the driver datasource.setDriverClassLoader(getClass().getClassLoader()); datasource.setDriverClassName("org.postgresql.Driver"); // SSL Configuration if (connectorConfig.isUseSSL()) { log.info("SSL is enabled on this PostgreSQL connection."); datasource.addConnectionProperty(PGProperty.SSL.getName(), String.valueOf(connectorConfig.isUseSSL())); if (connectorConfig.getPassword() != null) { datasource.addConnectionProperty(PGProperty.SSL_PASSWORD.getName(), connectorConfig.getPassword()); }/*from w w w. j a va2 s .com*/ if (connectorConfig.getSslFactory() != null) { datasource.addConnectionProperty(PGProperty.SSL_FACTORY.getName(), connectorConfig.getSslFactory()); } if (connectorConfig.getSslFactoryArg() != null) { datasource.addConnectionProperty(PGProperty.SSL_FACTORY_ARG.getName(), connectorConfig.getSslFactoryArg()); } if (connectorConfig.getSslMode() != null) { datasource.addConnectionProperty(PGProperty.SSL_MODE.getName(), connectorConfig.getSslMode()); } if (connectorConfig.getSslCert() != null) { datasource.addConnectionProperty(PGProperty.SSL_CERT.getName(), connectorConfig.getSslCert()); } if (connectorConfig.getSslKey() != null) { datasource.addConnectionProperty(PGProperty.SSL_KEY.getName(), connectorConfig.getSslKey()); } if (connectorConfig.getSslRootCert() != null) { datasource.addConnectionProperty(PGProperty.SSL_ROOT_CERT.getName(), connectorConfig.getSslRootCert()); } if (connectorConfig.getSslHostNameVerifier() != null) { datasource.addConnectionProperty(PGProperty.SSL_HOSTNAME_VERIFIER.getName(), connectorConfig.getSslHostNameVerifier()); } if (connectorConfig.getSslPasswordCallback() != null) { datasource.addConnectionProperty(PGProperty.SSL_PASSWORD_CALLBACK.getName(), connectorConfig.getSslPasswordCallback()); } } this.dbi = new DBI(datasource); log.info("Configured PostgreSQL as metadata storage"); }
From source file:org.lib4j.dbcp.DataSources.java
/** * Create a <code>BasicDataSource</code> given a dbcp JAXB binding. * * @param dbcp JAXB dbcp binding.//from w w w . j a v a 2 s .co m * @param driverClassLoader Class loader to be used to load the JDBC driver. * @return the <code>BasicDataSource</code> instance. * @throws SQLException If a database access error occurs. */ public static BasicDataSource createDataSource(final Dbcp dbcp, final ClassLoader driverClassLoader) throws SQLException { final BasicDataSource dataSource = new BasicDataSource(); final Dbcp.Jdbc jdbc = dbcp.getJdbc(); dataSource.setDriverClassName(jdbc.getDriverClassName()); dataSource.setDriverClassLoader(driverClassLoader); dataSource.setUrl(jdbc.getUrl()); dataSource.setUsername(jdbc.getUsername()); dataSource.setPassword(jdbc.getPassword()); final Dbcp.Default _default = dbcp.getDefault(); if (_default != null && _default.getCatalog() != null) dataSource.setDefaultCatalog(_default.getCatalog()); dataSource.setDefaultAutoCommit( _default == null || _default.getAutoCommit() == null || _default.getAutoCommit()); dataSource.setDefaultReadOnly(_default != null && _default.getReadOnly() != null && _default.getReadOnly()); if (_default != null && _default.getQueryTimeout() != null) dataSource.setDefaultQueryTimeout(_default.getQueryTimeout()); if (_default != null && _default.getTransactionIsolation() != null) { if ("NONE".equals(_default.getTransactionIsolation())) dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE); else if ("READ_COMMITTED".equals(_default.getTransactionIsolation())) dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); else if ("READ_UNCOMMITTED".equals(_default.getTransactionIsolation())) dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); else if ("REPEATABLE_READ".equals(_default.getTransactionIsolation())) dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); else if ("SERIALIZABLE".equals(_default.getTransactionIsolation())) dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); else throw new UnsupportedOperationException( "Unsupported transaction isolation: " + _default.getTransactionIsolation()); } final Dbcp.Connection connection = dbcp.getConnection(); if (connection != null) { if (connection.getProperties() != null) for (final Dbcp.Connection.Properties.Property property : connection.getProperties().getProperty()) if (property.getName() != null && property.getValue() != null) dataSource.addConnectionProperty(property.getName(), property.getValue()); if (connection.getInitSqls() != null) { final List<String> initSqls = new ArrayList<>(); for (final String initSql : connection.getInitSqls().getInitSql()) initSqls.add(initSql); dataSource.setConnectionInitSqls(initSqls); } } final Dbcp.Size size = dbcp.getSize(); dataSource.setInitialSize(size == null || size.getInitialSize() == null ? 0 : size.getInitialSize()); dataSource.setMaxTotal(size == null || size.getMaxTotal() == null ? 8 : INDEFINITE.equals(size.getMaxTotal()) ? -1 : Integer.parseInt(size.getMaxTotal())); dataSource.setMaxIdle(size == null || size.getMaxIdle() == null ? 8 : INDEFINITE.equals(size.getMaxIdle()) ? -1 : Integer.parseInt(size.getMaxIdle())); dataSource.setMinIdle(size == null || size.getMinIdle() == null ? 9 : size.getMinIdle()); if (size == null || size.getMaxOpenPreparedStatements() == null || INDEFINITE.equals(size.getMaxOpenPreparedStatements())) { dataSource.setPoolPreparedStatements(false); } else { dataSource.setPoolPreparedStatements(true); dataSource.setMaxOpenPreparedStatements(Integer.parseInt(size.getMaxOpenPreparedStatements())); } final Dbcp.Pool pool = dbcp.getPool(); if (pool == null || pool.getQueue() == null || "lifo".equals(pool.getQueue())) dataSource.setLifo(true); else if ("fifo".equals(pool.getQueue())) dataSource.setLifo(false); else throw new UnsupportedOperationException("Unsupported queue spec: " + pool.getQueue()); dataSource.setCacheState(pool != null && pool.getCacheState() != null && pool.getCacheState()); dataSource.setMaxWaitMillis( pool == null || pool.getMaxWait() != null || INDEFINITE.equals(pool.getMaxWait()) ? -1 : Long.parseLong(pool.getMaxWait())); dataSource.setMaxConnLifetimeMillis(pool == null || pool.getMaxConnectionLifetime() == null || INDEFINITE.equals(pool.getMaxConnectionLifetime()) ? 0 : Long.parseLong(pool.getMaxConnectionLifetime())); dataSource.setEnableAutoCommitOnReturn(_default == null || pool.getEnableAutoCommitOnReturn() == null || pool.getEnableAutoCommitOnReturn()); dataSource.setRollbackOnReturn( pool == null || pool.getRollbackOnReturn() == null || pool.getRollbackOnReturn()); if (pool != null && pool.getRemoveAbandoned() != null) { if ("borrow".equals(pool.getRemoveAbandoned().getOn())) dataSource.setRemoveAbandonedOnBorrow(true); else if ("maintenance".equals(pool.getRemoveAbandoned().getOn())) dataSource.setRemoveAbandonedOnMaintenance(true); else throw new UnsupportedOperationException( "Unsupported remove abandoned spec: " + pool.getRemoveAbandoned().getOn()); dataSource.setRemoveAbandonedTimeout(pool.getRemoveAbandoned().getTimeout()); } dataSource.setAbandonedUsageTracking( pool != null && pool.getAbandonedUsageTracking() != null && pool.getAbandonedUsageTracking()); dataSource.setAccessToUnderlyingConnectionAllowed( pool != null && pool.getAllowAccessToUnderlyingConnection() != null && pool.getAllowAccessToUnderlyingConnection()); final Dbcp.Pool.Eviction evictor = pool != null && pool.getEviction() != null ? pool.getEviction() : null; if (evictor != null) { dataSource.setTimeBetweenEvictionRunsMillis(evictor.getTimeBetweenRuns()); dataSource.setNumTestsPerEvictionRun(evictor.getNumTestsPerRun()); dataSource.setMinEvictableIdleTimeMillis( evictor.getMinIdleTime() == null ? 1800000 : evictor.getMinIdleTime()); dataSource.setSoftMinEvictableIdleTimeMillis( evictor.getSoftMinIdleTime() == null || INDEFINITE.equals(evictor.getSoftMinIdleTime()) ? -1 : Long.parseLong(evictor.getSoftMinIdleTime())); if (evictor.getPolicyClassName() != null) dataSource.setEvictionPolicyClassName(evictor.getPolicyClassName()); } final Dbcp.Validation validation = dbcp.getValidation(); if (validation != null && validation.getQuery() != null) dataSource.setValidationQuery(validation.getQuery()); dataSource.setTestOnBorrow( validation == null || validation.getTestOnBorrow() == null || validation.getTestOnBorrow()); dataSource.setTestOnReturn( validation != null && validation.getTestOnReturn() != null && validation.getTestOnReturn()); dataSource.setTestWhileIdle( validation != null && validation.getTestWhileIdle() != null && validation.getTestWhileIdle()); if (validation != null && validation.getFastFail() != null) { dataSource.setFastFailValidation(true); if (validation.getFastFail().getDisconnectionSqlCodes() != null) dataSource.setDisconnectionSqlCodes( Arrays.asList(validation.getFastFail().getDisconnectionSqlCodes().split(" "))); } final Dbcp.Logging logging = dbcp.getLogging(); if (logging != null) { final Logger logger = LoggerFactory.getLogger(DataSources.class); final LoggerPrintWriter loggerPrintWriter = new LoggerPrintWriter(logger, Level.valueOf(logging.getLevel().toString())); dataSource.setLogWriter(loggerPrintWriter); dataSource.setLogExpiredConnections(logging.isLogExpiredConnections()); if (logging.isLogAbandoned()) { dataSource.setAbandonedLogWriter(loggerPrintWriter); dataSource.setLogAbandoned(true); } } return dataSource; }
From source file:org.ofbiz.core.entity.transaction.DBCPConnectionFactory.java
private static BasicDataSource createDataSource(JdbcDatasourceInfo jdbcDatasource) throws Exception { final Properties dbcpProperties = loadDbcpProperties(); final BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(dbcpProperties); dataSource.setDriverClassLoader(Thread.currentThread().getContextClassLoader()); dataSource.setDriverClassName(jdbcDatasource.getDriverClassName()); dataSource.setUrl(jdbcDatasource.getUri()); dataSource.setUsername(jdbcDatasource.getUsername()); dataSource.setPassword(jdbcDatasource.getPassword()); if (isNotEmpty(jdbcDatasource.getIsolationLevel())) { dataSource.setDefaultTransactionIsolation( TransactionIsolations.fromString(jdbcDatasource.getIsolationLevel())); }//www . j a va 2s . co m if (dbcpProperties.containsKey(PROP_JMX) && Boolean.valueOf(dbcpProperties.getProperty(PROP_JMX))) { dataSource.setJmxName( ObjectName.getInstance(dbcpProperties.getProperty(PROP_MBEANNAME)).getCanonicalName()); } return dataSource; }