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;
}
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();
}
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");
}
}
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));
}
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);
}
}
Aggregations