Created
September 19, 2021 14:19
-
-
Save hoangnt-2197/0b45f06bbffc7588078e9e5804841f98 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
@Configuration | |
@ComponentScan( | |
basePackages = "com.example.routing.hpjp.util.spring.routing" | |
) | |
@PropertySource( | |
"/META-INF/jdbc-postgresql-replication.properties" | |
) | |
public class TransactionRoutingConfiguration | |
extends AbstractJPAConfiguration { | |
@Value("${jdbc.url.primary}") | |
private String primaryUrl; | |
@Value("${jdbc.url.replica}") | |
private String replicaUrl; | |
@Value("${jdbc.username}") | |
private String username; | |
@Value("${jdbc.password}") | |
private String password; | |
@Bean | |
public DataSource readWriteDataSource() { | |
PGSimpleDataSource dataSource = new PGSimpleDataSource(); | |
dataSource.setURL(primaryUrl); | |
dataSource.setUser(username); | |
dataSource.setPassword(password); | |
return connectionPoolDataSource(dataSource); | |
} | |
@Bean | |
public DataSource readOnlyDataSource() { | |
PGSimpleDataSource dataSource = new PGSimpleDataSource(); | |
dataSource.setURL(replicaUrl); | |
dataSource.setUser(username); | |
dataSource.setPassword(password); | |
return connectionPoolDataSource(dataSource); | |
} | |
@Bean | |
public TransactionRoutingDataSource actualDataSource() { | |
TransactionRoutingDataSource routingDataSource = | |
new TransactionRoutingDataSource(); | |
Map<Object, Object> dataSourceMap = new HashMap<>(); | |
dataSourceMap.put( | |
DataSourceType.READ_WRITE, | |
readWriteDataSource() | |
); | |
dataSourceMap.put( | |
DataSourceType.READ_ONLY, | |
readOnlyDataSource() | |
); | |
routingDataSource.setTargetDataSources(dataSourceMap); | |
return routingDataSource; | |
} | |
@Override | |
protected Properties additionalProperties() { | |
Properties properties = super.additionalProperties(); | |
properties.setProperty( | |
"hibernate.connection.provider_disables_autocommit", | |
Boolean.TRUE.toString() | |
); | |
return properties; | |
} | |
@Override | |
protected String[] packagesToScan() { | |
return new String[]{ | |
"com.vladmihalcea.book.hpjp.hibernate.transaction.forum" | |
}; | |
} | |
@Override | |
protected String databaseType() { | |
return Database.POSTGRESQL.name().toLowerCase(); | |
} | |
protected HikariConfig hikariConfig( | |
DataSource dataSource) { | |
HikariConfig hikariConfig = new HikariConfig(); | |
int cpuCores = Runtime.getRuntime().availableProcessors(); | |
hikariConfig.setMaximumPoolSize(cpuCores * 4); | |
hikariConfig.setDataSource(dataSource); | |
hikariConfig.setAutoCommit(false); | |
return hikariConfig; | |
} | |
protected HikariDataSource connectionPoolDataSource( | |
DataSource dataSource) { | |
return new HikariDataSource(hikariConfig(dataSource)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment