Skip to content

Commit 2c4a021

Browse files
committed
HHH-19708 MultiTenantConnectionProvider should fall back to writable replica
This is better than throwing, because you might be using: - JDBC driver-level support for replicas, together with - true multi-tenancy
1 parent 7b4feea commit 2c4a021

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/MultiTenantConnectionProvider.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* <p>
2323
* An application usually implements its own custom {@code MultiTenantConnectionProvider}
2424
* by subclassing {@link AbstractMultiTenantConnectionProvider}.
25+
* <p>
26+
* Support for read-only replicas may be implemented by overriding the operations
27+
* {@link #getReadOnlyConnection} and {@link #releaseReadOnlyConnection}.
2528
*
2629
* @param <T> The tenant identifier type
2730
*
@@ -73,12 +76,17 @@ public interface MultiTenantConnectionProvider<T> extends Service, Wrapped {
7376
* @throws SQLException Indicates a problem opening a connection
7477
* @throws org.hibernate.HibernateException Indicates a problem obtaining a connection
7578
*
79+
* @implNote This default implementation simply calls {@link #getConnection(Object)},
80+
* which returns a connection a writable replica. If this operation is overridden to
81+
* return a connection to a distinct read-only replica, the matching operation
82+
* {@link #releaseReadOnlyConnection(Object, Connection)} must also be overridden.
83+
*
7684
* @since 7.2
7785
*/
7886
@Incubating
7987
default Connection getReadOnlyConnection(T tenantIdentifier)
8088
throws SQLException {
81-
throw new UnsupportedOperationException( "No read-only replica is available" );
89+
return getConnection( tenantIdentifier );
8290
}
8391

8492
/**
@@ -101,12 +109,18 @@ default Connection getReadOnlyConnection(T tenantIdentifier)
101109
* @throws SQLException Indicates a problem closing the connection
102110
* @throws org.hibernate.HibernateException Indicates a problem releasing a connection
103111
*
112+
* @implNote This default implementation simply calls
113+
* {@link #releaseConnection(Object, Connection)}. If
114+
* {@link #getReadOnlyConnection(Object)} is overridden to return a
115+
* connection to a distinct read-only replica, this operation must also
116+
* be overridden.
117+
*
104118
* @since 7.2
105119
*/
106120
@Incubating
107121
default void releaseReadOnlyConnection(T tenantIdentifier, Connection connection)
108122
throws SQLException {
109-
throw new UnsupportedOperationException( "No read-only replica is available" );
123+
releaseConnection( tenantIdentifier, connection );
110124
}
111125

112126
/**

0 commit comments

Comments
 (0)