Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. spring.primary.url=jdbc:postgresql://localhost:5432/kode12
  2. spring.primary.username=postgres
  3. spring.primary.password=root
  4. spring.primary.driverClassName=org.postgresql.Driver
  5.  
  6. spring.secondary.url=jdbc:mysql://localhost:3306/kode12
  7. spring.secondary.username=root
  8. spring.secondary.password=root
  9. spring.secondary.driverClassName=com.mysql.jdbc.Driver
  10.  
  11. package com.test;
  12.  
  13. import org.springframework.boot.SpringApplication;
  14. import org.springframework.boot.autoconfigure.SpringBootApplication;
  15.  
  16. @SpringBootApplication
  17. public Application {
  18. public static void main(String[] args) {
  19. SpringApplication.run(Application.class, args);
  20. }
  21. }
  22.  
  23. package com.test.config;
  24. import javax.sql.DataSource;
  25. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  26. import org.springframework.boot.context.properties.ConfigurationProperties;
  27. import org.springframework.context.annotation.Bean;
  28. import org.springframework.context.annotation.Configuration;
  29. import org.springframework.context.annotation.Primary;
  30.  
  31. @Configuration
  32. public class ApplicationConfiguration {
  33.  
  34. @Primary
  35. @Bean(name = "primaryDB")
  36. @ConfigurationProperties(prefix = "spring.primary")
  37. public DataSource postgresDataSource() {
  38. return DataSourceBuilder.create().build();
  39. }
  40.  
  41. @Bean(name = "primaryEM")
  42. public LocalContainerEntityManagerFactoryBean storingEntityManagerFactory(
  43. EntityManagerFactoryBuilder builder, @Qualifier("primaryDB") DataSource ds) {
  44. return builder
  45. .dataSource(ds)
  46. .packages("com.test")
  47. .persistenceUnit("primaryPU")
  48. .build();
  49. }
  50.  
  51. @Bean(name = "secondaryDB")
  52. @ConfigurationProperties(prefix = "spring.secondary")
  53. public DataSource mysqlDataSource() {
  54. return DataSourceBuilder.create().build();
  55. }
  56.  
  57. @Bean(name = "secondaryEM")
  58. public LocalContainerEntityManagerFactoryBean storingEntityManagerFactory(
  59. EntityManagerFactoryBuilder builder, @Qualifier("secondaryDB") DataSource ds) {
  60. return builder
  61. .dataSource(ds)
  62. .packages("com.test")
  63. .persistenceUnit("secondaryPU")
  64. .build();
  65. }
  66.  
  67. }
  68.  
  69. public abstract class GenericDAO<T extends Serializable> {
  70.  
  71. private Class<T> clazz = null;
  72.  
  73. @PersistenceContext
  74. protected EntityManager entityManager;
  75.  
  76. public void setClazz(Class<T> clazzToSet) {
  77. this.clazz = clazzToSet;
  78. }
  79.  
  80. public T findOne(Integer id) {
  81. return this.entityManager.find(this.clazz, id);
  82. }
  83.  
  84. public List<T> findAll() {
  85. return this.entityManager.createQuery("from " + this.clazz.getName()).getResultList();
  86. }
  87.  
  88. @Transactional
  89. public void save(T entity) {
  90. this.entityManager.persist(setModifiedAt(entity));
  91. }
  92. }
  93.  
  94. @Repository
  95. public class PersonDAO extends GenericDAO<Person> {
  96. public PersonDAO() {
  97. this.setClazz(Person.class);
  98. }
  99. }
  100.  
  101. @Repository
  102. public class ProductDAO extends GenericDAO<Product> {
  103. public ProductDAO() {
  104. this.setClazz(Product.class);
  105. }
  106. }
  107.  
  108. @Service
  109. public class TestService {
  110.  
  111. @Autowired
  112. @PersistenceContext(name = "primaryEm")
  113. PersonDAO personDao;
  114.  
  115. @Autowired
  116. @PersistenceContext(name = "secondaryEm")
  117. ProductDAO productDao;
  118.  
  119. // This should write to primary datasource
  120. public void savePerson(Person person) {
  121. personDao.save(person);
  122. }
  123.  
  124. // This should write to secondary datasource
  125. public void saveProduct(Product product) {
  126. productDao.save(product);
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement