Created
November 13, 2020 21:16
-
-
Save iundarigun/a8b90cd46b0236ce1095302b932649a2 to your computer and use it in GitHub Desktop.
MappingSolution
This file contains hidden or 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 | |
class EntityManagerConfiguration( | |
localContainerEntityManagerFactoryBean: LocalContainerEntityManagerFactoryBean | |
) { | |
init { | |
if (localContainerEntityManagerFactoryBean.jpaDialect is HibernateJpaDialect) { | |
(localContainerEntityManagerFactoryBean.jpaDialect as HibernateJpaDialect) | |
.setPrepareConnection(false) | |
} | |
} | |
} |
This file contains hidden or 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 | |
class TransactionRoutingConfiguration( | |
private val dataSourceListProperties: DataSourceListProperties | |
) { | |
@Bean | |
@Primary | |
fun dataSource(): DataSource { | |
val transactionRoutingDataSource: AbstractRoutingDataSource = TransactionRoutingDataSource() | |
val readDataSource = buildDataSourceFromProperties(dataSourceListProperties.readDatasource, true) | |
val writeDataSource = buildDataSourceFromProperties(dataSourceListProperties.writeDatasource, false) | |
val targetDataSources: MutableMap<Any, Any> = mutableMapOf( | |
DataSourceType.READ_ONLY to readDataSource, | |
DataSourceType.READ_WRITE to writeDataSource | |
) | |
transactionRoutingDataSource.setDefaultTargetDataSource(readDataSource) | |
transactionRoutingDataSource.setTargetDataSources(targetDataSources) | |
transactionRoutingDataSource.afterPropertiesSet() | |
return transactionRoutingDataSource | |
} | |
private fun buildDataSourceFromProperties(dataSourceProperty: DataSourceProperties, readOnly: Boolean): DataSource { | |
return connectionPoolDataSource( | |
DataSourceBuilder.create() | |
.url(dataSourceProperty.url) | |
.username(dataSourceProperty.username) | |
.password(dataSourceProperty.password) | |
.driverClassName(dataSourceProperty.driverClassName) | |
.build(), | |
readOnly | |
) | |
} | |
private fun hikariConfig(dataSource: DataSource, readOnly: Boolean): HikariConfig { | |
val hikariConfig = HikariConfig() | |
hikariConfig.dataSource = dataSource | |
hikariConfig.isAutoCommit = false | |
hikariConfig.isReadOnly = readOnly | |
return hikariConfig | |
} | |
private fun connectionPoolDataSource(dataSource: DataSource, readOnly: Boolean): HikariDataSource { | |
return HikariDataSource(hikariConfig(dataSource, readOnly)) | |
} | |
} |
This file contains hidden or 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
class TransactionRoutingDataSource : AbstractRoutingDataSource() { | |
override fun determineCurrentLookupKey(): Any { | |
return if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { | |
DataSourceType.READ_ONLY | |
} else { | |
DataSourceType.READ_WRITE | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment