Allow Hibernate's Transaction act like JPA's EntityTransaction

This commit is contained in:
Steve Ebersole 2017-11-29 22:00:06 -06:00
parent dced921456
commit 3a1eb3382b
4 changed files with 34 additions and 3 deletions

View File

@ -158,8 +158,20 @@ public int getTimeout() {
return this.transactionCoordinator.getTimeOut(); return this.transactionCoordinator.getTimeOut();
} }
@Override
public void markRollbackOnly() {
// this is the Hibernate-specific API, whereas #setRollbackOnly is the
// JPA-defined API. In our opinion it is much more user-friendly to
// always allow user/integration to indicate that the transaction
// should not be allowed to commit.
internalGetTransactionDriverControl().markRollbackOnly();
}
@Override @Override
public void setRollbackOnly() { public void setRollbackOnly() {
// Since this is the JPA-defined one, we make sure the txn is active first
// so long as compliance (JpaCompliance) has not been defined to disable
// that check - making this active more like Hibernate's #markRollbackOnly
if ( jpaCompliance.isJpaTransactionComplianceEnabled() ) { if ( jpaCompliance.isJpaTransactionComplianceEnabled() ) {
if ( !isActive() ) { if ( !isActive() ) {
throw new IllegalStateException( throw new IllegalStateException(
@ -169,7 +181,7 @@ public void setRollbackOnly() {
} }
} }
internalGetTransactionDriverControl().markRollbackOnly(); markRollbackOnly();
} }
@Override @Override

View File

@ -29,7 +29,7 @@ public JpaComplianceImpl(Map configurationSettings, boolean jpaByDefault) {
queryCompliance = ConfigurationHelper.getBoolean( queryCompliance = ConfigurationHelper.getBoolean(
AvailableSettings.JPA_QUERY_COMPLIANCE, AvailableSettings.JPA_QUERY_COMPLIANCE,
configurationSettings, configurationSettings,
legacyQueryCompliance == null ? jpaByDefault : ConfigurationHelper.toBoolean( legacyQueryCompliance, jpaByDefault ) ConfigurationHelper.toBoolean( legacyQueryCompliance, jpaByDefault )
); );
transactionCompliance = ConfigurationHelper.getBoolean( transactionCompliance = ConfigurationHelper.getBoolean(
AvailableSettings.JPA_TRANSACTION_COMPLIANCE, AvailableSettings.JPA_TRANSACTION_COMPLIANCE,

View File

@ -34,7 +34,7 @@ public void testClosedChecks() {
.build(); .build();
try { try {
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources() final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources( ssr )
.buildMetadata() .buildMetadata()
.getSessionFactoryBuilder(); .getSessionFactoryBuilder();
final SessionFactory sf = factoryBuilder.build(); final SessionFactory sf = factoryBuilder.build();

View File

@ -13,6 +13,7 @@
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import javax.persistence.TransactionRequiredException;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.QueryImplementor;
@ -20,6 +21,7 @@
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -188,4 +190,21 @@ public void testSetInvalidMaxResults() {
} }
); );
} }
@Test
public void testUpdateRequiresTxn() {
inSession(
session -> {
try {
assertFalse( session.getTransaction().isActive() );
// Query
session.createQuery( "update Person set name = 'steve'" ).executeUpdate();
fail( "expecting failure" );
}
catch (TransactionRequiredException expected) {
// expected condition
}
}
);
}
} }