Merge remote-tracking branch 'upstream5/master' into wip/6.0_merged_9

This commit is contained in:
Andrea Boriero 2019-10-17 16:02:43 +01:00
commit 8b500acc1d
6 changed files with 273 additions and 13 deletions

View File

@ -957,7 +957,7 @@ public class ActionQueue {
throw he;
}
catch (Exception e) {
throw new AssertionFailure( "Unable to perform beforeTransactionCompletion callback", e );
throw new HibernateException( "Unable to perform beforeTransactionCompletion callback: " + e.getMessage(), e );
}
}
}
@ -987,7 +987,7 @@ public class ActionQueue {
// continue loop
}
catch (Exception e) {
throw new AssertionFailure( "Exception releasing cache locks", e );
throw new HibernateException( "Unable to perform afterTransactionCompletion callback: " + e.getMessage(), e );
}
}

View File

@ -0,0 +1,133 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.actionqueue;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.HibernateException;
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class CustomAfterCompletionTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-13666")
public void success() {
inSession( session -> {
AtomicBoolean called = new AtomicBoolean( false );
session.getActionQueue().registerProcess( new AfterTransactionCompletionProcess() {
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
called.set( true );
}
} );
Assert.assertFalse( called.get() );
inTransaction( session, theSession -> {
theSession.persist( new SimpleEntity( "jack" ) );
} );
Assert.assertTrue( called.get() );
} );
// Check that the transaction was committed
inTransaction( session -> {
long count = session.createQuery( "select count(*) from SimpleEntity", Long.class )
.uniqueResult();
assertEquals( 1L, count );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-13666")
public void failure() {
try {
inSession( session -> {
session.getActionQueue().registerProcess( new AfterTransactionCompletionProcess() {
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
throw new RuntimeException( "My exception" );
}
} );
inTransaction( session, theSession -> {
theSession.persist( new SimpleEntity( "jack" ) );
} );
} );
Assert.fail( "Expected exception to be thrown" );
}
catch (Exception e) {
assertThat( e, instanceOf( HibernateException.class ) );
assertThat( e.getMessage(), containsString( "Unable to perform afterTransactionCompletion callback" ) );
Throwable cause = e.getCause();
assertThat( cause, instanceOf( RuntimeException.class ) );
assertThat( cause.getMessage(), containsString( "My exception" ) );
// Make sure that the original message is appended to the wrapping exception's message, for convenience
assertThat( e.getMessage(), containsString( "My exception" ) );
}
// Check that the transaction was committed
inTransaction( session -> {
long count = session.createQuery( "select count(*) from SimpleEntity", Long.class )
.uniqueResult();
assertEquals( 1L, count );
} );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { SimpleEntity.class };
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Entity(name = "SimpleEntity")
public static class SimpleEntity {
@Id
@GeneratedValue
private Integer id;
private String name;
SimpleEntity() {
}
SimpleEntity(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -0,0 +1,133 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.actionqueue;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.HibernateException;
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class CustomBeforeCompletionTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-13666")
public void success() {
inSession( session -> {
AtomicBoolean called = new AtomicBoolean( false );
session.getActionQueue().registerProcess( new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
called.set( true );
}
} );
Assert.assertFalse( called.get() );
inTransaction( session, theSession -> {
theSession.persist( new SimpleEntity( "jack" ) );
} );
Assert.assertTrue( called.get() );
} );
// Check that the transaction was committed
inTransaction( session -> {
long count = session.createQuery( "select count(*) from SimpleEntity", Long.class )
.uniqueResult();
assertEquals( 1L, count );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-13666")
public void failure() {
try {
inSession( session -> {
session.getActionQueue().registerProcess( new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
throw new RuntimeException( "My exception" );
}
} );
inTransaction( session, theSession -> {
theSession.persist( new SimpleEntity( "jack" ) );
} );
} );
Assert.fail( "Expected exception to be thrown" );
}
catch (Exception e) {
assertThat( e, instanceOf( HibernateException.class ) );
assertThat( e.getMessage(), containsString( "Unable to perform beforeTransactionCompletion callback" ) );
Throwable cause = e.getCause();
assertThat( cause, instanceOf( RuntimeException.class ) );
assertThat( cause.getMessage(), containsString( "My exception" ) );
// Make sure that the original message is appended to the wrapping exception's message, for convenience
assertThat( e.getMessage(), containsString( "My exception" ) );
}
// Check that the transaction was rolled back
inTransaction( session -> {
long count = session.createQuery( "select count(*) from SimpleEntity", Long.class )
.uniqueResult();
assertEquals( 0L, count );
} );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { SimpleEntity.class };
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Entity(name = "SimpleEntity")
public static class SimpleEntity {
@Id
@GeneratedValue
private Integer id;
private String name;
SimpleEntity() {
}
SimpleEntity(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -37,7 +37,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Chris Cranford
*/
public class AfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase {
public class JtaAfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { SimpleEntity.class };
@ -98,10 +98,6 @@ public class AfterCompletionTest extends BaseNonConfigCoreFunctionalTestCase {
}
}
private void registerAfterCallbackCompletionHandler(Session session) {
( (SessionImplementor) session ).getActionQueue().registerProcess( new AfterCallbackCompletionHandler() );
}
public static class BeforeCallbackCompletionHandler implements BeforeTransactionCompletionProcess {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {

View File

@ -34,7 +34,7 @@ import static org.junit.Assert.fail;
/**
* @author Steve Ebersole
*/
public class BeforeCompletionFailureTest extends BaseNonConfigCoreFunctionalTestCase {
public class JtaBeforeCompletionFailureTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
@ -118,10 +118,6 @@ public class BeforeCompletionFailureTest extends BaseNonConfigCoreFunctionalTest
return new SimpleEntity( id, "key", "name" );
}
private void runTest() {
}
@Entity(name = "SimpleEntity")
public static class SimpleEntity {
@Id

View File

@ -31,6 +31,8 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import org.hibernate.test.tm.JtaAfterCompletionTest;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -39,7 +41,7 @@ import static org.junit.Assert.assertTrue;
/**
* An envers specific quest that verifies the {@link AuditProcessManager} gets flushed.
*
* There is a similar test called {@link org.hibernate.test.tm.AfterCompletionTest}
* There is a similar test called {@link JtaAfterCompletionTest}
* in hibernate-core which verifies that the callbacks fires.
*
* The premise behind this test is to verify that when a JTA transaction is aborted by