List of usage examples for javax.transaction TransactionManager suspend
public Transaction suspend() throws SystemException;
From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java
public void testSyncRepl() throws Exception { Integer age;//from w w w .j a va 2 s . c om Transaction tx; cache1.getConfiguration().setSyncCommitPhase(true); cache2.getConfiguration().setSyncCommitPhase(true); TransactionManager mgr = beginTransaction(); cache1.put("/a/b/c", "age", 38); tx = mgr.suspend(); assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age")); // log.debug("cache1: locks held before commit: " // + CachePrinter.printCacheLockingInfo(cache1)); // log.debug("cache2: locks held before commit: " // + CachePrinter.printCacheLockingInfo(cache2)); mgr.resume(tx); mgr.commit(); // log.debug("cache1: locks held after commit: " // + CachePrinter.printCacheLockingInfo(cache1)); // log.debug("cache2: locks held after commit: " // + CachePrinter.printCacheLockingInfo(cache2)); // value on cache2 must be 38 age = (Integer) cache2.get("/a/b/c", "age"); assertNotNull("\"age\" obtained from cache2 must be non-null ", age); assertTrue("\"age\" must be 38", age == 38); }
From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java
/** * Test for JBCACHE-361 -- does marking a tx on the remote side rollback-only * cause a rollback on the originating side? *///from w w w . j av a 2 s .com public void testSyncReplWithRemoteRollback() throws Exception { TransactionManager tm; final Fqn NODE1 = Fqn.fromString("/one/two/three"); cache1.getConfiguration().setSyncRollbackPhase(true); cache2.getConfiguration().setSyncRollbackPhase(true); // Test with a rollback on the remote side // listener aborts any active tx // TransactionAborterListener tal = new TransactionAborterListener(cache2); tm = beginTransaction(); cache1.put(NODE1, "age", 38); // instead of a listener lets just get a WL on ROOT on cache2. And hold on // to it. Transaction tx = tm.suspend(); tm.begin(); cache2.getRoot().put("x", "y"); Transaction tx2 = cache2.getTransactionManager().suspend(); tm.resume(tx); try { tm.commit(); fail("commit should throw a RollbackException, we should not get here"); } catch (RollbackException rollback) { } finally { tm.resume(tx2); tm.rollback(); } // Sleep, as the commit call to cache2 is async Thread.sleep(1000); // assertNull(tal.getCallbackException()); assertEquals(0, cache1.getNumberOfLocksHeld()); assertEquals(0, cache2.getNumberOfLocksHeld()); assertEquals(0, cache1.getNumberOfNodes()); assertEquals(0, cache2.getNumberOfNodes()); }
From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java
public static Transaction suspend() throws GenericTransactionException { try {/*from w w w. j a va 2 s . c o m*/ if (TransactionUtil.getStatus() != STATUS_NO_TRANSACTION) { TransactionManager txMgr = TransactionFactoryLoader.getInstance().getTransactionManager(); if (txMgr != null) { pushTransactionBeginStackSave(clearTransactionBeginStack()); pushSetRollbackOnlyCauseSave(clearSetRollbackOnlyCause()); Transaction trans = txMgr.suspend(); pushSuspendedTransaction(trans); return trans; } else { return null; } } else { Debug.logWarning("No transaction in place, so not suspending.", module); return null; } } catch (SystemException e) { throw new GenericTransactionException("System error, could not suspend transaction", e); } }
From source file:org.eclipse.ecr.automation.core.operations.execution.RunInNewTransaction.java
@OperationMethod public void run() throws Exception { // if the transaction was already marked for rollback, do nothing if (TransactionHelper.isTransactionMarkedRollback()) { return;//from w w w . j av a2 s .c o m } Map<String, Object> vars = isolate ? new HashMap<String, Object>(ctx.getVars()) : ctx.getVars(); OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars); subctx.setInput(ctx.getInput()); final TransactionManager transactionManager = TransactionHelper.lookupTransactionManager(); final Transaction globalTx = transactionManager.suspend(); try { TransactionHelper.startTransaction(); try { service.run(subctx, chainId); } catch (Exception e) { handleRollbackOnlyOnGlobal(globalTx, e); } finally { try { TransactionHelper.commitOrRollbackTransaction(); } catch (TransactionRuntimeException e) { handleRollbackOnlyOnGlobal(globalTx, e); } } } finally { transactionManager.resume(globalTx); } }
From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java
public static Transaction suspend() throws GenericTransactionException { try {// w ww.ja v a 2 s.c om if (TransactionUtil.getStatus() != STATUS_NO_TRANSACTION) { TransactionManager txMgr = TransactionFactory.getTransactionManager(); if (txMgr != null) { pushTransactionBeginStackSave(clearTransactionBeginStack()); pushSetRollbackOnlyCauseSave(clearSetRollbackOnlyCause()); Transaction trans = txMgr.suspend(); pushSuspendedTransaction(trans); return trans; } else { return null; } } else { logger.warn("No transaction in place, so not suspending."); return null; } } catch (SystemException e) { throw new GenericTransactionException("System error, could not suspend transaction", e); } }
From source file:org.jboss.ejb3.entity.JTATableIdGenerator.java
public synchronized Serializable generate(SessionImplementor session, Object object) throws HibernateException { // get TransactionManager from JNDI // no JNDI properties provided -> we are in the container TransactionManager tm = transactionManagerLookup.getTransactionManager(new Properties()); Transaction surroundingTransaction = null; // for resuming in finally block Connection conn = null; // for ressource cleanup String sql = null; // for exception try {/*from w ww .j a v a 2 s . co m*/ long result; // holds the resulting sequence value // prepare a new transaction context for the generator surroundingTransaction = tm.suspend(); if (log.isDebugEnabled()) { log.debug("surrounding tx suspended"); } tm.begin(); // get connection from managed environment conn = session.getBatcher().openConnection(); // execute fetching of current sequence value sql = query; PreparedStatement qps = conn.prepareStatement(query); try { ResultSet rs = qps.executeQuery(); if (!rs.next()) { String err = "could not read sequence value - you need to populate the table: " + tableName; log.error(err); throw new IdentifierGenerationException(err); } result = rs.getLong(1); rs.close(); } catch (SQLException sqle) { log.error("could not read a sequence value", sqle); throw sqle; } finally { qps.close(); } // increment sequence value sql = update; long sequence = result + 1; PreparedStatement ups = conn.prepareStatement(update); try { ups.setLong(1, sequence); ups.setLong(2, result); ups.executeUpdate(); } catch (SQLException sqle) { log.error("could not update sequence value in: " + tableName, sqle); throw sqle; } finally { ups.close(); } // commit transaction to ensure updated sequence is not rolled back tm.commit(); // transform sequence to the desired type and return the value Number typedSequence = IdentifierGeneratorFactory.createNumber(sequence, returnClass); if (log.isDebugEnabled()) { log.debug("generate() returned: " + typedSequence); } return typedSequence; } catch (SQLException sqle) { throw JDBCExceptionHelper.convert(session.getFactory().getSQLExceptionConverter(), sqle, "could not get or update next value", sql); } catch (Exception e) { try { tm.rollback(); throw new HibernateException(e); } catch (SystemException e1) { throw new HibernateException(e1); } } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { // ignore exception } // switch back to surrounding transaction context if (surroundingTransaction != null) { try { tm.resume(surroundingTransaction); if (log.isDebugEnabled()) { log.debug("surrounding tx resumed"); } } catch (Exception e) { throw new HibernateException(e); } } } }
From source file:org.nuxeo.runtime.transaction.TransactionHelper.java
/** * Suspend the current transaction if active and start a new transaction * * @return the suspended transaction or null * @throws TransactionRuntimeException// www. j a va 2 s . c om * @since 5.6 */ public static Transaction requireNewTransaction() { TransactionManager tm = NuxeoContainer.getTransactionManager(); if (tm == null) { return null; } try { Transaction tx = tm.getTransaction(); if (tx != null) { tx = tm.suspend(); } tm.begin(); return tx; } catch (NotSupportedException | SystemException e) { throw new TransactionRuntimeException("Cannot suspend tx", e); } }
From source file:org.nuxeo.runtime.transaction.TransactionHelper.java
public static Transaction suspendTransaction() { TransactionManager tm = NuxeoContainer.getTransactionManager(); if (tm == null) { return null; }/*from w w w . j av a2 s .c om*/ try { Transaction tx = tm.getTransaction(); if (tx != null) { tx = tm.suspend(); } return tx; } catch (SystemException e) { throw new TransactionRuntimeException("Cannot suspend tx", e); } }
From source file:org.openejb.transaction.TxRequiresNew.java
public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionManager transactionManager) throws Throwable { Transaction callerTransaction = transactionManager.suspend(); try {/* ww w . jav a2 s . co m*/ transactionManager.begin(); try { InvocationResult result = interceptor.invoke(ejbInvocation); return result; } catch (RollbackException re) { throw re; } catch (Throwable t) { try { transactionManager.setRollbackOnly(); } catch (Exception e) { log.warn("Unable to roll back", e); } throw t; } finally { if (transactionManager.getStatus() == Status.STATUS_ACTIVE) { transactionManager.commit(); } else { transactionManager.rollback(); } } } finally { if (callerTransaction != null) { transactionManager.resume(callerTransaction); } } }
From source file:org.wso2.carbon.attachment.mgt.core.dao.impl.TransactionManagerProvider.java
public void doNonTransactionalWork(java.lang.Runnable runnable) throws NotSupportedException { TransactionManager transactionManager = null; Transaction transaction = null;/*www.j av a2 s .c o m*/ try { transactionManager = getTransactionManager(); transaction = transactionManager.suspend(); } catch (Exception e) { NotSupportedException nse = new NotSupportedException(e.getMessage()); nse.initCause(e); log.error(nse.getLocalizedMessage(), nse); throw nse; } runnable.run(); try { transactionManager.resume(transaction); } catch (Exception e) { log.error(e.getLocalizedMessage(), e); try { transaction.setRollbackOnly(); } catch (SystemException se2) { throw new GeneralException(se2); } NotSupportedException nse = new NotSupportedException(e.getMessage()); nse.initCause(e); throw nse; } }