Skip to content

Commit 4a0172f

Browse files
committed
HHH-19350 improve interpretation logic in PhysicalConnectionHandlingMode
and properly document inter-mode compatibility in jdoc
1 parent 696363e commit 4a0172f

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ public enum ConnectionAcquisitionMode {
1818
/**
1919
* The {@code Connection} will be acquired as soon as a session is opened.
2020
* <p>
21-
* This circumvents the {@link ConnectionReleaseMode}, as the {@code Connection}
22-
* will then be held until the session is closed.
21+
* In this acquisition mode, {@link ConnectionReleaseMode#ON_CLOSE} must be used.
2322
*/
2423
IMMEDIATELY,
2524
/**
2625
* A {@code Connection} is acquired only when (and if) it's actually needed.
2726
* <p>
2827
* This is the default (and legacy) behavior.
28+
* <p>
29+
* In this acquisition mode, any {@link ConnectionReleaseMode} must be used.
2930
*/
3031
AS_NEEDED;
3132

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public interface SessionBuilder {
8888

8989
/**
9090
* Specifies the connection handling modes for the session.
91+
* <p>
92+
* Note that if {@link ConnectionAcquisitionMode#IMMEDIATELY} is specified,
93+
* then the release mode must be {@link ConnectionReleaseMode#ON_CLOSE}.
9194
*
9295
* @return {@code this}, for method chaining
9396
*

hibernate-core/src/main/java/org/hibernate/resource/jdbc/spi/PhysicalConnectionHandlingMode.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import org.hibernate.ConnectionAcquisitionMode;
1010
import org.hibernate.ConnectionReleaseMode;
1111

12+
import static java.util.Objects.requireNonNull;
1213
import static org.hibernate.ConnectionAcquisitionMode.AS_NEEDED;
1314
import static org.hibernate.ConnectionAcquisitionMode.IMMEDIATELY;
1415
import static org.hibernate.ConnectionReleaseMode.AFTER_STATEMENT;
1516
import static org.hibernate.ConnectionReleaseMode.AFTER_TRANSACTION;
1617
import static org.hibernate.ConnectionReleaseMode.BEFORE_TRANSACTION_COMPLETION;
1718
import static org.hibernate.ConnectionReleaseMode.ON_CLOSE;
19+
import static org.hibernate.internal.util.StringHelper.isBlank;
1820

1921
/**
2022
* Enumerates valid combinations of {@link ConnectionAcquisitionMode} and
@@ -72,42 +74,39 @@ public ConnectionReleaseMode getReleaseMode() {
7274
}
7375

7476
public static PhysicalConnectionHandlingMode interpret(Object setting) {
75-
if ( setting == null ) {
76-
return null;
77+
if ( setting instanceof PhysicalConnectionHandlingMode mode ) {
78+
return mode;
7779
}
80+
else if ( setting instanceof String string ) {
81+
return isBlank( string ) ? null
82+
: valueOf( string.trim().toUpperCase( Locale.ROOT ) );
7883

79-
if ( setting instanceof PhysicalConnectionHandlingMode physicalConnectionHandlingMode ) {
80-
return physicalConnectionHandlingMode;
8184
}
82-
83-
final String value = setting.toString().trim();
84-
if ( value.isEmpty() ) {
85+
else {
8586
return null;
8687
}
87-
88-
return valueOf( value.toUpperCase( Locale.ROOT ) );
8988
}
9089

9190
public static PhysicalConnectionHandlingMode interpret(
9291
ConnectionAcquisitionMode acquisitionMode,
9392
ConnectionReleaseMode releaseMode) {
94-
if ( acquisitionMode == IMMEDIATELY ) {
95-
if ( releaseMode != null && releaseMode != ON_CLOSE ) {
96-
throw new IllegalArgumentException(
93+
requireNonNull( acquisitionMode, "ConnectionAcquisitionMode must be specified" );
94+
requireNonNull( acquisitionMode, "ConnectionReleaseMode must be specified" );
95+
return switch ( acquisitionMode ) {
96+
case AS_NEEDED -> switch ( releaseMode ) {
97+
case ON_CLOSE -> DELAYED_ACQUISITION_AND_HOLD;
98+
case AFTER_STATEMENT -> DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT;
99+
case BEFORE_TRANSACTION_COMPLETION -> DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION;
100+
case AFTER_TRANSACTION -> DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION;
101+
};
102+
case IMMEDIATELY -> switch ( releaseMode ) {
103+
case ON_CLOSE -> IMMEDIATE_ACQUISITION_AND_HOLD;
104+
default -> throw new IllegalArgumentException(
97105
"Only ConnectionReleaseMode.ON_CLOSE can be used in combination with "
98106
+ "ConnectionAcquisitionMode.IMMEDIATELY; but ConnectionReleaseMode."
99107
+ releaseMode.name() + " was specified."
100108
);
101-
}
102-
return IMMEDIATE_ACQUISITION_AND_HOLD;
103-
}
104-
else {
105-
return switch ( releaseMode ) {
106-
case AFTER_STATEMENT -> DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT;
107-
case BEFORE_TRANSACTION_COMPLETION -> DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION;
108-
case AFTER_TRANSACTION -> DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION;
109-
default -> DELAYED_ACQUISITION_AND_HOLD;
110109
};
111-
}
110+
};
112111
}
113112
}

0 commit comments

Comments
 (0)