下面列出了怎么用org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode的API类实例代码及写法,或者点击链接到github查看源代码。
protected Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", hibernateDialect);
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.put(
"hibernate.integrator_provider",
(IntegratorProvider) () -> Collections.singletonList(
new ClassImportIntegrator(Arrays.asList(PostDTO.class))
)
);
properties.put(
AvailableSettings.CONNECTION_HANDLING,
//PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
);
return properties;
}
/**
* Return whether the given Hibernate Session will always hold the same
* JDBC Connection. This is used to check whether the transaction manager
* can safely prepare and clean up the JDBC Connection used for a transaction.
* <p>The default implementation checks the Session's connection release mode
* to be "on_close".
* @param session the Hibernate Session to check
* @see ConnectionReleaseMode#ON_CLOSE
* @return Whether the same connection is needed for the whole session
*/
protected boolean isSameConnectionForEntireSession(Session session) {
if (!(session instanceof SessionImplementor)) {
// The best we can do is to assume we're safe.
return true;
}
PhysicalConnectionHandlingMode releaseMode =
((SessionImplementor) session).getJdbcCoordinator()
.getLogicalConnection()
.getConnectionHandlingMode();
return PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_HOLD.equals(releaseMode);
}
private PhysicalConnectionHandlingMode determineConnectionHandlingMode(
PhysicalConnectionHandlingMode connectionHandlingMode,
JdbcConnectionAccess jdbcConnectionAccess) {
if ( connectionHandlingMode.getReleaseMode() == ConnectionReleaseMode.AFTER_STATEMENT
&& !jdbcConnectionAccess.supportsAggressiveRelease() ) {
return PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION;
}
return connectionHandlingMode;
}
public Session openTemporarySession() throws HibernateException {
return withOptions()
.autoClose( false )
.flushMode( FlushMode.MANUAL )
.connectionHandlingMode( PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT )
.openSession();
}
@SuppressWarnings("deprecation")
private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
Map configurationSettings,
StandardServiceRegistry serviceRegistry) {
final PhysicalConnectionHandlingMode specifiedHandlingMode = PhysicalConnectionHandlingMode.interpret(
configurationSettings.get( CONNECTION_HANDLING )
);
if ( specifiedHandlingMode != null ) {
return specifiedHandlingMode;
}
final TransactionCoordinatorBuilder transactionCoordinatorBuilder = serviceRegistry.getService( TransactionCoordinatorBuilder.class );
// see if the deprecated ConnectionAcquisitionMode/ConnectionReleaseMode were used..
final ConnectionAcquisitionMode specifiedAcquisitionMode = ConnectionAcquisitionMode.interpret(
configurationSettings.get( ACQUIRE_CONNECTIONS )
);
final ConnectionReleaseMode specifiedReleaseMode = ConnectionReleaseMode.interpret(
configurationSettings.get( RELEASE_CONNECTIONS )
);
if ( specifiedAcquisitionMode != null || specifiedReleaseMode != null ) {
return interpretConnectionHandlingMode( specifiedAcquisitionMode, specifiedReleaseMode, configurationSettings, transactionCoordinatorBuilder );
}
return transactionCoordinatorBuilder.getDefaultConnectionHandlingMode();
}
@SuppressWarnings("deprecation")
private PhysicalConnectionHandlingMode interpretConnectionHandlingMode(
ConnectionAcquisitionMode specifiedAcquisitionMode,
ConnectionReleaseMode specifiedReleaseMode,
Map configurationSettings,
TransactionCoordinatorBuilder transactionCoordinatorBuilder) {
DeprecationLogger.DEPRECATION_LOGGER.logUseOfDeprecatedConnectionHandlingSettings();
final ConnectionAcquisitionMode effectiveAcquisitionMode = specifiedAcquisitionMode == null
? ConnectionAcquisitionMode.AS_NEEDED
: specifiedAcquisitionMode;
final ConnectionReleaseMode effectiveReleaseMode;
if ( specifiedReleaseMode == null ) {
// check the actual setting. If we get in here it *should* be "auto" or null
final String releaseModeName = ConfigurationHelper.getString( RELEASE_CONNECTIONS, configurationSettings, "auto" );
assert "auto".equalsIgnoreCase( releaseModeName );
// nothing was specified (or someone happened to configure the "magic" value)
if ( effectiveAcquisitionMode == ConnectionAcquisitionMode.IMMEDIATELY ) {
effectiveReleaseMode = ConnectionReleaseMode.ON_CLOSE;
}
else {
effectiveReleaseMode = transactionCoordinatorBuilder.getDefaultConnectionReleaseMode();
}
}
else {
effectiveReleaseMode = specifiedReleaseMode;
}
return PhysicalConnectionHandlingMode.interpret( effectiveAcquisitionMode, effectiveReleaseMode );
}
public void applyConnectionReleaseMode(ConnectionReleaseMode connectionReleaseMode) {
if ( this.connectionHandlingMode == null ) {
this.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret(
ConnectionAcquisitionMode.AS_NEEDED,
connectionReleaseMode
);
}
else {
this.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret(
this.connectionHandlingMode.getAcquisitionMode(),
connectionReleaseMode
);
}
}
protected void additionalProperties(Properties properties) {
DataSource actualDataSource = (DataSource) properties
.get(AvailableSettings.DATASOURCE);
properties.put(
AvailableSettings.CONNECTION_PROVIDER,
new ThreadLocalDatasourceConnectionProvider(actualDataSource)
);
properties.put(
AvailableSettings.CONNECTION_HANDLING,
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT
);
properties.put(
AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT,
Boolean.TRUE.toString()
);
properties.put(
AvailableSettings.GENERATE_STATISTICS,
Boolean.TRUE.toString()
);
properties.put(
StatisticsInitiator.STATS_BUILDER,
TransactionStatisticsFactory.class.getName()
);
}
@Override
public PhysicalConnectionHandlingMode getDefaultConnectionHandlingMode() {
return PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION;
}
@Override
public PhysicalConnectionHandlingMode getDefaultConnectionHandlingMode() {
// todo : I want to change this to PhysicalConnectionHandlingMode#IMMEDIATE_ACQUISITION_AND_HOLD
return PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT;
}
@Override
public PhysicalConnectionHandlingMode getConnectionHandlingMode() {
return PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD;
}
@Override
public PhysicalConnectionHandlingMode getConnectionHandlingMode() {
return connectionHandlingMode;
}
@Override
public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
delegate.connectionHandlingMode( mode );
return getThis();
}
@Override
public T connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
delegate.connectionHandlingMode( mode );
return getThis();
}
@Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return connectionHandlingMode;
}
@Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return connectionHandlingMode;
}
@Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return null;
}
@Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return delegate.getPhysicalConnectionHandlingMode();
}
@Override
public T applyConnectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) {
delegate.applyConnectionHandlingMode( connectionHandlingMode );
return getThis();
}
@Override
public PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode() {
return connectionHandlingMode;
}
public void applyConnectionHandlingMode(PhysicalConnectionHandlingMode mode) {
this.connectionHandlingMode = mode;
}
@Override
public SessionFactoryBuilder applyConnectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) {
this.optionsBuilder.applyConnectionHandlingMode( connectionHandlingMode );
return this;
}
/**
* Signifies that the connection release mode from the original session should be used to create the new session.
*
* @param mode The connection handling mode to use.
*
* @return {@code this}, for method chaining
*/
T connectionHandlingMode(PhysicalConnectionHandlingMode mode);
/**
* The mode for physical handling of the JDBC Connection
*
* @return The JDBC Connection handlng mode
*
* @deprecated (since 5.2) access via {@link #getLogicalConnection} instead
*/
@Deprecated
default PhysicalConnectionHandlingMode getConnectionHandlingMode() {
return getLogicalConnection().getConnectionHandlingMode();
}
/**
* Apply the specified handling mode for JDBC connections
*
* @param connectionHandlingMode The handling mode to apply
*
* @return {@code this}, for method chaining
*
* @see org.hibernate.cfg.AvailableSettings#ACQUIRE_CONNECTIONS
* @see org.hibernate.cfg.AvailableSettings#RELEASE_CONNECTIONS
* @see org.hibernate.ConnectionAcquisitionMode
* @see ConnectionReleaseMode
*/
SessionFactoryBuilder applyConnectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode);
PhysicalConnectionHandlingMode getDefaultConnectionHandlingMode();
PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode();
PhysicalConnectionHandlingMode getPhysicalConnectionHandlingMode();