Last active
December 31, 2019 09:31
-
-
Save ehsaniara/913b40ec7e63ad27cf4bf04a577acb06 to your computer and use it in GitHub Desktop.
DataSourceConfigRead part of https://medium.com/@ehsaniara/spring-boot-2-with-multiple-datasource-for-postgres-data-replication-182c89124f54
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.ehsaniara.multidatasource.configurations; | |
import com.zaxxer.hikari.HikariConfig; | |
import com.zaxxer.hikari.HikariDataSource; | |
import org.hibernate.jpa.HibernatePersistenceProvider; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.persistence.EntityManagerFactory; | |
import static com.ehsaniara.multidatasource.DemoApplication.JPA_PROPERTIES; | |
import static com.ehsaniara.multidatasource.DemoApplication.MODEL_PACKAGE; | |
/** | |
* @author Jay Ehsaniara, Dec 30 2019 | |
*/ | |
@Configuration | |
@ConfigurationProperties("spring.datasource-read") | |
@EnableTransactionManagement | |
@EnableJpaRepositories( | |
entityManagerFactoryRef = "entityManagerFactoryRead", | |
transactionManagerRef = "transactionManagerRead", | |
basePackages = {"com.ehsaniara.multidatasource.repository.readRepository"} | |
) | |
public class DataSourceConfigRead extends HikariConfig { | |
public final static String PERSISTENCE_UNIT_NAME = "read"; | |
@Bean | |
public HikariDataSource dataSourceRead() { | |
return new HikariDataSource(this); | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactoryRead( | |
final HikariDataSource dataSourceRead) { | |
return new LocalContainerEntityManagerFactoryBean() {{ | |
setDataSource(dataSourceRead); | |
setPersistenceProviderClass(HibernatePersistenceProvider.class); | |
setPersistenceUnitName(PERSISTENCE_UNIT_NAME); | |
setPackagesToScan(MODEL_PACKAGE); | |
setJpaProperties(JPA_PROPERTIES); | |
}}; | |
} | |
@Bean | |
public PlatformTransactionManager transactionManagerRead(EntityManagerFactory entityManagerFactoryRead) { | |
return new JpaTransactionManager(entityManagerFactoryRead); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment