HHH-13565 Promote to FastSessionServices: isJtaTransactionAccessible

This commit is contained in:
Sanne Grinovero 2019-08-12 19:47:26 +01:00
parent 8931ef0962
commit be9fe27592
3 changed files with 18 additions and 16 deletions

View File

@ -407,7 +407,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@Override
public Transaction getTransaction() throws HibernateException {
if ( !isTransactionAccessible() ) {
if ( ! fastSessionServices.isJtaTransactionAccessible ) {
throw new IllegalStateException(
"Transaction is not accessible when using JTA with JPA-compliant transaction access enabled"
);
@ -415,17 +415,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return accessTransaction();
}
protected boolean isTransactionAccessible() {
// JPA requires that access not be provided to the transaction when using JTA.
// This is overridden when SessionFactoryOptions isJtaTransactionAccessEnabled() is true.
if ( getFactory().getSessionFactoryOptions().getJpaCompliance().isJpaTransactionComplianceEnabled() &&
getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta() &&
!getFactory().getSessionFactoryOptions().isJtaTransactionAccessEnabled() ) {
return false;
}
return true;
}
@Override
public Transaction accessTransaction() {
if ( this.currentHibernateTransaction == null ) {

View File

@ -87,6 +87,7 @@ final class FastSessionServices {
final ClassLoaderService classLoaderService;
final TransactionCoordinatorBuilder transactionCoordinatorBuilder;
final JdbcServices jdbcServices;
final boolean isJtaTransactionAccessible;
//Private fields:
private final Dialect dialect;
@ -120,9 +121,8 @@ final class FastSessionServices {
//Other highly useful constants:
this.dialect = jdbcServices.getJdbcEnvironment().getDialect();
this.disallowOutOfTransactionUpdateOperations = !sf.getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations();
this.useStreamForLobBinding = Environment.useStreamsForBinary()
|| dialect.useInputStreamToInsertBlob();
this.requiresMultiTenantConnectionProvider = sf.getSettings().getMultiTenancyStrategy().requiresMultiTenantConnectionProvider();
this.useStreamForLobBinding = Environment.useStreamsForBinary() || dialect.useInputStreamToInsertBlob();
this.requiresMultiTenantConnectionProvider = sf.getSettings().getMultiTenancyStrategy().requiresMultiTenantConnectionProvider();
//Some "hot" services:
this.connectionProvider = requiresMultiTenantConnectionProvider ? null : sr.getService( ConnectionProvider.class );
@ -130,6 +130,8 @@ final class FastSessionServices {
this.classLoaderService = sr.getService( ClassLoaderService.class );
this.transactionCoordinatorBuilder = sr.getService( TransactionCoordinatorBuilder.class );
this.jdbcServices = sr.getService( JdbcServices.class );
this.isJtaTransactionAccessible = isTransactionAccessible( sf, transactionCoordinatorBuilder );
}
Iterable<ClearEventListener> getClearEventListeners() {
@ -217,4 +219,15 @@ final class FastSessionServices {
return remapped == null ? sqlTypeDescriptor : remapped;
}
private static boolean isTransactionAccessible(SessionFactoryImpl sf, TransactionCoordinatorBuilder transactionCoordinatorBuilder) {
// JPA requires that access not be provided to the transaction when using JTA.
// This is overridden when SessionFactoryOptions isJtaTransactionAccessEnabled() is true.
if ( sf.getSessionFactoryOptions().getJpaCompliance().isJpaTransactionComplianceEnabled() &&
transactionCoordinatorBuilder.isJta() &&
!sf.getSessionFactoryOptions().isJtaTransactionAccessEnabled() ) {
return false;
}
return true;
}
}

View File

@ -2493,7 +2493,7 @@ public final class SessionImpl
// We do not want an exception to be thrown if the transaction
// is not accessible. If the transaction is not accessible,
// then return null.
return isTransactionAccessible() ? accessTransaction() : null;
return fastSessionServices.isJtaTransactionAccessible ? accessTransaction() : null;
}
@Override