@Bean(destroyMethod = "close") @ConditionalOnExpression( "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}") public DataSource dataSource( DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) { log.debug("Configuring Datasource"); if (dataSourceProperties.getUrl() == null) { log.error( "Your database connection pool configuration is incorrect! The application" + " cannot start. Please check your Spring profile, current profiles are: {}", Arrays.toString(env.getActiveProfiles())); throw new ApplicationContextException("Database connection pool is not configured correctly"); } HikariConfig config = new HikariConfig(); config.setDataSourceClassName(dataSourceProperties.getDriverClassName()); config.addDataSourceProperty("url", dataSourceProperties.getUrl()); if (dataSourceProperties.getUsername() != null) { config.addDataSourceProperty("user", dataSourceProperties.getUsername()); } else { config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user } if (dataSourceProperties.getPassword() != null) { config.addDataSourceProperty("password", dataSourceProperties.getPassword()); } else { config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password } if (metricRegistry != null) { config.setMetricRegistry(metricRegistry); } return new HikariDataSource(config); }
@Test public void determineUsername() throws Exception { DataSourceProperties properties = new DataSourceProperties(); properties.afterPropertiesSet(); assertThat(properties.getUsername()).isNull(); assertThat(properties.determineUsername()).isEqualTo("sa"); }
@Test public void determineUsernameWithExplicitConfig() throws Exception { DataSourceProperties properties = new DataSourceProperties(); properties.setUsername("foo"); properties.afterPropertiesSet(); assertThat(properties.getUsername()).isEqualTo("foo"); assertThat(properties.determineUsername()).isEqualTo("foo"); }
@Bean(destroyMethod = "close") @ConditionalOnExpression( "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}") public DataSource dataSource( DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) { log.debug("Configuring Datasource"); if (dataSourceProperties.getUrl() == null) { log.error( "Your database connection pool configuration is incorrect! The application" + " cannot start. Please check your Spring profile, current profiles are: {}", Arrays.toString(env.getActiveProfiles())); throw new ApplicationContextException("Database connection pool is not configured correctly"); } HikariConfig config = new HikariConfig(); config.setDataSourceClassName(dataSourceProperties.getDriverClassName()); config.addDataSourceProperty("url", dataSourceProperties.getUrl()); if (dataSourceProperties.getUsername() != null) { config.addDataSourceProperty("user", dataSourceProperties.getUsername()); } else { config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user } if (dataSourceProperties.getPassword() != null) { config.addDataSourceProperty("password", dataSourceProperties.getPassword()); } else { config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password } // MySQL optimizations, see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource" .equals(dataSourceProperties.getDriverClassName())) { config.addDataSourceProperty( "cachePrepStmts", jHipsterProperties.getDatasource().isCachePrepStmts()); config.addDataSourceProperty( "prepStmtCacheSize", jHipsterProperties.getDatasource().getPrepStmtCacheSize()); config.addDataSourceProperty( "prepStmtCacheSqlLimit", jHipsterProperties.getDatasource().getPrepStmtCacheSqlLimit()); } if (metricRegistry != null) { config.setMetricRegistry(metricRegistry); } return new HikariDataSource(config); }
/* * (non-javadoc) * * Use Tomcat 7 DBCP because GAE restricts usage on * java.lang.management.ManagementFactory * */ @Bean public DataSource datasource() { BasicDataSource datasource = new BasicDataSource(); datasource.setDriverClassName(properties.getDriverClassName()); datasource.setUrl(properties.getUrl()); datasource.setUsername(properties.getUsername()); datasource.setPassword(properties.getPassword()); datasource.setMaxActive(12); return datasource; }
/** * Initialize OracleDataSource manually because datasource property * spring.datasource.type=oracle.jdbc.pool.OracleDataSource is not correctly handled by spring * https://github.com/spring-projects/spring-boot/issues/6027#issuecomment-221582708 * * @param properties * @return * @throws SQLException */ @Bean public DataSource dataSource(final DataSourceProperties properties) throws SQLException { final PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource(); dataSource.setUser(properties.getUsername()); dataSource.setPassword(properties.getPassword()); dataSource.setURL(properties.getUrl()); dataSource.setFastConnectionFailoverEnabled(true); dataSource.setMaxPoolSize(20); dataSource.setMinPoolSize(5); dataSource.setMaxIdleTime(5); dataSource.setValidateConnectionOnBorrow(true); dataSource.setMaxStatements(10); dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); return dataSource; }