HHH-5949 - Migrate, complete and integrate TransactionFactory as a service

This commit is contained in:
Steve Ebersole 2011-03-03 14:16:58 -06:00
parent bf186e7a6e
commit 4d5b9f1ca1
12 changed files with 322 additions and 122 deletions

View File

@ -67,7 +67,7 @@ libraries = [
jcl: 'commons-logging:commons-logging:99.0-does-not-exist',
// testing
atomikos: 'com.atomikos:transactions-jdbc:3.6.4',
atomikos: 'com.atomikos:transactions-jdbc:3.7.0',
junit: 'junit:junit:3.8.2',
testng: 'org.testng:testng:5.8:jdk15',
jpa_modelgen: 'org.hibernate:hibernate-jpamodelgen:1.1.0.Final',

View File

@ -32,7 +32,9 @@ import org.hibernate.cache.access.AccessType;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
import org.hibernate.testing.ServiceRegistryBuilder;
/**
* Base class for tests of EntityRegion and CollectionRegion implementations.
@ -65,7 +67,9 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
String entityCfg = "entity";
cfg.setProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, entityCfg);
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry(), cfg, getCacheTestSupport()
ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
supportedAccessTypeTest(regionFactory, cfg.getProperties());
}
@ -85,16 +89,20 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
public void testIsTransactionAware() throws Exception {
Configuration cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, true, false);
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry(), cfg, getCacheTestSupport()
ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
TransactionalDataRegion region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
assertTrue("Region is transaction-aware", region.isTransactionAware());
CacheTestUtil.stopRegionFactory(regionFactory, getCacheTestSupport());
cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, true, false);
// Make it non-transactional
cfg.getProperties().remove(Environment.TRANSACTION_MANAGER_STRATEGY);
cfg.getProperties().remove( JtaPlatformInitiator.JTA_PLATFORM );
regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry(), cfg, getCacheTestSupport()
ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
assertFalse("Region is not transaction-aware", region.isTransactionAware());
@ -104,7 +112,9 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
public void testGetCacheDataDescription() throws Exception {
Configuration cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, true, false);
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
getServiceRegistry(), cfg, getCacheTestSupport()
ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ),
cfg,
getCacheTestSupport()
);
TransactionalDataRegion region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
CacheDataDescription cdd = region.getCacheDataDescription();

View File

@ -43,7 +43,6 @@ import org.hibernate.cache.infinispan.util.CacheAdapter;
import org.hibernate.cache.infinispan.util.FlagAdapter;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.spi.ServiceRegistry;
import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl;
@ -519,7 +518,9 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
private final String configResource;
private final String configName;
private String preferIPv4Stack;
private ServiceRegistry serviceRegistry;
private ServiceRegistry localServiceRegistry;
private ServiceRegistry remoteServiceRegistry;
public AccessStrategyTestSetup(Test test, String configName) {
this(test, configName, null);
@ -539,16 +540,13 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
preferIPv4Stack = System.getProperty(PREFER_IPV4STACK);
System.setProperty(PREFER_IPV4STACK, "true");
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
localCfg = createConfiguration(configName, configResource);
localServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry( localCfg.getProperties() );
localRegionFactory = CacheTestUtil.startRegionFactory( localServiceRegistry, localCfg );
localCfg = createConfiguration(configName, configResource);
localRegionFactory = CacheTestUtil.startRegionFactory( serviceRegistry, localCfg );
remoteCfg = createConfiguration(configName, configResource);
remoteRegionFactory = CacheTestUtil.startRegionFactory(
serviceRegistry,
remoteCfg
);
remoteCfg = createConfiguration(configName, configResource);
remoteServiceRegistry = ServiceRegistryBuilder.buildServiceRegistry( remoteCfg.getProperties() );
remoteRegionFactory = CacheTestUtil.startRegionFactory( remoteServiceRegistry, remoteCfg );
}
@Override
@ -570,8 +568,11 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
remoteRegionFactory.stop();
}
finally {
if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry );
if ( localServiceRegistry != null ) {
ServiceRegistryBuilder.destroy( localServiceRegistry );
}
if ( remoteServiceRegistry != null ) {
ServiceRegistryBuilder.destroy( remoteServiceRegistry );
}
}
}

View File

@ -47,12 +47,12 @@ import org.hibernate.cache.RegionFactory;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaPlatformImpl;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeTestCase;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeConnectionProviderImpl;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl;
import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeTransactionManagerLookup;
import org.hibernate.transaction.TransactionManagerLookup;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
@ -104,12 +104,12 @@ public class ConcurrentWriteTest extends SingleNodeTestCase {
return DualNodeConnectionProviderImpl.class;
}
@Override
protected Class<? extends TransactionManagerLookup> getTransactionManagerLookupClass() {
return DualNodeTransactionManagerLookup.class;
}
@Override
protected Class<? extends JtaPlatform> getJtaPlatform() {
return DualNodeJtaPlatformImpl.class;
}
/**
/**
* test that DB can be queried
*
* @throws java.lang.Exception

View File

@ -9,9 +9,11 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.transaction.spi.TransactionFactory;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import org.hibernate.test.cache.infinispan.tm.JtaPlatformImpl;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.transaction.TransactionManagerLookup;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
@ -28,19 +30,22 @@ public abstract class SingleNodeTestCase extends FunctionalTestCase {
tm = getTransactionManager();
}
protected TransactionManager getTransactionManager() {
try {
if (getTransactionManagerLookupClass() == null)
return null;
else
return getTransactionManagerLookupClass().newInstance().getTransactionManager(null);
} catch (Exception e) {
log.error("Error", e);
throw new RuntimeException(e);
}
}
protected TransactionManager getTransactionManager() {
try {
Class<? extends JtaPlatform> jtaPlatformClass = getJtaPlatform();
if ( jtaPlatformClass == null ) {
return null;
}
else {
return jtaPlatformClass.newInstance().retrieveTransactionManager();
}
}
catch (Exception e) {
log.error("Error", e);
throw new RuntimeException(e);
}
}
public String[] getMappings() {
return new String[] {
"cache/infinispan/functional/Item.hbm.xml",
@ -65,9 +70,9 @@ public abstract class SingleNodeTestCase extends FunctionalTestCase {
return org.hibernate.test.cache.infinispan.tm.XaConnectionProvider.class;
}
protected Class<? extends TransactionManagerLookup> getTransactionManagerLookupClass() {
return org.hibernate.test.cache.infinispan.tm.XaTransactionManagerLookup.class;
}
protected Class<? extends JtaPlatform> getJtaPlatform() {
return JtaPlatformImpl.class;
}
protected boolean getUseQueryCache() {
return true;
@ -79,11 +84,12 @@ public abstract class SingleNodeTestCase extends FunctionalTestCase {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, String.valueOf(getUseQueryCache()));
cfg.setProperty(Environment.CACHE_REGION_FACTORY, getCacheRegionFactory().getName());
cfg.setProperty(Environment.CONNECTION_PROVIDER, getConnectionProviderClass().getName());
if (getTransactionManagerLookupClass() != null) {
cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, getTransactionManagerLookupClass().getName());
}
cfg.setProperty(Environment.TRANSACTION_STRATEGY, getTransactionFactoryClass().getName());
if ( getJtaPlatform() != null ) {
cfg.getProperties().put( JtaPlatformInitiator.JTA_PLATFORM, getJtaPlatform() );
}
cfg.setProperty( Environment.TRANSACTION_STRATEGY, getTransactionFactoryClass().getName() );
cfg.setProperty( Environment.CONNECTION_PROVIDER, getConnectionProviderClass().getName() );
}
protected void beginTx() throws Exception {

View File

@ -37,11 +37,13 @@ import org.hibernate.classic.Session;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.engine.transaction.spi.TransactionFactory;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import org.hibernate.test.cache.infinispan.tm.JtaPlatformImpl;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.test.cache.infinispan.functional.Contact;
import org.hibernate.test.cache.infinispan.functional.Customer;
import org.hibernate.transaction.TransactionManagerLookup;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
@ -82,9 +84,9 @@ public class BulkOperationsTestCase extends FunctionalTestCase {
return org.hibernate.test.cache.infinispan.tm.XaConnectionProvider.class;
}
protected Class<? extends TransactionManagerLookup> getTransactionManagerLookupClass() {
return org.hibernate.test.cache.infinispan.tm.XaTransactionManagerLookup.class;
}
protected JtaPlatform getJtaPlatform() {
return new JtaPlatformImpl();
}
public void configure(Configuration cfg) {
super.configure(cfg);
@ -92,16 +94,16 @@ public class BulkOperationsTestCase extends FunctionalTestCase {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_QUERY_CACHE, "false");
cfg.setProperty(Environment.CACHE_REGION_FACTORY, getCacheRegionFactory().getName());
cfg.setProperty(Environment.CONNECTION_PROVIDER, getConnectionProviderClass().getName());
cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, getTransactionManagerLookupClass().getName());
cfg.setProperty(Environment.TRANSACTION_STRATEGY, getTransactionFactoryClass().getName());
cfg.getProperties().put( JtaPlatformInitiator.JTA_PLATFORM, getJtaPlatform() );
cfg.setProperty(Environment.CONNECTION_PROVIDER, getConnectionProviderClass().getName());
}
public void testBulkOperations() throws Throwable {
log.info("*** testBulkOperations()");
boolean cleanedUp = false;
try {
tm = getTransactionManagerLookupClass().newInstance().getTransactionManager(null);
tm = getJtaPlatform().retrieveTransactionManager();
createContacts();

View File

@ -0,0 +1,87 @@
/*
* 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.cache.infinispan.functional.cluster;
import org.hibernate.HibernateException;
import org.hibernate.TransactionException;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import org.hibernate.service.spi.Configurable;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Map;
/**
* @author Steve Ebersole
*/
public class DualNodeJtaPlatformImpl implements JtaPlatform, Configurable {
private String nodeId;
@Override
public void configure(Map configurationValues) {
nodeId = (String) configurationValues.get( DualNodeTestCase.NODE_ID_PROP );
if ( nodeId == null ) {
throw new HibernateException(DualNodeTestCase.NODE_ID_PROP + " not configured");
}
}
@Override
public TransactionManager retrieveTransactionManager() {
return DualNodeJtaTransactionManagerImpl.getInstance( nodeId );
}
@Override
public UserTransaction retrieveUserTransaction() {
throw new TransactionException( "UserTransaction not used in these tests" );
}
@Override
public Object getTransactionIdentifier(Transaction transaction) {
return transaction;
}
@Override
public boolean canRegisterSynchronization() {
return JtaStatusHelper.isActive( retrieveTransactionManager() );
}
@Override
public void registerSynchronization(Synchronization synchronization) {
try {
retrieveTransactionManager().getTransaction().registerSynchronization( synchronization );
}
catch (Exception e) {
throw new TransactionException( "Could not obtain transaction from TM" );
}
}
@Override
public int getCurrentStatus() throws SystemException {
return JtaStatusHelper.getStatus( retrieveTransactionManager() );
}
}

View File

@ -31,6 +31,7 @@ import org.hibernate.cfg.Mappings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.testing.junit.functional.ExecutionEnvironment;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
@ -134,20 +135,14 @@ public abstract class DualNodeTestCase extends FunctionalTestCase {
return DualNodeConnectionProviderImpl.class;
}
protected Class getTransactionManagerLookupClass() {
return DualNodeTransactionManagerLookup.class;
protected Class getJtaPlatformClass() {
return DualNodeJtaPlatformImpl.class;
}
protected Class getTransactionFactoryClass() {
return CMTTransactionFactory.class;
}
/**
* Apply any node-specific configurations to our first node.
*
* @param the
* Configuration to update.
*/
protected void configureFirstNode(Configuration cfg) {
cfg.setProperty(NODE_ID_PROP, LOCAL);
}
@ -156,12 +151,6 @@ public abstract class DualNodeTestCase extends FunctionalTestCase {
return Collections.singletonMap( NODE_ID_FIELD, LOCAL );
}
/**
* Apply any node-specific configurations to our second node.
*
* @param the
* Configuration to update.
*/
protected void configureSecondNode(Configuration cfg) {
cfg.setProperty(NODE_ID_PROP, REMOTE);
}
@ -187,7 +176,7 @@ public abstract class DualNodeTestCase extends FunctionalTestCase {
super.configure(cfg);
cfg.setProperty(Environment.CONNECTION_PROVIDER, getConnectionProviderClass().getName());
cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, getTransactionManagerLookupClass().getName());
cfg.setProperty( JtaPlatformInitiator.JTA_PLATFORM, getJtaPlatformClass().getName());
cfg.setProperty(Environment.TRANSACTION_STRATEGY, getTransactionFactoryClass().getName());
cfg.setProperty(Environment.CACHE_REGION_FACTORY, getCacheRegionFactory().getName());
cfg.setProperty(Environment.USE_QUERY_CACHE, String.valueOf(getUseQueryCache()));

View File

@ -0,0 +1,74 @@
/*
* 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.cache.infinispan.tm;
import org.hibernate.TransactionException;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
/**
* @author Steve Ebersole
*/
public class JtaPlatformImpl implements JtaPlatform {
@Override
public TransactionManager retrieveTransactionManager() {
return XaTransactionManagerImpl.getInstance();
}
@Override
public UserTransaction retrieveUserTransaction() {
throw new TransactionException( "UserTransaction not used in these tests" );
}
@Override
public Object getTransactionIdentifier(Transaction transaction) {
return transaction;
}
@Override
public boolean canRegisterSynchronization() {
return JtaStatusHelper.isActive( XaTransactionManagerImpl.getInstance() );
}
@Override
public void registerSynchronization(Synchronization synchronization) {
try {
XaTransactionManagerImpl.getInstance().getTransaction().registerSynchronization( synchronization );
}
catch (Exception e) {
throw new TransactionException( "Could not obtain transaction from TM" );
}
}
@Override
public int getCurrentStatus() throws SystemException {
return JtaStatusHelper.getStatus( XaTransactionManagerImpl.getInstance() );
}
}

View File

@ -1,52 +0,0 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat, Inc. and/or it's affiliates, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.hibernate.test.cache.infinispan.tm;
import java.util.Properties;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import org.hibernate.HibernateException;
import org.hibernate.transaction.TransactionManagerLookup;
/**
* XaResourceCapableTransactionManagerLookup.
*
* @author Galder Zamarreño
* @since 3.5
*/
public class XaTransactionManagerLookup implements TransactionManagerLookup {
public Object getTransactionIdentifier(Transaction transaction) {
return transaction;
}
public TransactionManager getTransactionManager(Properties props) throws HibernateException {
return XaTransactionManagerImpl.getInstance();
}
public String getUserTransactionName() {
throw new UnsupportedOperationException( "jndi currently not implemented for these tests" );
}
}

View File

@ -0,0 +1,82 @@
/*
* 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.cache.infinispan.util;
import org.infinispan.transaction.tm.BatchModeTransactionManager;
import org.hibernate.HibernateException;
import org.hibernate.TransactionException;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.service.jta.platform.spi.JtaPlatform;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
/**
* @author Steve Ebersole
*/
public class BatchModeJtaPlatform implements JtaPlatform {
@Override
public TransactionManager retrieveTransactionManager() {
try {
return BatchModeTransactionManager.getInstance();
}
catch (Exception e) {
throw new HibernateException("Failed getting BatchModeTransactionManager", e);
}
}
@Override
public UserTransaction retrieveUserTransaction() {
throw new UnsupportedOperationException();
}
@Override
public Object getTransactionIdentifier(Transaction transaction) {
return transaction;
}
@Override
public boolean canRegisterSynchronization() {
return JtaStatusHelper.isActive( retrieveTransactionManager() );
}
@Override
public void registerSynchronization(Synchronization synchronization) {
try {
retrieveTransactionManager().getTransaction().registerSynchronization( synchronization );
}
catch (Exception e) {
throw new TransactionException( "Could not obtain transaction from TM" );
}
}
@Override
public int getCurrentStatus() throws SystemException {
return JtaStatusHelper.getStatus( retrieveTransactionManager() );
}
}

View File

@ -37,6 +37,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Settings;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.jta.platform.internal.JtaPlatformInitiator;
import org.hibernate.service.spi.ServiceRegistry;
/**
@ -50,7 +51,7 @@ public class CacheTestUtil {
Configuration cfg = new Configuration();
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.USE_STRUCTURED_CACHE, "true");
cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, BatchModeTransactionManagerLookup.class.getName());
cfg.setProperty( JtaPlatformInitiator.JTA_PLATFORM, BatchModeJtaPlatform.class.getName() );
cfg.setProperty(Environment.CACHE_REGION_FACTORY, regionFactory.getName());
cfg.setProperty(Environment.CACHE_REGION_PREFIX, regionPrefix);