Skip to content

Commit abace00

Browse files
committed
HHH-5184 - Create a ConnectionAcquisitionMode as corollary to ConnectionReleaseMode
1 parent b1d1a3c commit abace00

15 files changed

+272
-53
lines changed

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
/**
1010
* Indicates the manner in which JDBC Connections should be acquired. Inverse to
1111
* {@link org.hibernate.ConnectionReleaseMode}.
12-
* <p/>
13-
* NOTE : Not yet used. The only current behavior is the legacy behavior, which is
14-
* {@link #AS_NEEDED}.
1512
*
1613
* @author Steve Ebersole
1714
*/
@@ -23,11 +20,16 @@ public enum ConnectionAcquisitionMode {
2320
*/
2421
IMMEDIATELY,
2522
/**
26-
* The legacy behavior. A Connection is only acquired when (if_) it is actually needed.
23+
* The legacy behavior. A Connection is only acquired when (if) it is actually needed.
2724
*/
28-
AS_NEEDED,
29-
/**
30-
* Not sure yet tbh :)
31-
*/
32-
DEFAULT
25+
AS_NEEDED;
26+
27+
public static ConnectionAcquisitionMode interpret(String value) {
28+
if ( value != null
29+
&& ( "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value ) ) ) {
30+
return IMMEDIATELY;
31+
}
32+
33+
return AS_NEEDED;
34+
}
3335
}

hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
2424
import org.hibernate.loader.BatchFetchStyle;
2525
import org.hibernate.proxy.EntityNotFoundDelegate;
26+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2627
import org.hibernate.resource.jdbc.spi.StatementInspector;
2728
import org.hibernate.tuple.entity.EntityTuplizer;
2829
import org.hibernate.tuple.entity.EntityTuplizerFactory;
@@ -622,6 +623,20 @@ public SessionFactoryBuilder applyEntityTuplizer(
622623
*/
623624
public SessionFactoryBuilder applyJdbcFetchSize(int size);
624625

626+
/**
627+
* Apply the specified handling mode for JDBC connections
628+
*
629+
* @param connectionHandlingMode The handling mode to apply
630+
*
631+
* @return {@code this}, for method chaining
632+
*
633+
* @see org.hibernate.cfg.AvailableSettings#ACQUIRE_CONNECTIONS
634+
* @see org.hibernate.cfg.AvailableSettings#RELEASE_CONNECTIONS
635+
* @see org.hibernate.ConnectionAcquisitionMode
636+
* @see ConnectionReleaseMode
637+
*/
638+
SessionFactoryBuilder applyConnectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode);
639+
625640
/**
626641
* Apply a ConnectionReleaseMode.
627642
*
@@ -630,8 +645,11 @@ public SessionFactoryBuilder applyEntityTuplizer(
630645
* @return {@code this}, for method chaining
631646
*
632647
* @see org.hibernate.cfg.AvailableSettings#RELEASE_CONNECTIONS
648+
*
649+
* @deprecated Use {@link #applyConnectionHandlingMode} instead
633650
*/
634-
public SessionFactoryBuilder applyConnectionReleaseMode(ConnectionReleaseMode connectionReleaseMode);
651+
@Deprecated
652+
SessionFactoryBuilder applyConnectionReleaseMode(ConnectionReleaseMode connectionReleaseMode);
635653

636654
/**
637655
* Should Hibernate apply comments to SQL it generates?

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java

+52-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414
import java.util.Map;
1515

16+
import org.hibernate.ConnectionAcquisitionMode;
1617
import org.hibernate.ConnectionReleaseMode;
1718
import org.hibernate.CustomEntityDirtinessStrategy;
1819
import org.hibernate.EmptyInterceptor;
@@ -48,13 +49,15 @@
4849
import org.hibernate.internal.util.config.ConfigurationHelper;
4950
import org.hibernate.loader.BatchFetchStyle;
5051
import org.hibernate.proxy.EntityNotFoundDelegate;
52+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
5153
import org.hibernate.resource.jdbc.spi.StatementInspector;
5254
import org.hibernate.resource.transaction.TransactionCoordinatorBuilder;
5355
import org.hibernate.service.spi.ServiceRegistryImplementor;
5456
import org.hibernate.tuple.entity.EntityTuplizer;
5557
import org.hibernate.tuple.entity.EntityTuplizerFactory;
5658
import org.jboss.logging.Logger;
5759

60+
import static org.hibernate.cfg.AvailableSettings.ACQUIRE_CONNECTIONS;
5861
import static org.hibernate.cfg.AvailableSettings.AUTO_CLOSE_SESSION;
5962
import static org.hibernate.cfg.AvailableSettings.AUTO_EVICT_COLLECTION_CACHE;
6063
import static org.hibernate.cfg.AvailableSettings.AUTO_SESSION_EVENTS_LISTENER;
@@ -411,9 +414,26 @@ public SessionFactoryBuilder applyJdbcFetchSize(int size) {
411414
return this;
412415
}
413416

417+
@Override
418+
public SessionFactoryBuilder applyConnectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) {
419+
this.options.connectionHandlingMode = connectionHandlingMode;
420+
return this;
421+
}
422+
414423
@Override
415424
public SessionFactoryBuilder applyConnectionReleaseMode(ConnectionReleaseMode connectionReleaseMode) {
416-
this.options.connectionReleaseMode = connectionReleaseMode;
425+
if ( this.options.connectionHandlingMode == null ) {
426+
this.options.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret(
427+
ConnectionAcquisitionMode.AS_NEEDED,
428+
connectionReleaseMode
429+
);
430+
}
431+
else {
432+
this.options.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret(
433+
this.options.connectionHandlingMode.getAcquisitionMode(),
434+
connectionReleaseMode
435+
);
436+
}
417437
return this;
418438
}
419439

@@ -526,7 +546,7 @@ public static class SessionFactoryOptionsStateStandardImpl implements SessionFac
526546
private Integer jdbcFetchSize;
527547
private boolean scrollableResultSetsEnabled;
528548
private boolean commentsEnabled;
529-
private ConnectionReleaseMode connectionReleaseMode;
549+
private PhysicalConnectionHandlingMode connectionHandlingMode;
530550
private boolean wrapResultSetsEnabled;
531551

532552
private Map<String, SQLFunction> sqlFunctions;
@@ -684,14 +704,30 @@ public SessionFactoryOptionsStateStandardImpl(StandardServiceRegistry serviceReg
684704
);
685705
this.jdbcFetchSize = ConfigurationHelper.getInteger( STATEMENT_FETCH_SIZE, configurationSettings );
686706

707+
final ConnectionAcquisitionMode connectionAcquisitionMode = ConnectionAcquisitionMode.interpret(
708+
ConfigurationHelper.getString(
709+
ACQUIRE_CONNECTIONS,
710+
configurationSettings,
711+
ConnectionAcquisitionMode.AS_NEEDED.name()
712+
)
713+
);
714+
715+
final ConnectionReleaseMode connectionReleaseMode;
687716
final String releaseModeName = ConfigurationHelper.getString( RELEASE_CONNECTIONS, configurationSettings, "auto" );
688717
if ( "auto".equals( releaseModeName ) ) {
689-
this.connectionReleaseMode = serviceRegistry.getService( TransactionCoordinatorBuilder.class )
690-
.getDefaultConnectionReleaseMode();
718+
// nothing was specified (or someone happened to configure the "magic" value)
719+
if ( connectionAcquisitionMode == ConnectionAcquisitionMode.IMMEDIATELY ) {
720+
connectionReleaseMode = ConnectionReleaseMode.ON_CLOSE;
721+
}
722+
else {
723+
connectionReleaseMode = serviceRegistry.getService( TransactionCoordinatorBuilder.class )
724+
.getDefaultConnectionReleaseMode();
725+
}
691726
}
692727
else {
693728
connectionReleaseMode = ConnectionReleaseMode.parse( releaseModeName );
694729
}
730+
this.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret( connectionAcquisitionMode, connectionReleaseMode );
695731

696732
this.commentsEnabled = ConfigurationHelper.getBoolean( USE_SQL_COMMENTS, configurationSettings );
697733

@@ -928,9 +964,14 @@ public Integer getJdbcFetchSize() {
928964
return jdbcFetchSize;
929965
}
930966

967+
@Override
968+
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
969+
return connectionHandlingMode;
970+
}
971+
931972
@Override
932973
public ConnectionReleaseMode getConnectionReleaseMode() {
933-
return connectionReleaseMode;
974+
return getPhysicalConnectionHandlingMode().getReleaseMode();
934975
}
935976

936977
@Override
@@ -1194,9 +1235,14 @@ public Integer getJdbcFetchSize() {
11941235
return options.getJdbcFetchSize();
11951236
}
11961237

1238+
@Override
1239+
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
1240+
return options.getPhysicalConnectionHandlingMode();
1241+
}
1242+
11971243
@Override
11981244
public ConnectionReleaseMode getConnectionReleaseMode() {
1199-
return options.getConnectionReleaseMode();
1245+
return getPhysicalConnectionHandlingMode().getReleaseMode();
12001246
}
12011247

12021248
@Override

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsImpl.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Map;
1010

11+
import org.hibernate.ConnectionAcquisitionMode;
1112
import org.hibernate.ConnectionReleaseMode;
1213
import org.hibernate.CustomEntityDirtinessStrategy;
1314
import org.hibernate.EntityMode;
@@ -27,6 +28,7 @@
2728
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
2829
import org.hibernate.loader.BatchFetchStyle;
2930
import org.hibernate.proxy.EntityNotFoundDelegate;
31+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
3032
import org.hibernate.resource.jdbc.spi.StatementInspector;
3133
import org.hibernate.tuple.entity.EntityTuplizerFactory;
3234

@@ -108,7 +110,7 @@ public class SessionFactoryOptionsImpl implements SessionFactoryOptions {
108110
private final Integer jdbcFetchSize;
109111
private final boolean scrollableResultSetsEnabled;
110112
private final boolean commentsEnabled;
111-
private final ConnectionReleaseMode connectionReleaseMode;
113+
private final PhysicalConnectionHandlingMode physicalConnectionHandlingMode;
112114
private final boolean wrapResultSetsEnabled;
113115

114116
private final Map<String, SQLFunction> sqlFunctions;
@@ -168,7 +170,7 @@ public SessionFactoryOptionsImpl(SessionFactoryOptionsState state) {
168170
this.autoEvictCollectionCache = state.isAutoEvictCollectionCache();
169171

170172
this.schemaAutoTooling = state.getSchemaAutoTooling();
171-
this.connectionReleaseMode = state.getConnectionReleaseMode();
173+
this.physicalConnectionHandlingMode = state.getPhysicalConnectionHandlingMode();
172174
this.getGeneratedKeysEnabled = state.isGetGeneratedKeysEnabled();
173175
this.jdbcBatchSize = state.getJdbcBatchSize();
174176
this.jdbcBatchVersionedData = state.isJdbcBatchVersionedData();
@@ -408,9 +410,14 @@ public Integer getJdbcFetchSize() {
408410
return jdbcFetchSize;
409411
}
410412

413+
@Override
414+
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
415+
return physicalConnectionHandlingMode;
416+
}
417+
411418
@Override
412419
public ConnectionReleaseMode getConnectionReleaseMode() {
413-
return connectionReleaseMode;
420+
return physicalConnectionHandlingMode.getReleaseMode();
414421
}
415422

416423
@Override

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsState.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Map;
1010

11+
import org.hibernate.ConnectionAcquisitionMode;
1112
import org.hibernate.ConnectionReleaseMode;
1213
import org.hibernate.CustomEntityDirtinessStrategy;
1314
import org.hibernate.EntityMode;
@@ -26,6 +27,7 @@
2627
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
2728
import org.hibernate.loader.BatchFetchStyle;
2829
import org.hibernate.proxy.EntityNotFoundDelegate;
30+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2931
import org.hibernate.resource.jdbc.spi.StatementInspector;
3032
import org.hibernate.tuple.entity.EntityTuplizerFactory;
3133

@@ -127,7 +129,13 @@ public interface SessionFactoryOptionsState {
127129

128130
public Integer getJdbcFetchSize();
129131

130-
public ConnectionReleaseMode getConnectionReleaseMode();
132+
PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode();
133+
134+
/**
135+
* @deprecated Use {@link #getPhysicalConnectionHandlingMode()} instead
136+
*/
137+
@Deprecated
138+
ConnectionReleaseMode getConnectionReleaseMode();
131139

132140
public boolean isCommentsEnabled();
133141

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
2727
import org.hibernate.loader.BatchFetchStyle;
2828
import org.hibernate.proxy.EntityNotFoundDelegate;
29+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2930
import org.hibernate.resource.jdbc.spi.StatementInspector;
3031
import org.hibernate.tuple.entity.EntityTuplizerFactory;
3132

@@ -34,6 +35,7 @@
3435
*
3536
* @author Steve Ebersole
3637
*/
38+
@SuppressWarnings("unused")
3739
public abstract class AbstractDelegatingSessionFactoryOptions implements SessionFactoryOptions {
3840
private final SessionFactoryOptions delegate;
3941

@@ -272,6 +274,12 @@ public Integer getJdbcFetchSize() {
272274
}
273275

274276
@Override
277+
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
278+
return delegate.getPhysicalConnectionHandlingMode();
279+
}
280+
281+
@Override
282+
@SuppressWarnings("deprecation")
275283
public ConnectionReleaseMode getConnectionReleaseMode() {
276284
return delegate.getConnectionReleaseMode();
277285
}

hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Map;
1010

11+
import org.hibernate.ConnectionAcquisitionMode;
1112
import org.hibernate.ConnectionReleaseMode;
1213
import org.hibernate.CustomEntityDirtinessStrategy;
1314
import org.hibernate.EntityMode;
@@ -26,6 +27,7 @@
2627
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
2728
import org.hibernate.loader.BatchFetchStyle;
2829
import org.hibernate.proxy.EntityNotFoundDelegate;
30+
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2931
import org.hibernate.resource.jdbc.spi.StatementInspector;
3032
import org.hibernate.tuple.entity.EntityTuplizerFactory;
3133

@@ -151,7 +153,13 @@ public interface SessionFactoryOptions {
151153

152154
public Integer getJdbcFetchSize();
153155

154-
public ConnectionReleaseMode getConnectionReleaseMode();
156+
PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode();
157+
158+
/**
159+
* @deprecated Use {@link #getPhysicalConnectionHandlingMode()} instead
160+
*/
161+
@Deprecated
162+
ConnectionReleaseMode getConnectionReleaseMode();
155163

156164
public boolean isCommentsEnabled();
157165

hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,20 @@ public interface AvailableSettings {
513513
String FLUSH_BEFORE_COMPLETION = "hibernate.transaction.flush_before_completion";
514514

515515
/**
516-
* Specifies how Hibernate should release JDBC connections.
516+
* Specifies how Hibernate should acquire JDBC connections. Should generally only configure
517+
* this or {@link #RELEASE_CONNECTIONS}, not both
518+
*
519+
* @see org.hibernate.ConnectionAcquisitionMode
520+
*
521+
* @since 5.1
522+
*/
523+
String ACQUIRE_CONNECTIONS = "hibernate.connection.acquisition_mode";
524+
525+
/**
526+
* Specifies how Hibernate should release JDBC connections. Should generally only configure
527+
* this or {@link #ACQUIRE_CONNECTIONS}, not both
528+
*
529+
* @see org.hibernate.ConnectionReleaseMode
517530
*/
518531
String RELEASE_CONNECTIONS = "hibernate.connection.release_mode";
519532

0 commit comments

Comments
 (0)