mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-09 12:44:49 +00:00
Allow Hibernate's Transaction act like JPA's EntityTransaction
This commit is contained in:
parent
dced921456
commit
3a1eb3382b
@ -158,8 +158,20 @@ public int 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
|
||||
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 void setRollbackOnly() {
|
||||
}
|
||||
}
|
||||
|
||||
internalGetTransactionDriverControl().markRollbackOnly();
|
||||
markRollbackOnly();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,7 +29,7 @@ public JpaComplianceImpl(Map configurationSettings, boolean jpaByDefault) {
|
||||
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,
|
||||
|
@ -34,7 +34,7 @@ public void testClosedChecks() {
|
||||
.build();
|
||||
|
||||
try {
|
||||
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources()
|
||||
final SessionFactoryBuilderImplementor factoryBuilder = (SessionFactoryBuilderImplementor) new MetadataSources( ssr )
|
||||
.buildMetadata()
|
||||
.getSessionFactoryBuilder();
|
||||
final SessionFactory sf = factoryBuilder.build();
|
||||
|
@ -13,6 +13,7 @@
|
||||
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.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
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
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user