Java Code Examples for javax.transaction.UserTransaction#getStatus()

The following examples show how to use javax.transaction.UserTransaction#getStatus() . 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: SchedulerPluginWithUserTransactionSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * If the given UserTransaction is not null, it is committed/rolledback,
 * and then returned to the UserTransactionHelper.
 */
private void resolveUserTransaction(UserTransaction userTransaction) {
    if (userTransaction != null) {
        try {
            if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                userTransaction.rollback();
            } else {
                userTransaction.commit();
            } 
        } catch (Throwable t) {
            getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t);
        } finally {
            UserTransactionHelper.returnUserTransaction(userTransaction);
        }
    }
}
 
Example 2
Source File: SchedulerPluginWithUserTransactionSupport.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * If the given UserTransaction is not null, it is committed/rolledback,
 * and then returned to the UserTransactionHelper.
 */
private void resolveUserTransaction(UserTransaction userTransaction) {
    if (userTransaction != null) {
        try {
            if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
                userTransaction.rollback();
            } else {
                userTransaction.commit();
            } 
        } catch (Throwable t) {
            getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t);
        } finally {
            UserTransactionHelper.returnUserTransaction(userTransaction);
        }
    }
}
 
Example 3
Source File: BeanManagedUserTransactionStrategy.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
protected void applyTransactionTimeout()
{
    Integer transactionTimeout = getDefaultTransactionTimeoutInSeconds();

    if (transactionTimeout == null)
    {
        //the default configured for the container will be used
        return;
    }

    try
    {
        UserTransaction userTransaction = resolveUserTransaction();

        if (userTransaction != null && userTransaction.getStatus() != Status.STATUS_ACTIVE)
        {
            userTransaction.setTransactionTimeout(transactionTimeout);
        }
    }
    catch (SystemException e)
    {
        LOGGER.log(Level.WARNING, "UserTransaction#setTransactionTimeout failed", e);
    }
}
 
Example 4
Source File: JtaStatusHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Extract the status code from a {@link UserTransaction}
 *
 * @param userTransaction The {@link UserTransaction} from which to extract the status.
 *
 * @return The transaction status
 *
 * @throws TransactionException If the {@link UserTransaction} reports the status as unknown
 */
public static int getStatus(UserTransaction userTransaction) {
	try {
		final int status = userTransaction.getStatus();
		if ( status == Status.STATUS_UNKNOWN ) {
			throw new TransactionException( "UserTransaction reported transaction status as unknown" );
		}
		return status;
	}
	catch ( SystemException se ) {
		throw new TransactionException( "Could not determine transaction status", se );
	}
}
 
Example 5
Source File: TransactionUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Gets the status of the transaction in the current thread IF
 * transactions are available, otherwise returns STATUS_NO_TRANSACTION */
public static int getStatus() throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();
    if (ut != null) {
        try {
            return ut.getStatus();
        } catch (SystemException e) {
            throw new GenericTransactionException("System error, could not get status", e);
        }
    }
    return STATUS_NO_TRANSACTION;
}
 
Example 6
Source File: JtaTransactionHelper.java    From snakerflow with Apache License 2.0 5 votes vote down vote up
public static int getUserTransactionStatus(UserTransaction userTransaction) {
	int status = -1;
	try {
		status = userTransaction.getStatus();
	} catch (SystemException e) {
		throw new SnakerException("无法获取事务状态:" + e.getMessage(), e);
	}
	return status;
}
 
Example 7
Source File: UserTransactionFactoryBeanTest.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Test
public void testGetObject_Null() throws Exception {
    Jta.configure(null, null);
    UserTransaction userTransaction = userTransactionFactoryBean.getObject();

    // should not be null, but should be a proxy to a non-existent transaction manager
    assertNotNull(userTransaction);
    assertTrue(Proxy.isProxyClass(userTransaction.getClass()));

    try {
        userTransaction.getStatus();
        fail("IllegalStateException should have been thrown");
    } catch (IllegalStateException e) {}
}
 
