Search in sources :

Example 36 with NotSupportedException

use of javax.transaction.NotSupportedException in project ofbiz-framework by apache.

the class TransactionUtil method begin.

/**
 * Begins a transaction in the current thread IF transactions are available; only
 * tries if the current transaction status is ACTIVE, if not active it returns false.
 * If and on only if it begins a transaction it will return true. In other words, if
 * a transaction is already in place it will return false and do nothing.
 */
public static boolean begin(int timeout) throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();
    if (ut != null) {
        try {
            int currentStatus = ut.getStatus();
            if (Debug.verboseOn()) {
                Debug.logVerbose("Current status : " + getTransactionStateString(currentStatus), module);
            }
            if (currentStatus == Status.STATUS_ACTIVE) {
                if (Debug.verboseOn()) {
                    Debug.logVerbose("Active transaction in place, so no transaction begun", module);
                }
                return false;
            } else if (currentStatus == Status.STATUS_MARKED_ROLLBACK) {
                Exception e = getTransactionBeginStack();
                if (e != null) {
                    Debug.logWarning(e, "Active transaction marked for rollback in place, so no transaction begun; this stack trace shows when the exception began: ", module);
                } else {
                    Debug.logWarning("Active transaction marked for rollback in place, so no transaction begun", module);
                }
                RollbackOnlyCause roc = getSetRollbackOnlyCause();
                // do we have a cause? if so, throw special exception
                if (UtilValidate.isNotEmpty(roc)) {
                    throw new GenericTransactionException("The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: " + roc.getCauseMessage(), roc.getCauseThrowable());
                }
                return false;
            }
            internalBegin(ut, timeout);
            // reset the transaction stamps, just in case...
            clearTransactionStamps();
            // initialize the start stamp
            getTransactionStartStamp();
            // set the tx begin stack placeholder
            setTransactionBeginStack();
            // initialize the debug resource
            if (debugResources()) {
                DebugXaResource dxa = new DebugXaResource();
                try {
                    dxa.enlist();
                } catch (XAException e) {
                    Debug.logError(e, module);
                }
            }
            return true;
        } catch (NotSupportedException e) {
            throw new GenericTransactionException("Not Supported error, could not begin transaction (probably a nesting problem)", e);
        } catch (SystemException e) {
            throw new GenericTransactionException("System error, could not begin transaction", e);
        }
    }
    if (Debug.infoOn()) {
        Debug.logInfo("No user transaction, so no transaction begun", module);
    }
    return false;
}
Also used : UserTransaction(javax.transaction.UserTransaction) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) NotSupportedException(javax.transaction.NotSupportedException) GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Example 37 with NotSupportedException

use of javax.transaction.NotSupportedException in project narayana by jbosstm.

the class JDBC2Test method testBC.

@Test
public void testBC() throws SystemException, NotSupportedException, SQLException, HeuristicRollbackException, HeuristicMixedException, RollbackException {
    try (Statement stmt = conn.createStatement()) {
        try {
            stmt.executeUpdate("DROP TABLE test_table");
        } catch (Exception e) {
        // Ignore
        } finally {
            stmt.executeUpdate("CREATE TABLE test_table (a INTEGER,b INTEGER)");
        }
    }
    javax.transaction.TransactionManager tx = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tx.begin();
    TransactionSynchronizationRegistryImple transactionSynchronizationRegistryImple = new TransactionSynchronizationRegistryImple();
    transactionSynchronizationRegistryImple.registerInterposedSynchronization(new Synchronization() {

        @Override
        public void beforeCompletion() {
            try {
                ResultSet res = conn.prepareStatement("SELECT * FROM test_table").executeQuery();
                int rowCount1 = 0;
                while (res.next()) {
                    rowCount1++;
                }
                conn.createStatement().execute("INSERT INTO test_table (a, b) VALUES (1,2)");
                res = conn.prepareStatement("SELECT * FROM test_table").executeQuery();
                int rowCount2 = 0;
                while (res.next()) {
                    rowCount2++;
                }
                conn.close();
                if (rowCount2 != rowCount1 + 1) {
                    fail("Number of rows = " + rowCount2 + ", test was expecting " + rowCount1 + 1);
                }
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }

        @Override
        public void afterCompletion(int status) {
        }
    });
    tx.commit();
}
Also used : TransactionSynchronizationRegistryImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Synchronization(javax.transaction.Synchronization) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) Test(org.junit.Test)

Example 38 with NotSupportedException

use of javax.transaction.NotSupportedException in project narayana by jbosstm.

the class JTATransactionCommitTest2 method test.

