HHH-13262 - Add test for issue

This commit is contained in:
Andrea Boriero 2019-02-12 10:48:27 +00:00 committed by gbadner
parent b4f76b67ff
commit 4256f300dd
1 changed files with 38 additions and 35 deletions

View File

@ -8,20 +8,18 @@ package org.hibernate.test.flush;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TransactionRequiredException; import javax.persistence.TransactionRequiredException;
import java.io.Serializable;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.junit.Test;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Test;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue; import static org.hamcrest.core.IsNull.nullValue;
@ -50,21 +48,21 @@ public class NonTransactionalDataAccessTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected void prepareTest() throws Exception { protected void prepareTest() throws Exception {
try (Session s = openSession()) { final MyEntity entity = new MyEntity( "entity" );
final MyEntity entity = new MyEntity( "entity" ); inTransaction(
s.getTransaction().begin(); session -> {
try { session.save( entity );
s.save( entity );
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
s.getTransaction().rollback();
} }
throw e; );
} }
}
@After
public void tearDown() {
inTransaction(
session -> {
session.createQuery( "delete from MyEntity" ).executeUpdate();
}
);
} }
@Test @Test
@ -82,6 +80,26 @@ public class NonTransactionalDataAccessTest extends BaseCoreFunctionalTestCase {
} }
} }
@Test
public void testNativeQueryAllowingOutOfTransactionUpdateOperations() throws Exception {
allowUpdateOperationOutsideTransaction = "true";
rebuildSessionFactory();
prepareTest();
try (Session s = openSession()) {
s.createSQLQuery( "delete from MY_ENTITY" ).executeUpdate();
}
}
@Test(expected = TransactionRequiredException.class)
public void testNativeQueryDisallowingOutOfTransactionUpdateOperations() throws Exception {
allowUpdateOperationOutsideTransaction = "false";
rebuildSessionFactory();
prepareTest();
try (Session s = openSession()) {
s.createSQLQuery( "delete from MY_ENTITY" ).executeUpdate();
}
}
@Test(expected = TransactionRequiredException.class) @Test(expected = TransactionRequiredException.class)
public void testFlushDisallowingOutOfTransactionUpdateOperations() throws Exception { public void testFlushDisallowingOutOfTransactionUpdateOperations() throws Exception {
allowUpdateOperationOutsideTransaction = "false"; allowUpdateOperationOutsideTransaction = "false";
@ -113,6 +131,7 @@ public class NonTransactionalDataAccessTest extends BaseCoreFunctionalTestCase {
} }
@Entity(name = "MyEntity") @Entity(name = "MyEntity")
@Table(name = "MY_ENTITY")
public static class MyEntity { public static class MyEntity {
@Id @Id
@GeneratedValue @GeneratedValue
@ -135,20 +154,4 @@ public class NonTransactionalDataAccessTest extends BaseCoreFunctionalTestCase {
this.name = name; this.name = name;
} }
} }
public final Serializable doInsideTransaction(Session s, java.util.function.Supplier<Serializable> sp) {
s.getTransaction().begin();
try {
final Serializable result = sp.get();
s.getTransaction().commit();
return result;
}
catch (Exception e) {
if ( s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
s.getTransaction().rollback();
}
throw e;
}
}
} }