-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AYS-297 | Application DataSource Has Been Configured for Reader and W…
…riter Instances (#364)
- Loading branch information
1 parent
eda1813
commit 6f61411
Showing
18 changed files
with
186 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
97 changes: 97 additions & 0 deletions
97
src/main/java/org/ays/common/config/datasource/AysDatabaseConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.ays.common.config.datasource; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
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.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
class AysDatabaseConfiguration { | ||
|
||
@Value("${spring.datasource.reader.url}") | ||
private String readerUrl; | ||
|
||
@Value("${spring.datasource.writer.url}") | ||
private String writerUrl; | ||
|
||
@Value("${spring.datasource.username}") | ||
private String username; | ||
|
||
@Value("${spring.datasource.password}") | ||
private String password; | ||
|
||
@Value("${spring.datasource.driver-class-name}") | ||
private String driverClassName; | ||
|
||
|
||
@Bean | ||
DataSource readOnlyDataSource() { | ||
return DataSourceBuilder.create() | ||
.username(this.username) | ||
.password(this.password) | ||
.url(this.readerUrl) | ||
.driverClassName(this.driverClassName) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@LiquibaseDataSource | ||
DataSource readWriteDataSource() { | ||
return DataSourceBuilder.create() | ||
.username(this.username) | ||
.password(this.password) | ||
.url(this.writerUrl) | ||
.driverClassName(this.driverClassName) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource.hikari") | ||
DataSource hikariDataSource() { | ||
return DataSourceBuilder.create() | ||
.type(HikariDataSource.class) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
DataSource routingDataSource(DataSource readWriteDataSource, DataSource readOnlyDataSource) { | ||
return new AysTransactionRoutingDataSource(readWriteDataSource, readOnlyDataSource); | ||
} | ||
|
||
|
||
@Bean | ||
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource readWriteDataSource, | ||
DataSource readOnlyDataSource, | ||
EntityManagerFactoryBuilder entityManagerFactoryBuilder) { | ||
|
||
DataSource dataSource = routingDataSource(readWriteDataSource, readOnlyDataSource); | ||
return entityManagerFactoryBuilder | ||
.dataSource(dataSource) | ||
.packages("org.ays") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
PlatformTransactionManager transactionManager(PlatformTransactionManager jpaTransactionManager) { | ||
return new AysTransactionManager(jpaTransactionManager); | ||
} | ||
|
||
@Bean | ||
PlatformTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) { | ||
return new JpaTransactionManager(entityManagerFactory); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/ays/common/config/datasource/AysTransactionManager.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ays.common.config.datasource; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.TransactionException; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.TransactionDefinition; | ||
import org.springframework.transaction.TransactionStatus; | ||
|
||
@RequiredArgsConstructor | ||
class AysTransactionManager implements PlatformTransactionManager { | ||
|
||
private final PlatformTransactionManager platformTransactionManager; | ||
|
||
@Override | ||
public @NotNull TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { | ||
boolean isReadOnly = definition != null && definition.isReadOnly(); | ||
AysTransactionRoutingDataSource.setReadonlyDataSource(isReadOnly); | ||
return platformTransactionManager.getTransaction(definition); | ||
} | ||
|
||
@Override | ||
public void commit(@NotNull TransactionStatus status) throws TransactionException { | ||
platformTransactionManager.commit(status); | ||
} | ||
|
||
@Override | ||
public void rollback(@NotNull TransactionStatus status) throws TransactionException { | ||
platformTransactionManager.rollback(status); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/ays/common/config/datasource/AysTransactionRoutingDataSource.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.ays.common.config.datasource; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Map; | ||
|
||
class AysTransactionRoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
private static DataSourceType currentDataSource = DataSourceType.READ_WRITE; | ||
|
||
public AysTransactionRoutingDataSource(DataSource readWriteDataSource, DataSource readOnlyDataSource) { | ||
|
||
super.setTargetDataSources( | ||
Map.of( | ||
DataSourceType.READ_WRITE, readWriteDataSource, | ||
DataSourceType.READ_ONLY, readOnlyDataSource | ||
) | ||
); | ||
|
||
super.setDefaultTargetDataSource(readWriteDataSource); | ||
} | ||
|
||
static void setReadonlyDataSource(boolean isReadonly) { | ||
|
||
if (isReadonly) { | ||
currentDataSource = DataSourceType.READ_ONLY; | ||
return; | ||
} | ||
|
||
currentDataSource = DataSourceType.READ_WRITE; | ||
} | ||
|
||
@Override | ||
public Object determineCurrentLookupKey() { | ||
return currentDataSource; | ||
} | ||
|
||
private enum DataSourceType { | ||
READ_ONLY, | ||
READ_WRITE | ||
} | ||
|
||
} |
Oops, something went wrong.