Created
March 17, 2020 12:50
-
-
Save appkr/898e44fe753fc525c639dc4a4b28150b to your computer and use it in GitHub Desktop.
Spring Database Connection 분리
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
spring: | |
datasource: | |
hikari: | |
master: | |
jdbc-url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC | |
username: root | |
password: root | |
poolName: Hikari | |
maximum-pool-size: 2 | |
auto-commit: false | |
data-source-properties: | |
cachePrepStmts: true | |
prepStmtCacheSize: 250 | |
prepStmtCacheSqlLimit: 2048 | |
useServerPrepStmts: true | |
slave: | |
jdbc-url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC | |
username: root | |
password: root | |
poolName: Hikari | |
maximum-pool-size: 2 | |
auto-commit: false | |
data-source-properties: | |
cachePrepStmts: true | |
prepStmtCacheSize: 250 | |
prepStmtCacheSqlLimit: 2048 | |
useServerPrepStmts: true |
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
@Configuration | |
@EnableJpaRepositories("package.to.repository") | |
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware") | |
@EnableTransactionManagement | |
public class DatabaseConfiguration { | |
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class); | |
@Bean | |
@ConfigurationProperties(prefix = "spring.datasource.hikari.master") | |
public DataSource masterDataSource() { | |
System.out.println("masterDataSource"); | |
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | |
} | |
@Bean | |
@ConfigurationProperties(prefix = "spring.datasource.hikari.slave") | |
public DataSource slaveDataSource() { | |
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | |
} | |
@Primary | |
@Bean | |
public DataSource dataSource() { | |
ReplicationRoutingDataSource routingDataSource = new ReplicationRoutingDataSource(); | |
Map<Object, Object> dataSourceMap = new LinkedHashMap<>(); | |
dataSourceMap.put("master", masterDataSource()); | |
dataSourceMap.put("slave", slaveDataSource()); | |
routingDataSource.setTargetDataSources(dataSourceMap); | |
routingDataSource.setDefaultTargetDataSource(masterDataSource()); | |
routingDataSource.afterPropertiesSet(); | |
return new LazyConnectionDataSourceProxy(routingDataSource); | |
} | |
} |
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
public class ReplicationRoutingDataSource extends AbstractRoutingDataSource { | |
@Override | |
protected Object determineCurrentLookupKey() { | |
return TransactionSynchronizationManager | |
.isCurrentTransactionReadOnly() ? "slave" : "master"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://vladmihalcea.com/read-write-read-only-transaction-routing-spring/