Não consegui fazer a integração com os dois bancos de dados! Ele sobe a aplicação porem não consigo utilizar o banco de dados localmente nem com o heroku
EDIT Verifiquei que o erro ocorre quando eu coloco o @Profile no Datasource e Properties
JPAConfiguration
@EnableTransactionManagement
public class JPAConfiguration{
/**
* DataSource definition for database connection. Settings are read from
* the application.properties file (using the env object).
*/
@Bean
@Profile("dev")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("db.driver");
dataSource.setUrl("db.url");
dataSource.setUsername("db.username");
dataSource.setPassword("db.password");
return dataSource;
}
@Bean
@Profile("dev")
public Properties additionalProperties() {
Properties additionalProperties = new Properties();
additionalProperties.put( "hibernate.dialect", "hibernate.dialect");
additionalProperties.put("hibernate.show_sql", "hibernate.show_sql");
additionalProperties.put("hibernate.hbm2ddl.auto", "hibernate.hbm2ddl.auto");
return additionalProperties;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource datasource, Properties additionalProperties) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("entitymanager.packagesToScan");
factoryBean.setDataSource(datasource);
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setJpaProperties(additionalProperties);
return factoryBean;
}
/**
* Declare the transaction manager.
*/
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return transactionManager;
}
/**
* PersistenceExceptionTranslationPostProcessor is a bean post processor
* which adds an advisor to any bean annotated with Repository so that any
* platform-specific exceptions are caught and then rethrown as one
* Spring's unchecked data access exceptions (i.e. a subclass of
* DataAccessException).
*/
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;
JPAProductionConfiguration
@Profile("prod")
public class JPAProductionConfiguration {
@Autowired
private Environment environment;
@Bean
public Properties additionalProperties() {
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
props.setProperty("hibernate.show_sql", "true");
props.setProperty("hibernate.hbm2ddl.auto", "update");
return props;
}
@Bean
public DataSource dataSource() throws URISyntaxException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
// usuario:senha@host:port/path
URI dbUrl = new URI(environment.getProperty("DATABASE_URL"));
dataSource.setUrl("jdbc:postgresql://"+dbUrl.getHost()
+":"+dbUrl.getPort()+dbUrl.getPath());
dataSource.setUsername(dbUrl.getUserInfo().split(":")[0]);
dataSource.setPassword(dbUrl.getUserInfo().split(":")[1]);
return dataSource;
}
}