@Override public void validate(UIValidationContext validator) { final DatabaseType database = dbType.getValue(); final DatabaseDriver driver = DatabaseDriver.fromProductName(database.name()); if (driver.equals(DatabaseDriver.UNKNOWN)) { // Spring Boot doesn't know about this DB validator.addValidationError(dbType, "Spring Boot doesn't know about DB '" + dbType.getName() + "'"); } if (isNotEmbeddedDB(database) && !databaseURL.hasValue() && !dataSourceName.hasValue()) { validator.addValidationError(dataSourceName, "Either DataSource name or database URL is required"); validator.addValidationError(databaseURL, "Either DataSource name or database URL is required"); } }
private XADataSource createXaDataSource(DataSourceProperties properties) throws ClassNotFoundException, LinkageError { String className = Optional .ofNullable(properties.getXa()) .map(Xa::getDataSourceClassName) .map(String::trim) .map(Strings::emptyToNull) .orElseGet(() -> DatabaseDriver .fromJdbcUrl(properties.determineUrl()) .getXaDataSourceClassName()); Assert.state(StringUtils.hasLength(className), "No XA DataSource class name specified or inferred"); XADataSource dataSource = createXaDataSourceInstance(className); bindXaProperties(dataSource, properties); return dataSource; }
protected org.apache.tomcat.jdbc.pool.DataSource createTomcatDataSource(DataSourceProperties properties) { org.apache.tomcat.jdbc.pool.DataSource dataSource = createDataSource( properties, org.apache.tomcat.jdbc.pool.DataSource.class); DatabaseDriver databaseDriver = DatabaseDriver .fromJdbcUrl(properties.determineUrl()); String validationQuery = databaseDriver.getValidationQuery(); if (validationQuery != null) { dataSource.setTestOnBorrow(true); dataSource.setValidationQuery(validationQuery); } return dataSource; }
protected String getValidationQuery(String product) { String query = this.query; if (!StringUtils.hasText(query)) { DatabaseDriver specific = DatabaseDriver.fromProductName(product); query = specific.getValidationQuery(); } if (!StringUtils.hasText(query)) { query = DEFAULT_QUERY; } return query; }
private XADataSource createXaDataSource() { String className = this.properties.getXa().getDataSourceClassName(); if (!StringUtils.hasLength(className)) { className = DatabaseDriver.fromJdbcUrl(this.properties.determineUrl()) .getXaDataSourceClassName(); } Assert.state(StringUtils.hasLength(className), "No XA DataSource class name specified"); XADataSource dataSource = createXaDataSourceInstance(className); bindXaProperties(dataSource, this.properties); return dataSource; }
private void maybeGetDriverClassName() { if (!this.properties.containsKey("driverClassName") && this.properties.containsKey("url")) { String url = this.properties.get("url"); String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName(); this.properties.put("driverClassName", driverClass); } }
@Test public void tomcatValidatesConnectionByDefault() { org.apache.tomcat.jdbc.pool.DataSource dataSource = autoConfigureDataSource( org.apache.tomcat.jdbc.pool.DataSource.class); assertThat(dataSource.isTestOnBorrow()).isTrue(); assertThat(dataSource.getValidationQuery()) .isEqualTo(DatabaseDriver.HSQLDB.getValidationQuery()); }
@Test public void commonsDbcpValidatesConnectionByDefault() { BasicDataSource dataSource = autoConfigureDataSource(BasicDataSource.class, "org.apache.tomcat", "com.zaxxer.hikari"); assertThat(dataSource.getTestOnBorrow()).isTrue(); assertThat(dataSource.getValidationQuery()) .isEqualTo(DatabaseDriver.HSQLDB.getValidationQuery()); }
private String driverClassName() { if (StringUtils.hasText(driverClassName)) { return driverClassName; } return DatabaseDriver.fromJdbcUrl(url).getDriverClassName(); }
private String validationQuery() { if (StringUtils.hasText(validationQuery)) { return validationQuery; } return DatabaseDriver.fromJdbcUrl(url).getValidationQuery(); }