@Test
public void test() throws Exception {
    TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
    tm.begin();
    Transaction tx = tm.getTransaction();
    tx.commit();
    assertEquals(Status.STATUS_COMMITTED, tm.getStatus());
    try {
        tm.begin();
        fail("Begin call completed successfully - this shouldn't have happened");
    } catch (NotSupportedException e) {
        System.out.println("NotSupportedException \"" + e.getMessage() + "\" occurred this is expected and correct");
    }
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) NotSupportedException(javax.transaction.NotSupportedException) Test(org.junit.Test)

Example 39 with NotSupportedException

use of javax.transaction.NotSupportedException in project narayana by jbosstm.

the class BaseTransaction method begin.

public void begin() throws javax.transaction.NotSupportedException, javax.transaction.SystemException {
    if (jtaLogger.logger.isTraceEnabled()) {
        jtaLogger.logger.trace("BaseTransaction.begin");
    }
    if (!_supportSubtransactions) {
        try {
            checkTransactionState();
        } catch (IllegalStateException e1) {
            NotSupportedException notSupportedException = new NotSupportedException(e1.getMessage());
            notSupportedException.initCause(e1);
            throw notSupportedException;
        } catch (Exception e2) {
            javax.transaction.SystemException systemException = new javax.transaction.SystemException(e2.toString());
            systemException.initCause(e2);
            throw systemException;
        }
    }
    Integer value = _timeouts.get();
    // if not set then assume 0. What else can we do?
    int v = 0;
    if (value != null) {
        v = value.intValue();
    } else
        v = TxControl.getDefaultTimeout();
    // TODO set default timeout
    TransactionImple.putTransaction(new TransactionImple(v));
}
Also used : NotSupportedException(javax.transaction.NotSupportedException) NotSupportedException(javax.transaction.NotSupportedException) InvalidTransactionException(javax.transaction.InvalidTransactionException)

Example 40 with NotSupportedException

use of javax.transaction.NotSupportedException in project narayana by jbosstm.

the class Test03 method main.

public static void main(String[] args) {
    Setup orbClass = null;
    try {
        boolean needOrb = true;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-local")) {
                needOrb = false;
            }
        }
        if (needOrb) {
            Class c = Thread.currentThread().getContextClassLoader().loadClass("org.jboss.jbossts.qa.Utils.OrbSetup");
            orbClass = (Setup) c.newInstance();
            orbClass.start(args);
        }
        boolean correct = true;
        javax.transaction.TransactionManager transactionManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
        correct = correct && (transactionManager.getTransaction() == null);
        correct = correct && (transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION);
        transactionManager.begin();
        correct = correct && (transactionManager.getTransaction() != null);
        correct = correct && (transactionManager.getStatus() == Status.STATUS_ACTIVE);
        try {
            transactionManager.begin();
            correct = false;
        } catch (NotSupportedException notSupportedException) {
        }
        correct = correct && (transactionManager.getTransaction() != null);
        correct = correct && (transactionManager.getStatus() == Status.STATUS_ACTIVE);
        transactionManager.rollback();
        correct = correct && (transactionManager.getTransaction() == null);
        correct = correct && (transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION);
        if (correct) {
            System.out.println("Passed");
        } else {
            System.out.println("Failed");
        }
    } catch (Exception exception) {
        System.out.println("Failed");
        System.err.print("Test03.main: ");
        exception.printStackTrace(System.err);
    } catch (Error error) {
        System.out.println("Failed");
        System.err.print("Test03.main: ");
        error.printStackTrace(System.err);
    }
    try {
        if (orbClass != null) {
            orbClass.stop();
        }
    } catch (Exception exception) {
        System.err.print("Test03.main: ");
        exception.printStackTrace(System.err);
    } catch (Error error) {
        System.err.print("Test03.main: ");
        error.printStackTrace(System.err);
    }
}
Also used : Setup(org.jboss.jbossts.qa.Utils.Setup) NotSupportedException(javax.transaction.NotSupportedException) NotSupportedException(javax.transaction.NotSupportedException)

Aggregations

NotSupportedException (javax.transaction.NotSupportedException)53 SystemException (javax.transaction.SystemException)42 RollbackException (javax.transaction.RollbackException)22 HeuristicMixedException (javax.transaction.HeuristicMixedException)20 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)20 Transaction (javax.transaction.Transaction)17 UserTransaction (javax.transaction.UserTransaction)11 TransactionManager (javax.transaction.TransactionManager)10 SQLException (java.sql.SQLException)8 NamingException (javax.naming.NamingException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 InvalidTransactionException (javax.transaction.InvalidTransactionException)5 Connection (java.sql.Connection)4 GeneralException (org.apache.openjpa.util.GeneralException)4 File (java.io.File)3 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2