77import java .sql .Connection ;
88import java .sql .SQLException ;
99
10+ import org .hibernate .Incubating ;
1011import org .hibernate .dialect .Dialect ;
1112import org .hibernate .engine .jdbc .connections .internal .DatabaseConnectionInfoImpl ;
1213import org .hibernate .engine .jdbc .env .spi .ExtractedDatabaseMetaData ;
@@ -40,20 +41,62 @@ public interface ConnectionProvider extends Service, Wrapped {
4041 * @return The obtained JDBC connection
4142 *
4243 * @throws SQLException Indicates a problem opening a connection
43- * @throws org.hibernate.HibernateException Indicates a problem otherwise obtaining a connection.
44+ * @throws org.hibernate.HibernateException Indicates a problem obtaining a connection.
4445 */
4546 Connection getConnection () throws SQLException ;
4647
48+ /**
49+ * Obtains a connection to a read-only replica for use according to the underlying
50+ * strategy of this provider.
51+ *
52+ * @return The obtained JDBC connection
53+ *
54+ * @throws SQLException Indicates a problem opening a connection
55+ * @throws org.hibernate.HibernateException Indicates a problem obtaining a connection.
56+ *
57+ * @implNote This default implementation simply calls {@link #getConnection()},
58+ * which returns a connection to a writable replica. If this operation is overridden
59+ * to return a connection to a distinct read-only replica, the matching operation
60+ * {@link #closeReadOnlyConnection(Connection)} must also be overridden.
61+ *
62+ * @since 7.2
63+ */
64+ @ Incubating
65+ default Connection getReadOnlyConnection () throws SQLException {
66+ return getConnection ();
67+ }
68+
4769 /**
4870 * Release a connection from Hibernate use.
4971 *
5072 * @param connection The JDBC connection to release
5173 *
5274 * @throws SQLException Indicates a problem closing the connection
53- * @throws org.hibernate.HibernateException Indicates a problem otherwise releasing a connection.
75+ * @throws org.hibernate.HibernateException Indicates a problem releasing a connection.
5476 */
5577 void closeConnection (Connection connection ) throws SQLException ;
5678
79+ /**
80+ * Release a connection to a read-only replica from Hibernate use.
81+ *
82+ * @param connection The JDBC connection to release
83+ *
84+ * @throws SQLException Indicates a problem closing the connection
85+ * @throws org.hibernate.HibernateException Indicates a problem otherwise releasing a connection.
86+ *
87+ * @implNote This default implementation simply calls
88+ * {@link #closeConnection(Connection)}. If
89+ * {@link #getReadOnlyConnection()} is overridden to return a
90+ * connection to a distinct read-only replica, this operation must also
91+ * be overridden.
92+ *
93+ * @since 7.2
94+ */
95+ @ Incubating
96+ default void closeReadOnlyConnection (Connection connection ) throws SQLException {
97+ closeConnection ( connection );
98+ }
99+
57100 /**
58101 * Does this connection provider support aggressive release of JDBC connections and later
59102 * re-acquisition of those connections if needed?
@@ -72,6 +115,34 @@ public interface ConnectionProvider extends Service, Wrapped {
72115 */
73116 boolean supportsAggressiveRelease ();
74117
118+ /**
119+ * Does this connection provider correctly set the
120+ * {@linkplain java.sql.Connection#setSchema schema}
121+ * of the returned JDBC connections?
122+ * @return {@code true} if the connection provider handles this;
123+ * {@code false} if the client should set the schema
124+ *
125+ * @implNote If necessary, a {@code ConnectionProvider} may
126+ * call {@link org.hibernate.context.spi.MultiTenancy#getTenantSchemaMapper}
127+ * to obtain the {@link org.hibernate.context.spi.TenantSchemaMapper}.
128+ */
129+ @ Incubating
130+ default boolean handlesConnectionSchema () {
131+ return false ;
132+ }
133+
134+ /**
135+ * Does this connection provider correctly set the
136+ * {@linkplain java.sql.Connection#setReadOnly read-only mode}
137+ * of the returned JDBC connections?
138+ * @return {@code true} if the connection provider handles this;
139+ * {@code false} if the client should set the read-only mode
140+ */
141+ @ Incubating
142+ default boolean handlesConnectionReadOnly () {
143+ return false ;
144+ }
145+
75146 /**
76147 * @return an informative instance of {@link DatabaseConnectionInfo} for logging.
77148 *
0 commit comments