com.atomikos.icatch.jta.UserTransactionManager Java Examples

The following examples show how to use com.atomikos.icatch.jta.UserTransactionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataSourceConfig.java    From mybatis.flying with Apache License 2.0 6 votes vote down vote up
@Bean(name = "springTransactionManager")
// 用户数据源的事务管理器
public JtaTransactionManager accountTxManager() {
	UserTransactionManager atomikosTransactionManager = new UserTransactionManager();
	atomikosTransactionManager.setForceShutdown(true);

	UserTransaction atomikosUserTransaction = new UserTransactionImp();
	try {
		atomikosUserTransaction.setTransactionTimeout(300);
	} catch (SystemException e) {
	}

	JtaTransactionManager springTransactionManager = new org.springframework.transaction.jta.JtaTransactionManager();
	springTransactionManager.setTransactionManager(atomikosTransactionManager);
	springTransactionManager.setUserTransaction(atomikosUserTransaction);
	springTransactionManager.setAllowCustomIsolationLevels(true);

	return springTransactionManager;
}
 
Example #2
Source File: BasicTransactionAssistanceFactoryImpl.java    From genericconnector with Apache License 2.0 6 votes vote down vote up
/** before calling this method, please ensure you have called {@link TransactionConfigurator#setup(String, CommitRollbackCallback)} */
@Override
public TransactionAssistant getTransactionAssistant() throws ResourceException {
	//enlist a new resource into the transaction. it will be delisted, when its closed.
	final CommitRollbackCallback commitRollbackCallback = TransactionConfigurator.getCommitRollbackCallback(jndiName);
	MicroserviceXAResource ms = new MicroserviceXAResource(jndiName, commitRollbackCallback);
	UserTransactionManager utm = getTM();
	try {
		if(utm.getStatus() == Status.STATUS_NO_TRANSACTION){
			throw new ResourceException("no transaction found. please start one before getting the transaction assistant. status was: " + utm.getStatus());
		}
		Transaction tx = utm.getTransaction();
		tx.enlistResource(ms);
		return new AtomikosTransactionAssistantImpl(ms);
	} catch (Exception e) {
		throw new ResourceException("Unable to get transaction status", e);
	}
}
 
Example #3
Source File: BasicTransactionAssistanceFactoryImplTest.java    From genericconnector with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoTX() throws ResourceException, IllegalStateException, RollbackException, SystemException {
	final UserTransactionManager tm = mock(UserTransactionManager.class);
	when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
	
	BasicTransactionAssistanceFactory f = new BasicTransactionAssistanceFactoryImpl("a"){
		@Override
		protected UserTransactionManager getTM() {
			return tm;
		}
	};

	try{
		//TEST
		f.getTransactionAssistant();
		fail("no exception");
	}catch(ResourceException e){
		//OK, expected
	}
}
 
Example #4
Source File: FastDepAtomikosTransactionConfigure.java    From fastdep with Apache License 2.0 5 votes vote down vote up
/**
 * atomikosTransactionManager
 *
 * @return userTransactionManager
 */
@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
public TransactionManager atomikosTransactionManager() {
    UserTransactionManager userTransactionManager = new UserTransactionManager();
    userTransactionManager.setForceShutdown(false);
    return userTransactionManager;
}
 
Example #5
Source File: SupportConfig.java    From jeesupport with MIT License 5 votes vote down vote up
/**
 * atomikos事务管理器,一般情况无需修改
 * @return
 */
@Bean( initMethod = "init", destroyMethod = "close" )
public UserTransactionManager atomikosTM(){
    if( CommonConfig.getBoolean( "jees.jdbs.enable" ) != true ) return null;

    UserTransactionManager utm = new UserTransactionManager();

    utm.setForceShutdown( false );

    log.debug( "--Spring Bean[atomikosTM]初始化." );
    return utm;
}
 
Example #6
Source File: SupportConfig.java    From jeesupport with MIT License 5 votes vote down vote up
/**
 * spring jta 事务管理器,一般情况无需修改
 * @param _utm
 * @param _uti
 * @return
 */
@Bean
public JtaTransactionManager transactionManager(@Qualifier( "atomikosTM" ) UserTransactionManager _utm,
                                       @Qualifier( "atomikosUT" ) UserTransactionImp _uti ){
    JtaTransactionManager jtm = new JtaTransactionManager();

    jtm.setTransactionManager( _utm );
    jtm.setUserTransaction( _uti );

    jtm.setAllowCustomIsolationLevels(
            CommonConfig.getBoolean("jees.jdbs.trans.allowCustomIsolationLevels", false ) );

    log.debug( "--Spring Bean[defaultTM]初始化." );
    return jtm;
}
 
Example #7
Source File: MainConfig.java    From spring-boot-jta-atomikos-sample with Apache License 2.0 5 votes vote down vote up
@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
public TransactionManager atomikosTransactionManager() throws Throwable {
	UserTransactionManager userTransactionManager = new UserTransactionManager();
	userTransactionManager.setForceShutdown(false);

	AtomikosJtaPlatform.transactionManager = userTransactionManager;

	return userTransactionManager;
}
 
Example #8
Source File: CreateUserServlet.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
	TransactionConfigurator.unregisterMicroserviceResourceFactory("xa/bookingSystem");
	TransactionConfigurator.unregisterMicroserviceResourceFactory("xa/letterWriter");

	//dont do this here - see note in #contextInitialized
	new UserTransactionManager().close();
}
 
Example #9
Source File: AtomikosTransactionAssistantImpl.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
	UserTransactionManager utm = getTransactionManager();
	try {
		if(utm.getStatus() == Status.STATUS_NO_TRANSACTION){
			throw new RuntimeException("no transaction found. please start one before getting the transaction assistant. status was: " + utm.getStatus());
		}
		Transaction tx = utm.getTransaction();
		tx.delistResource(ms, ms.getUnderlyingConnection().wasExecuteSuccessful() ? XAResource.TMSUCCESS : XAResource.TMFAIL);
	} catch (Exception e) {
		throw new RuntimeException("Unable to delist resource from transaction", e);
	}
}
 
Example #10
Source File: AtomikosTransactionAssistantImplTest.java    From genericconnector with Apache License 2.0 5 votes vote down vote up
private void testCloseDelistsResource(final int result) throws Exception {
	final UserTransactionManager tm = mock(UserTransactionManager.class);
	final Transaction tx = mock(Transaction.class);
	when(tm.getTransaction()).thenReturn(tx);
	MicroserviceXAResource ms = getMs();
	final AtomicInteger count = new AtomicInteger();
	ms.start(getXid(), 0);
	AtomikosTransactionAssistantImpl impl = new AtomikosTransactionAssistantImpl(ms){
		@Override
		protected UserTransactionManager getTransactionManager() {
			return tm;
		}
	};

	try{
		//TEST
		impl.executeInActiveTransaction(new ExecuteCallback<Void>() {
			@Override
			public Void execute(String txid) throws Exception {
				count.incrementAndGet();
				if(result == XAResource.TMSUCCESS){
					return null; //no exception => TMSUCCESS
				}else{
					throw new Exception(); // => TMFAIL
				}
			}
		});
		if(result == XAResource.TMFAIL) fail("no exception");
	}catch(Exception e){
		if(result == XAResource.TMSUCCESS) fail("exception not expected");
	}

	//TEST
	impl.close();

	assertEquals(1, count.get());
	verify(tx, times(1)).delistResource(eq(ms), eq(result));
}
 
Example #11
Source File: JpaCoreConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "init", destroyMethod = "close")
public UserTransactionManager userTransactionManager() {
    UserTransactionManager utm = new UserTransactionManager();
    utm.setForceShutdown(true);

    return utm;
}
 
Example #12
Source File: AtomikosTransactionAssistantImpl.java    From genericconnector with Apache License 2.0 4 votes vote down vote up
protected UserTransactionManager getTransactionManager() {
	return new UserTransactionManager();
}
 
Example #13
Source File: BasicTransactionAssistanceFactoryImpl.java    From genericconnector with Apache License 2.0 4 votes vote down vote up
protected UserTransactionManager getTM() {
	return new UserTransactionManager();
}
 
Example #14
Source File: XATransactionalSend.java    From hazelcastmq with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
  // Create a two node cluster on localhost. We need to use two separate nodes
  // (or two separate threads) because transactional messages produced can be
  // consumed in the same thread before the transaction is committed. This is
  // because all Hazelcast transactions are thread bound and don't know
  // anything about JMS sessions. Therefore two separate sessions in the same
  // thread will be transactional if either one is transactional.
  Config config = new Config();
  NetworkConfig networkConfig = config.getNetworkConfig();
  networkConfig.setPort(10571);
  networkConfig.getInterfaces().addInterface("127.0.0.1");
  JoinConfig joinConfig = networkConfig.getJoin();
  joinConfig.getMulticastConfig().setEnabled(false);
  joinConfig.getTcpIpConfig().setEnabled(true);
  joinConfig.getTcpIpConfig().addMember("127.0.0.1:10572");
  ClusterNode node1 = new ClusterNode(config);

  config = new Config();
  networkConfig = config.getNetworkConfig();
  networkConfig.setPort(10572);
  networkConfig.getInterfaces().addInterface("127.0.0.1");
  joinConfig = networkConfig.getJoin();
  joinConfig.getMulticastConfig().setEnabled(false);
  joinConfig.getTcpIpConfig().setEnabled(true);
  joinConfig.getTcpIpConfig().addMember("127.0.0.1:10571");
  ClusterNode node2 = new ClusterNode(config);

  try {
    UserTransactionManager tm = new UserTransactionManager();
    tm.setTransactionTimeout(60);
    tm.begin();

    XAHazelcastMQContext context1 = node1.createXAContext();
    tm.getTransaction().enlistResource(context1.getXAResource());

    HazelcastMQProducer producer = context1.createProducer();
    
    HazelcastMQContext context2 = node2.createContext(false);
    HazelcastMQConsumer consumer = context2.createConsumer(queueName);

    // Send the first message while in a transaction.
    producer.send(queueName, "Hello World!".getBytes());

    // Try to consume the message that was sent in another node in a
    // transaction. This node shouldn't be able to see the message.
    HazelcastMQMessage msg = consumer.receive(1, TimeUnit.SECONDS);
    Assert.isNull(msg, "Something went wrong. A transactional message "
        + "shouldn't be visible.");

    // Commit the transaction so all nodes can see the transactional messages.
    log.info("Committing producer transaction 1.");
    tm.commit();

    // Try to consume the message again. This time is should be visible.
    msg = consumer.receive(1, TimeUnit.SECONDS);
    Assert.notNull(msg, "Something went wrong. A message should be visible.");
    log.info("Got message body: " + new String(msg.getBody()));

    // Start a second transaction using the same context.
    tm.begin();
    tm.getTransaction().enlistResource(context1.getXAResource());

    // Send another message in another transaction.
    producer.send(queueName, "Goodbye World!".getBytes());
    log.info("Committing producer transaction 2.");
    tm.commit();

    msg = consumer.receive(1, TimeUnit.SECONDS);
    Assert.notNull(msg, "Something went wrong. A message should be visible.");
    log.info("Got message body: " + new String(msg.getBody()));

    tm.close();
    context1.close();
    context2.close();
  }
  finally {
    node1.shutdown();
    node2.shutdown();
  }
}