-
-
Save kasramp/7bd3c2d0f7e919ef20087bfee97e37e2 to your computer and use it in GitHub Desktop.
This file contains 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
package com.madadipouya.example.multiple.datasource.song.config | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import org.springframework.boot.jdbc.DataSourceBuilder; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.core.env.Environment; | |
import org.springframework.core.env.Profiles; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
import org.springframework.transaction.PlatformTransactionManager; | |
@Configuration | |
@EnableJpaRepositories(entityManagerFactoryRef = "songEntityManagerFactory", | |
transactionManagerRef = "songTransactionManager", | |
basePackages = {"com.madadipouya.example.multiple.datasource.song"}) | |
public class SongDatasourceConfig { | |
private final Environment environment; | |
public SongDatasourceConfig(Environment environment) { | |
this.environment = environment; | |
} | |
@Bean | |
@Primary | |
public DataSource songDatasource() { | |
return return DataSourceBuilder.create() | |
.url("jdbc:mysql://localhost:3306/ds1?useUnicode=yes&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=UTC") | |
.username("root") | |
.password("secret") | |
.build(); | |
} | |
@Bean("songTransactionManager") | |
@Primary | |
public PlatformTransactionManager songTransactionManager(EntityManagerFactory entityManagerFactory) { | |
JpaTransactionManager transactionManager = new JpaTransactionManager(); | |
transactionManager.setEntityManagerFactory(entityManagerFactory); | |
return transactionManager; | |
} | |
@Bean("songEntityManagerFactory") | |
@Primary | |
public LocalContainerEntityManagerFactoryBean songEntityManagerFactory() { | |
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); | |
entityManager.setDataSource(this.songDatasource()); | |
entityManager.setPackagesToScan("com.madadipouya.example.multiple.datasource.song"); | |
entityManager.setPersistenceUnitName("songDatasource"); | |
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | |
vendorAdapter.setGenerateDdl(true); | |
entityManager.setJpaVendorAdapter(vendorAdapter); | |
if (isArchiveProfile()) { | |
entityManager.setMappingResources("orm.xml"); | |
} | |
return entityManager; | |
} | |
private boolean isArchiveProfile() { | |
return environment.acceptsProfiles(Profiles.of("archive")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment