FINERACT-2081: Consider read-only all the transactions when the connection is read-only
This commit is contained in:
+2
-4
@@ -70,10 +70,8 @@ public class TomcatJdbcDataSourcePerTenantService implements RoutingDataSourceSe
|
|||||||
Long tenantConnectionKey = tenantConnection.getConnectionId();
|
Long tenantConnectionKey = tenantConnection.getConnectionId();
|
||||||
// if tenantConnection information available switch to the
|
// if tenantConnection information available switch to the
|
||||||
// appropriate datasource for that tenant.
|
// appropriate datasource for that tenant.
|
||||||
actualDataSource = TENANT_TO_DATA_SOURCE_MAP.computeIfAbsent(tenantConnectionKey, (key) -> {
|
actualDataSource = TENANT_TO_DATA_SOURCE_MAP.computeIfAbsent(tenantConnectionKey,
|
||||||
DataSource tenantSpecificDataSource = dataSourcePerTenantServiceFactory.createNewDataSourceFor(tenantConnection);
|
(key) -> dataSourcePerTenantServiceFactory.createNewDataSourceFor(tenantConnection));
|
||||||
return tenantSpecificDataSource;
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-2
@@ -20,6 +20,8 @@ package org.apache.fineract.infrastructure.core.persistence;
|
|||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.FlushModeType;
|
import jakarta.persistence.FlushModeType;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -41,7 +43,7 @@ public class ExtendedJpaTransactionManager extends JpaTransactionManager {
|
|||||||
@Override
|
@Override
|
||||||
protected void doBegin(Object transaction, TransactionDefinition definition) {
|
protected void doBegin(Object transaction, TransactionDefinition definition) {
|
||||||
super.doBegin(transaction, definition);
|
super.doBegin(transaction, definition);
|
||||||
if (isReadOnlyTx(transaction)) {
|
if (isReadOnlyConnection() || isReadOnlyTx(transaction)) {
|
||||||
EntityManager entityManager = getCurrentEntityManager();
|
EntityManager entityManager = getCurrentEntityManager();
|
||||||
if (entityManager != null) {
|
if (entityManager != null) {
|
||||||
entityManager.setFlushMode(FlushModeType.COMMIT);
|
entityManager.setFlushMode(FlushModeType.COMMIT);
|
||||||
@@ -52,7 +54,7 @@ public class ExtendedJpaTransactionManager extends JpaTransactionManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doCommit(DefaultTransactionStatus status) {
|
protected void doCommit(DefaultTransactionStatus status) {
|
||||||
if (isReadOnlyTx(status.getTransaction())) {
|
if (isReadOnlyConnection() || isReadOnlyTx(status.getTransaction())) {
|
||||||
EntityManager entityManager = getCurrentEntityManager();
|
EntityManager entityManager = getCurrentEntityManager();
|
||||||
if (entityManager != null) {
|
if (entityManager != null) {
|
||||||
entityManager.clear();
|
entityManager.clear();
|
||||||
@@ -62,6 +64,14 @@ public class ExtendedJpaTransactionManager extends JpaTransactionManager {
|
|||||||
invokeLifecycleCallbacks(TransactionLifecycleCallback::afterCommit);
|
invokeLifecycleCallbacks(TransactionLifecycleCallback::afterCommit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isReadOnlyConnection() {
|
||||||
|
try (Connection connection = getDataSource().getConnection()) {
|
||||||
|
return connection.isReadOnly();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doCleanupAfterCompletion(Object transaction) {
|
protected void doCleanupAfterCompletion(Object transaction) {
|
||||||
super.doCleanupAfterCompletion(transaction);
|
super.doCleanupAfterCompletion(transaction);
|
||||||
|
|||||||
Reference in New Issue
Block a user