Skip to content

Instantly share code, notes, and snippets.

@deyvisonborges
Created June 9, 2024 19:05
Show Gist options
  • Save deyvisonborges/b579314408d2cb8c3876adf38d956e1a to your computer and use it in GitHub Desktop.
Save deyvisonborges/b579314408d2cb8c3876adf38d956e1a to your computer and use it in GitHub Desktop.
// package com.mypackage.service.orders.app.persistence;
// import org.springframework.beans.factory.annotation.Qualifier;
// import org.springframework.boot.autoconfigure.domain.EntityScan;
// import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
// import org.springframework.boot.jdbc.DataSourceBuilder;
// import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
// import org.springframework.context.annotation.Primary;
// 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 javax.sql.DataSource;
// @Configuration
// @EntityScan("com.mypackage.service.orders.app.api.module.management.order.persistence.write.entities")
// @EnableJpaRepositories(
// basePackages = "com.mypackage.service.orders.app.api.module.management.order.persistence.write.repositories",
// entityManagerFactoryRef = "writableEntityManagerFactory",
// transactionManagerRef = "writableTransactionManager"
// )
// public class WritableJpaConfig {
// @Primary
// @Bean(name = "writableDataSource")
// public DataSource writableDataSource() {
// return DataSourceBuilder.create()
// // .url("jdbc:postgresql://${postgres.host}:${postgres.port}/${postgres.database}")
// // .username("${postgres.username}")
// // .password("${postgres.password}")
// // .driverClassName("org.postgresql.Driver")
// // .build();
// .url("jdbc:h2:mem:orders_writable;MODE=MYSQL;DATABASE_TO_LOWER=TRUE")
// .driverClassName("org.h2.Driver")
// .build();
// }
// @Primary
// @Bean(name = "writableEntityManagerFactory")
// public LocalContainerEntityManagerFactoryBean writableEntityManagerFactory(
// EntityManagerFactoryBuilder builder,
// @Qualifier("writableDataSource") DataSource dataSource,
// JpaProperties jpaProperties) {
// return builder
// .dataSource(dataSource)
// .packages("com.mypackage.service.orders.app.api.module.management.order.persistence")
// .persistenceUnit("writable")
// .properties(jpaProperties.getProperties())
// .build();
// }
// @Primary
// @Bean(name = "writableTransactionManager")
// public PlatformTransactionManager writableTransactionManager(
// @Qualifier("writableEntityManagerFactory") LocalContainerEntityManagerFactoryBean writableEntityManagerFactory) {
// return new JpaTransactionManager(writableEntityManagerFactory.getObject());
// }
// }
package com.mypackage.service.orders.app.persistence;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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 javax.sql.DataSource;
@Configuration
@EntityScan("com.mypackage.service.orders.app.api.module.management.order.persistence.write.entities")
@EnableJpaRepositories(
basePackages = "com.mypackage.service.orders.app.api.module.management.order.persistence.write.repositories",
entityManagerFactoryRef = "writableEntityManagerFactory",
transactionManagerRef = "writableTransactionManager"
)
public class WritableJpaConfig {
@Primary
@Bean(name = "writableDataSource")
public DataSource writableDataSource() {
return DataSourceBuilder.create()
.url("jdbc:h2:mem:orders_writable;MODE=MYSQL;DATABASE_TO_LOWER=TRUE")
.driverClassName("org.h2.Driver")
.build();
}
@Primary
@Bean(name = "writableEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean writableEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("writableDataSource") DataSource dataSource,
JpaProperties jpaProperties) {
return builder
.dataSource(dataSource)
.packages("com.mypackage.service.orders.app.api.module.management.order.persistence.write.entities")
.persistenceUnit("writable")
.properties(jpaProperties.getProperties())
.build();
}
@Primary
@Bean(name = "writableTransactionManager")
public PlatformTransactionManager writableTransactionManager(
@Qualifier("writableEntityManagerFactory") LocalContainerEntityManagerFactoryBean writableEntityManagerFactory) {
return new JpaTransactionManager(writableEntityManagerFactory.getObject());
}
}
===========
package com.mypackage.service.orders.app.persistence;
// import org.springframework.boot.autoconfigure.domain.EntityScan;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
// import org.springframework.data.redis.connection.RedisConnectionFactory;
// import org.springframework.data.redis.core.RedisTemplate;
// import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
// import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
// import org.springframework.data.redis.serializer.StringRedisSerializer;
// @Configuration
// @EntityScan(basePackages = "com.mypackage.service.orders.app.api.module.management.order.persistence.read.entities")
// @EnableRedisRepositories(basePackages = "com.mypackage.service.orders.app.api.module.management.order.persistence.read.repositories")
// public class RedisConfig {
// @Bean
// RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
// final RedisTemplate<String, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(connectionFactory);
// template.setKeySerializer(new StringRedisSerializer());
// template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// return template;
// }
// }
=====================
// package com.mypackage.service.orders.app.persistence;
// import org.springframework.beans.factory.annotation.Qualifier;
// import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
// import org.springframework.boot.jdbc.DataSourceBuilder;
// import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
// 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 javax.sql.DataSource;
// @Configuration
// @EnableJpaRepositories(
// basePackages = "com.mypackage.service.orders.app.api.module.management.order.persistence.read",
// entityManagerFactoryRef = "readableEntityManagerFactory",
// transactionManagerRef = "readableTransactionManager"
// )
// public class ReadableJpaConfig {
// @Bean(name = "readableDataSource")
// public DataSource readableDataSource() {
// return DataSourceBuilder.create()
// .url("jdbc:h2:mem:orders_readable;MODE=MYSQL;DATABASE_TO_LOWER=TRUE")
// .driverClassName("org.h2.Driver")
// .build();
// }
// @Bean(name = "readableEntityManagerFactory")
// public LocalContainerEntityManagerFactoryBean readableEntityManagerFactory(
// EntityManagerFactoryBuilder builder,
// @Qualifier("readableDataSource") DataSource dataSource,
// JpaProperties jpaProperties
// ) {
// return builder
// .dataSource(dataSource)
// .packages("com.mypackage.service.orders.app.api.module.management.order.persistence")
// .persistenceUnit("readable")
// .properties(jpaProperties.getProperties())
// .build();
// }
// @Bean(name = "readableTransactionManager")
// public PlatformTransactionManager readableTransactionManager(
// @Qualifier("readableEntityManagerFactory") LocalContainerEntityManagerFactoryBean readableEntityManagerFactory) {
// return new JpaTransactionManager(readableEntityManagerFactory.getObject());
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment