Last active
August 29, 2015 14:25
-
-
Save altfatterz/1485ed9f33b0d65a19f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class DatabaseConfig { | |
protected void configureDataSource(org.apache.tomcat.jdbc.pool.DataSource dataSource) { | |
dataSource.setMaxActive(20); | |
dataSource.setMaxIdle(8); | |
dataSource.setMinIdle(8); | |
dataSource.setTestOnBorrow(false); | |
dataSource.setTestOnReturn(false); | |
dataSource.setValidationQuery("SELECT 1"); | |
} | |
} | |
@Configuration | |
@Profile("dev") | |
class DevDatabaseConfig extends DatabaseConfig { | |
@Bean | |
public DataSource dataSource() { | |
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); | |
dataSource.setDriverClassName("org.h2.Driver"); | |
dataSource.setUrl("jdbc:h2:mem:myDB;MODE=PostgreSQL"); | |
dataSource.setUsername("sa"); | |
dataSource.setPassword(""); | |
configureDataSource(dataSource); | |
return dataSource; | |
} | |
} | |
@Configuration | |
@Profile("heroku") | |
class HerokuDatabaseConfig extends DatabaseConfig { | |
@Value("${spring.datasource.uri}") | |
private String databaseUri; | |
@Bean | |
public DataSource dataSource() throws URISyntaxException { | |
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); | |
URI dbUri = new URI(databaseUri); | |
dataSource.setUsername(dbUri.getUserInfo().split(":")[0]); | |
dataSource.setPassword(dbUri.getUserInfo().split(":")[1]); | |
dataSource.setUrl("jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath()); | |
configureDataSource(dataSource); | |
return dataSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment