Fabio Ebner
Fabio Ebner

Reputation: 2783

how can i use two databases with spring-boot

I trying to use two databases in my project so i follow this link:

this link

but just like example i create one Interface and extend CrudRepository

public interface UsuarioRepository extends CrudRepository<TbUsuario, Long>{}

so where i use?

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
} 

to specify my datasource??

Upvotes: 1

Views: 191

Answers (1)

Alexandre
Alexandre

Reputation: 36

You need to configure two Datasources, two EntityManagerFactories, and two TransactionManagers. Have a look at section 67.7 Use Two EntityManagers in the Spring Boot docs and 4.1.2. Annotation based configuration in the Spring Data JPA docs. You also need to disable datasource auto configuration in the Application class. An example showing how to put all this stuff together can be found here. Good luck!

Upvotes: 1

Related Questions