Example 8
Source File: AlfrescoTxDiskDriver.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
     * End an active transaction
     * 
     * @param sess SrvSession
     * @param tx Object
     */
    public void endTransaction(SrvSession sess, Object tx) {

      // Check that the transaction object is valid
      
      if ( tx == null)
        return;
      
      // Get the filesystem transaction
      
      FilesysTransaction filesysTx = (FilesysTransaction) tx;

      // Check if there is an active transaction
      
      if ( filesysTx != null && filesysTx.hasTransaction())
      {
        // Get the active transaction
        
          UserTransaction ftx = filesysTx.getTransaction();
          
          try
          {
              // Commit or rollback the transaction
              
              if ( ftx.getStatus() == Status.STATUS_MARKED_ROLLBACK ||
            	   ftx.getStatus() == Status.STATUS_ROLLEDBACK ||
            	   ftx.getStatus() == Status.STATUS_ROLLING_BACK)
              {
                  // Transaction is marked for rollback
                  
                  ftx.rollback();
                  
                  // DEBUG
                  
                  if ( logger.isDebugEnabled())
                      logger.debug("End transaction (rollback)");
              }
              else
              {
            	  // Commit the transaction
                  
                  ftx.commit();
                  
                  // DEBUG
                  
                  if ( logger.isDebugEnabled())
                      logger.debug("End transaction (commit)");
              }
          }
          catch ( Exception ex)
          {
        	  if ( logger.isDebugEnabled())
        		  logger.debug("Failed to end transaction, " + ex.getMessage());
//              throw new AlfrescoRuntimeException("Failed to end transaction", ex);
          }
          finally
          {
              // Clear the current transaction
              
              sess.clearTransaction();
          }
        }
    }
 
Example 9
Source File: CacheTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testTransactionalCacheDisableSharedCaches() throws Throwable
{
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_THREE, NEW_GLOBAL_THREE, null);
    
    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();
    try
    {
        // begin a transaction
        txn.begin();
        
        // Go directly past ALL shared caches
        transactionalCache.setDisableSharedCacheReadForTransaction(true);
        
        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache", transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should not be in any cache", transactionalCache.get(UPDATE_TXN_THREE));
        
        // Update the transactional caches
        transactionalCache.put(NEW_GLOBAL_TWO, "An update");
        transactionalCache.put(UPDATE_TXN_THREE, UPDATE_TXN_THREE);
        
        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache", transactionalCache.get(NEW_GLOBAL_ONE));
        assertEquals("Value should be in transactional cache", "An update", transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("Value should be in transactional cache", UPDATE_TXN_THREE, transactionalCache.get(UPDATE_TXN_THREE));
        
        txn.commit();
        
        // Now check that values were not written through for any caches
        assertEquals("Out-of-txn read must return shared value", NEW_GLOBAL_ONE, transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should be removed from shared cache", transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("New values must be written to shared cache", UPDATE_TXN_THREE, transactionalCache.get(UPDATE_TXN_THREE));
    }
    catch (Throwable e)
    {
        if (txn.getStatus() == Status.STATUS_ACTIVE)
        {
            txn.rollback();
        }
        throw e;
    }
}
 
