/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.test.jpa.txn; import org.junit.Test; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.engine.transaction.spi.TransactionImplementor; import org.hibernate.test.jpa.AbstractJPATest; import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * @author Steve Ebersole */ public class TransactionJoiningTest extends AbstractJPATest { @Override public void configure(Configuration cfg) { super.configure( cfg ); TestingJtaBootstrap.prepare( cfg.getProperties() ); cfg.setProperty( Environment.TRANSACTION_STRATEGY, CMTTransactionFactory.class.getName() ); } @Test public void testExplicitJoining() throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); SessionImplementor session = (SessionImplementor) sessionFactory().withOptions().autoJoinTransactions( false ).openSession(); TransactionImplementor transaction = (TransactionImplementor) ( (Session) session ).getTransaction(); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( transaction.isParticipating() ); session.getFlushMode(); // causes a call to TransactionCoordinator#pulse assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( transaction.isParticipating() ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); assertTrue( transaction.isActive() ); assertFalse( transaction.isParticipating() ); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); session.getFlushMode(); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); assertTrue( transaction.isActive() ); assertFalse( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertFalse( transaction.isParticipating() ); transaction.markForJoin(); transaction.join(); session.getFlushMode(); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); assertTrue( transaction.isActive() ); assertTrue( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertTrue( transaction.isParticipating() ); ( (Session) session ).close(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); } @Test public void testImplicitJoining() throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); SessionImplementor session = (SessionImplementor) sessionFactory().withOptions().autoJoinTransactions( false ).openSession(); session.getFlushMode(); } @Test public void control() throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); assertTrue( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); SessionImplementor session = (SessionImplementor) sessionFactory().openSession(); TransactionImplementor transaction = (TransactionImplementor) ( (Session) session ).getTransaction(); assertTrue( session.getTransactionCoordinator().isSynchronizationRegistered() ); assertTrue( transaction.isParticipating() ); ( (Session) session ).close(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); } }