Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FINERACT-2081: Consider read-only all the transactions when the connection is read-only #4217

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ public DataSource retrieveDataSource() {
Long tenantConnectionKey = tenantConnection.getConnectionId();
// if tenantConnection information available switch to the
// appropriate datasource for that tenant.
actualDataSource = TENANT_TO_DATA_SOURCE_MAP.computeIfAbsent(tenantConnectionKey, (key) -> {
DataSource tenantSpecificDataSource = dataSourcePerTenantServiceFactory.createNewDataSourceFor(tenantConnection);
return tenantSpecificDataSource;
});
actualDataSource = TENANT_TO_DATA_SOURCE_MAP.computeIfAbsent(tenantConnectionKey,
(key) -> dataSourcePerTenantServiceFactory.createNewDataSourceFor(tenantConnection));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
Expand All @@ -41,7 +43,7 @@ public ExtendedJpaTransactionManager() {
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
super.doBegin(transaction, definition);
if (isReadOnlyTx(transaction)) {
if (isReadOnlyConnection() || isReadOnlyTx(transaction)) {
EntityManager entityManager = getCurrentEntityManager();
if (entityManager != null) {
entityManager.setFlushMode(FlushModeType.COMMIT);
Expand All @@ -52,7 +54,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {

@Override
protected void doCommit(DefaultTransactionStatus status) {
if (isReadOnlyTx(status.getTransaction())) {
if (isReadOnlyConnection() || isReadOnlyTx(status.getTransaction())) {
EntityManager entityManager = getCurrentEntityManager();
if (entityManager != null) {
entityManager.clear();
Expand All @@ -62,6 +64,14 @@ protected void doCommit(DefaultTransactionStatus status) {
invokeLifecycleCallbacks(TransactionLifecycleCallback::afterCommit);
}

private boolean isReadOnlyConnection() {
try (Connection connection = getDataSource().getConnection()) {
return connection.isReadOnly();
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}

@Override
protected void doCleanupAfterCompletion(Object transaction) {
super.doCleanupAfterCompletion(transaction);
Expand Down
Loading