Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-14540 Don't share session-scoped interceptors with temp session
- Loading branch information
Showing
5 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...va/org/hibernate/envers/test/integration/tm/SessionFactoryInterceptorTransactionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.envers.test.integration.tm; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Map; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.transaction.TransactionManager; | ||
|
||
import org.hibernate.FlushMode; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; | ||
import org.hibernate.envers.test.Priority; | ||
import org.hibernate.envers.test.entities.StrTestEntity; | ||
import org.hibernate.internal.SessionImpl; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.jta.TestingJtaBootstrap; | ||
import org.hibernate.testing.jta.TestingJtaPlatformImpl; | ||
|
||
/** | ||
* @author Chris Cranford | ||
*/ | ||
public class SessionFactoryInterceptorTransactionTest extends BaseEnversJPAFunctionalTestCase { | ||
|
||
private TestInterceptor interceptor; | ||
private TransactionManager tm; | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { StrTestEntity.class }; | ||
} | ||
|
||
@Override | ||
protected void addConfigOptions(Map options) { | ||
super.addConfigOptions( options ); | ||
|
||
TestInterceptor.reset(); | ||
|
||
this.interceptor = new TestInterceptor(); | ||
options.put( AvailableSettings.INTERCEPTOR, interceptor ); | ||
options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, true ); | ||
|
||
TestingJtaBootstrap.prepare( options ); | ||
tm = TestingJtaPlatformImpl.INSTANCE.getTransactionManager(); | ||
} | ||
|
||
@Test | ||
@Priority(10) | ||
public void initData() throws Exception { | ||
// Revision 1 | ||
EntityManager em = getEntityManager(); | ||
// Explicitly use manual flush to trigger separate temporary session write via Envers | ||
em.unwrap( SessionImpl.class ).setHibernateFlushMode( FlushMode.MANUAL ); | ||
tm.begin(); | ||
StrTestEntity entity = new StrTestEntity( "Test" ); | ||
em.persist( entity ); | ||
em.flush(); | ||
tm.commit(); | ||
} | ||
|
||
@Test | ||
public void testInterceptorInvocations() throws Exception { | ||
// Expect the interceptor to have been created once and invoked twice, once for the original session | ||
// and follow-up for the Envers temporary session. | ||
final Map<TestInterceptor, Integer> invocationMap = TestInterceptor.getBeforeCompletionCallbacks(); | ||
assertEquals( 1, invocationMap.size() ); | ||
assertEquals( invocationMap.values().stream().filter( v -> v == 2 ).count(), 1 ); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...test/java/org/hibernate/envers/test/integration/tm/SessionInterceptorTransactionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.envers.test.integration.tm; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Map; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.transaction.TransactionManager; | ||
|
||
import org.hibernate.FlushMode; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; | ||
import org.hibernate.envers.test.Priority; | ||
import org.hibernate.envers.test.entities.StrTestEntity; | ||
import org.hibernate.internal.SessionImpl; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.jta.TestingJtaBootstrap; | ||
import org.hibernate.testing.jta.TestingJtaPlatformImpl; | ||
|
||
/** | ||
* @author Chris Cranford | ||
*/ | ||
public class SessionInterceptorTransactionTest extends BaseEnversJPAFunctionalTestCase { | ||
|
||
private TransactionManager tm; | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { StrTestEntity.class }; | ||
} | ||
|
||
@Override | ||
protected void addConfigOptions(Map options) { | ||
super.addConfigOptions( options ); | ||
|
||
TestInterceptor.reset(); | ||
|
||
options.put( AvailableSettings.SESSION_SCOPED_INTERCEPTOR, TestInterceptor.class.getName() ); | ||
options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" ); | ||
|
||
TestingJtaBootstrap.prepare( options ); | ||
tm = TestingJtaPlatformImpl.INSTANCE.getTransactionManager(); | ||
} | ||
|
||
@Test | ||
@Priority(10) | ||
public void initData() throws Exception { | ||
// Revision 1 | ||
EntityManager em = getEntityManager(); | ||
// Explicitly use manual flush to trigger separate temporary session write via Envers | ||
em.unwrap( SessionImpl.class ).setHibernateFlushMode( FlushMode.MANUAL ); | ||
tm.begin(); | ||
StrTestEntity entity = new StrTestEntity( "Test" ); | ||
em.persist( entity ); | ||
em.flush(); | ||
tm.commit(); | ||
} | ||
|
||
@Test | ||
public void testInterceptorInvocations() throws Exception { | ||
// The interceptor should only be created once and should only be invoked once. | ||
final Map<TestInterceptor, Integer> invocationMap = TestInterceptor.getBeforeCompletionCallbacks(); | ||
assertEquals( 1, invocationMap.size() ); | ||
assertEquals( invocationMap.values().stream().filter( v -> v == 1 ).count(), 1 ); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
hibernate-envers/src/test/java/org/hibernate/envers/test/integration/tm/TestInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.envers.test.integration.tm; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.hibernate.EmptyInterceptor; | ||
import org.hibernate.Transaction; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* @author Chris Cranford | ||
*/ | ||
public class TestInterceptor extends EmptyInterceptor { | ||
|
||
private static final Logger LOGGER = Logger.getLogger( TestInterceptor.class ); | ||
private static Map<TestInterceptor, Integer> interceptorInvocations = new HashMap<>(); | ||
|
||
public TestInterceptor() { | ||
interceptorInvocations.put( this, 0 ); | ||
} | ||
|
||
@Override | ||
public void beforeTransactionCompletion(Transaction tx) { | ||
super.beforeTransactionCompletion(tx); | ||
interceptorInvocations.put( this, interceptorInvocations.get( this ) + 1 ); | ||
LOGGER.info( "Interceptor beforeTransactionCompletion invoked" ); | ||
} | ||
|
||
public static Map<TestInterceptor, Integer> getBeforeCompletionCallbacks() { | ||
return interceptorInvocations; | ||
} | ||
|
||
public static void reset() { | ||
interceptorInvocations.clear(); | ||
} | ||
} |