Example 10
Source File: CacheTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testTransactionalCacheStatsOnCommit() throws Throwable
{
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test1", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test2", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test3", "v", null);
    
    
    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();
    
    final long hitsAtStart = cacheStats.count("transactionalCache", OpType.GET_HIT);
    final long missesAtStart = cacheStats.count("transactionalCache", OpType.GET_MISS);
    final long putsAtStart = cacheStats.count("transactionalCache", OpType.PUT);
    final long removesAtStart = cacheStats.count("transactionalCache", OpType.REMOVE);
    final long clearsAtStart = cacheStats.count("transactionalCache", OpType.CLEAR);
    
    try
    {
        // begin a transaction
        txn.begin();
        
        // Perform some puts
        transactionalCache.put("stats-test4", "v");
        transactionalCache.put("stats-test5", "v");
        transactionalCache.put("stats-test6", "v");
        transactionalCache.put("stats-test7", "v");
        transactionalCache.put("stats-test8", "v");

        // Perform some gets...
        // hits
        transactionalCache.get("stats-test3");
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // repeated hits won't touch the shared cache
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // misses - not yet committed
        transactionalCache.get("stats-miss1");
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");
        transactionalCache.get("stats-miss4");
        // repeated misses won't touch the shared cache
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");

        // Perform some removals
        transactionalCache.remove("stats-test1");
        transactionalCache.remove("stats-test2");
        transactionalCache.remove("stats-test3");
        transactionalCache.remove("stats-test9");
        transactionalCache.remove("stats-test10");
        transactionalCache.remove("stats-test11");
        transactionalCache.remove("stats-test12");
        transactionalCache.remove("stats-test13");
        
        // Check nothing has changed yet - changes not written through until commit or rollback
        assertEquals(hitsAtStart, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(removesAtStart, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(clearsAtStart, cacheStats.count("transactionalCache", OpType.CLEAR));
        
        // commit the transaction
        txn.commit();

        // TODO: remove is called twice for each remove (in beforeCommit and afterCommit) - check this is correct.
        assertEquals(removesAtStart+16, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(hitsAtStart+3, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart+4, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart+5, cacheStats.count("transactionalCache", OpType.PUT));
        // Performing a clear would affect the other stats, so a separate test is required.
        assertEquals(clearsAtStart+0, cacheStats.count("transactionalCache", OpType.CLEAR));
    }
    catch (Throwable e)
    {
        if (txn.getStatus() == Status.STATUS_ACTIVE)
        {
            txn.rollback();
        }
        throw e;
    }
}
 
Example 11
Source File: CacheTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testTransactionalCacheStatsForClears() throws Throwable
{
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test1", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test2", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test3", "v", null);
    
    
    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();
    
    final long hitsAtStart = cacheStats.count("transactionalCache", OpType.GET_HIT);
    final long missesAtStart = cacheStats.count("transactionalCache", OpType.GET_MISS);
    final long putsAtStart = cacheStats.count("transactionalCache", OpType.PUT);
    final long removesAtStart = cacheStats.count("transactionalCache", OpType.REMOVE);
    final long clearsAtStart = cacheStats.count("transactionalCache", OpType.CLEAR);
    
    try
    {
        // begin a transaction
        txn.begin();
        
        // Perform some puts
        transactionalCache.put("stats-test4", "v");
        transactionalCache.put("stats-test5", "v");
        transactionalCache.put("stats-test6", "v");
        transactionalCache.put("stats-test7", "v");
        transactionalCache.put("stats-test8", "v");

        // Perform some gets...
        // hits
        transactionalCache.get("stats-test3");
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // repeated hits won't touch the shared cache
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // misses - not yet committed
        transactionalCache.get("stats-miss1");
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");
        transactionalCache.get("stats-miss4");
        // repeated misses won't touch the shared cache
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");

        // Perform some removals
        transactionalCache.remove("stats-test1");
        transactionalCache.remove("stats-test2");
        transactionalCache.remove("stats-test3");
        transactionalCache.remove("stats-test9");
        transactionalCache.remove("stats-test10");
        transactionalCache.remove("stats-test11");
        transactionalCache.remove("stats-test12");
        transactionalCache.remove("stats-test13");
        
        // Perform some clears
        transactionalCache.clear();
        transactionalCache.clear();
        
        // Check nothing has changed yet - changes not written through until commit or rollback
        assertEquals(hitsAtStart, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(removesAtStart, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(clearsAtStart, cacheStats.count("transactionalCache", OpType.CLEAR));
        
        // commit the transaction
        txn.commit();

        assertEquals(clearsAtStart+2, cacheStats.count("transactionalCache", OpType.CLEAR));
        // There are no removes or puts propagated to the shared cache, as a result of the clears.
        assertEquals(removesAtStart+0, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(putsAtStart+0, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(hitsAtStart+3, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart+4, cacheStats.count("transactionalCache", OpType.GET_MISS));
    }
    catch (Throwable e)
    {
        if (txn.getStatus() == Status.STATUS_ACTIVE)
        {
            txn.rollback();
        }
        throw e;
    }
}
 
Example 12
Source File: AbstractCacheJtaSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testJtaTwoCaches() throws Exception {
    UserTransaction jtaTx = jotm.getUserTransaction();

    IgniteEx ignite = grid(0);

    IgniteCache<String, Integer> cache1 = jcache();

    IgniteCache<Object, Object> cache2 = ignite.cache("cache-2");

    assertNull(ignite.transactions().tx());

    jtaTx.begin();

    try {
        cache1.put("key", 0);
        cache2.put("key", 0);
        cache1.put("key1", 1);
        cache2.put("key2", 2);

        assertEquals(0, (int)cache1.get("key"));
        assertEquals(0, (int)cache2.get("key"));
        assertEquals(1, (int)cache1.get("key1"));
        assertEquals(2, (int)cache2.get("key2"));

        assertEquals(ignite.transactions().tx().state(), ACTIVE);

        jtaTx.commit();

        assertNull(ignite.transactions().tx());

        assertEquals(0, (int)cache1.get("key"));
        assertEquals(0, (int)cache2.get("key"));
        assertEquals(1, (int)cache1.get("key1"));
        assertEquals(2, (int)cache2.get("key2"));
    }
    finally {
        if (jtaTx.getStatus() == Status.STATUS_ACTIVE)
            jtaTx.rollback();
    }

    assertEquals(0, (int)cache1.get("key"));
    assertEquals(0, (int)cache2.get("key"));
    assertEquals(1, (int)cache1.get("key1"));
    assertEquals(2, (int)cache2.get("key2"));
}
 
Example 13
Source File: UserTransactionMock.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
public UserTransactionMock(UserTransaction userTransactionMock) throws SystemException {
    this.status = userTransactionMock.getStatus();
}