Skip to content

Commit 7b2b7d8

Browse files
committed
HHH-19708 auto-propagate the read-only mode to the JDBC Connection
1 parent ea154f4 commit 7b2b7d8

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

hibernate-core/src/main/java/org/hibernate/SessionBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ public interface SessionBuilder {
167167

168168
/**
169169
* Specify a {@linkplain Session#isDefaultReadOnly read-only mode}
170-
* for the session.
170+
* for the session. If a session is created in read-only mode, then
171+
* {@link Connection#setReadOnly} is called when a JDBC connection
172+
* is obtained.
171173
* <p>
172-
* If read/write replication is in use, then:
174+
* Furthermore, if read/write replication is in use, then:
173175
* <ul>
174176
* <li>a read-only session will connect to a read-only replica, but
175177
* <li>a non-read-only session will connect to a writable replica.

hibernate-core/src/main/java/org/hibernate/StatelessSessionBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public interface StatelessSessionBuilder {
5555
StatelessSessionBuilder tenantIdentifier(Object tenantIdentifier);
5656

5757
/**
58-
* Specify a read-only mode for the stateless session.
58+
* Specify a read-only mode for the stateless session. If a session
59+
* is created in read-only mode, then {@link Connection#setReadOnly}
60+
* is called when a JDBC connection is obtained.
5961
* <p>
60-
* If read/write replication is in use, then:
62+
* Furthermore, if read/write replication is in use, then:
6163
* <ul>
6264
* <li>a read-only session will connect to a read-only replica, but
6365
* <li>a non-read-only session will connect to a writable replica.

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,19 @@ public void afterObtainConnection(Connection connection) throws SQLException {
733733
initialSchema = connection.getSchema();
734734
connection.setSchema( tenantSchema() );
735735
}
736+
if ( readOnly ) {
737+
connection.setReadOnly( true );
738+
}
736739
}
737740

738741
@Override
739742
public void beforeReleaseConnection(Connection connection) throws SQLException {
740743
if ( useSchemaBasedMultiTenancy() ) {
741744
connection.setSchema( initialSchema );
742745
}
746+
if ( readOnly ) {
747+
connection.setReadOnly( false );
748+
}
743749
}
744750

745751
@Override

0 commit comments

Comments
 (0)