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 class TransactionImpl implements TransactionImplementor {
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
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 ( !isActive() ) {
throw new IllegalStateException(
@ -169,7 +181,7 @@ public class TransactionImpl implements TransactionImplementor {
}
}
internalGetTransactionDriverControl().markRollbackOnly();
markRollbackOnly();
}
@Override

View File

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

View File

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

View File

@ -13,6 +13,7 @@ import javax.persistence.Parameter;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TransactionRequiredException;
import org.hibernate.boot.MetadataSources;
import org.hibernate.query.spi.QueryImplementor;
@ -20,6 +21,7 @@ import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/**
@ -188,4 +190,21 @@ public class QueryApiTest extends BaseNonConfigCoreFunctionalTestCase {
}
);
}
@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
}
}
);
}
}