List of usage examples for javax.transaction Transaction registerSynchronization
public void registerSynchronization(Synchronization sync) throws RollbackException, IllegalStateException, SystemException;
From source file:io.strandberg.xadisk.XADiskSessionFactory.java
public XASession getXASession() throws SystemException, RollbackException { final Transaction transaction = transactionManager.getTransaction(); synchronized (transaction) { // get the xaSession associated with the current transaction - if any XASession xaSession = xaSessionMap.get(transaction); if (xaSession == null) { // create a new xaSession xaSession = xaFileSystem.createSessionForXATransaction(); logger.debug("New XASession has been created"); // enlist the xaSessions XAResource transaction.enlistResource(xaSession.getXAResource()); logger.debug("XASession's XAResource has been enlisted in transaction"); // Remove the xaSession from the xaSessionMap after the transaction is completed transaction.registerSynchronization(new Synchronization() { public void beforeCompletion() { }//from w w w . j a va 2s .c om public void afterCompletion(int status) { xaSessionMap.remove(transaction); logger.debug("XASession has been removed from the XASession Map"); } }); // Associate the transaction with the xaSession xaSessionMap.put(transaction, xaSession); logger.debug("New XASession added to XASession Map"); } else { logger.debug("XASession found in XASession Map"); } return xaSession; } }
From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java
public void testSyncReplWithModficationsOnBothCachesWithRollback() throws Exception { TransactionManager tm;/*from ww w . ja v a2 s .co m*/ final Fqn fqn1 = Fqn.fromString("/one/two/three"); final Fqn fqn2 = Fqn.fromString("/eins/zwei/drei"); cache1.getConfiguration().setSyncRollbackPhase(true); cache2.getConfiguration().setSyncRollbackPhase(true); tm = beginTransaction(); cache1.put(fqn1, "age", 38); cache2.put(fqn2, "age", 39); // this will rollback the transaction Transaction tx = tm.getTransaction(); tx.registerSynchronization(new TransactionAborter(tx)); try { tm.commit(); fail("commit should throw a RollbackException, we should not get here"); } catch (RollbackException rollback) { } assertEquals(0, cache1.getNumberOfLocksHeld()); assertEquals(0, cache2.getNumberOfLocksHeld()); assertEquals(0, cache1.getNumberOfNodes()); assertEquals(0, cache2.getNumberOfNodes()); }
From source file:org.grails.orm.hibernate.GrailsSessionContext.java
protected void registerJtaSynchronization(Session session, SessionHolder sessionHolder) { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(session); if (jtaTm == null) { return;//from w w w.ja v a 2s . com } try { Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx == null) { return; } int jtaStatus = jtaTx.getStatus(); if (jtaStatus != Status.STATUS_ACTIVE && jtaStatus != Status.STATUS_MARKED_ROLLBACK) { return; } LOG.debug("Registering JTA transaction synchronization for new Hibernate Session"); SessionHolder holderToUse = sessionHolder; // Register JTA Transaction with existing SessionHolder. // Create a new SessionHolder if none existed before. if (holderToUse == null) { holderToUse = new SessionHolder(session); } else { // it's up to the caller to manage concurrent sessions // holderToUse.addSession(session); } jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter(createSpringSessionSynchronization(holderToUse), jtaTm)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } catch (Throwable ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } }
From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java
public static void registerSynchronization(Synchronization sync) throws GenericTransactionException { if (sync == null) { return;/*from w w w . jav a2s. co m*/ } try { TransactionManager tm = TransactionFactoryLoader.getInstance().getTransactionManager(); if (tm != null && tm.getStatus() == STATUS_ACTIVE) { Transaction tx = tm.getTransaction(); if (tx != null) { tx.registerSynchronization(sync); } } } catch (RollbackException e) { throw new GenericTransactionException( "Roll Back error, could not register synchronization in transaction even though transactions are available, current transaction rolled back", e); } catch (SystemException e) { throw new GenericTransactionException( "System error, could not register synchronization in transaction even though transactions are available", e); } }
From source file:org.apache.ojb.odmg.JTATxManager.java
/** * Do synchronization of the given J2EE ODMG Transaction *//*w w w . j av a 2 s.com*/ private void registerSynchronization(TransactionImpl odmgTrans, Transaction transaction) { // todo only need for development if (odmgTrans == null || transaction == null) { log.error("One of the given parameters was null --> cannot do synchronization!" + " omdg transaction was null: " + (odmgTrans == null) + ", external transaction was null: " + (transaction == null)); return; } int status = -1; // default status. try { status = transaction.getStatus(); if (status != Status.STATUS_ACTIVE) { throw new OJBRuntimeException( "Transaction synchronization failed - wrong status of external container tx: " + getStatusString(status)); } } catch (SystemException e) { throw new OJBRuntimeException("Can't read status of external tx", e); } try { //Sequence of the following method calls is significant // 1. register the synchronization with the ODMG notion of a transaction. transaction.registerSynchronization((J2EETransactionImpl) odmgTrans); // 2. mark the ODMG transaction as being in a JTA Transaction // Associate external transaction with the odmg transaction. txRepository.set(new TxBuffer(odmgTrans, transaction)); } catch (Exception e) { log.error("Cannot associate PersistenceBroker with running Transaction", e); throw new OJBRuntimeException( "Transaction synchronization failed - wrong status of external container tx", e); } }
From source file:org.apache.openjpa.kernel.AbstractBrokerFactory.java
/** * Synchronize the given broker with a managed transaction, * optionally starting one if none is in progress. * * @return true if synched with transaction, false otherwise *//*from ww w . j av a2 s .c o m*/ boolean syncWithManagedTransaction(BrokerImpl broker, boolean begin) { Transaction trans; try { ManagedRuntime mr = broker.getManagedRuntime(); TransactionManager tm = mr.getTransactionManager(); if (tm == null) { throw new InternalException(_loc.get("null-transactionmanager", mr)); } trans = tm.getTransaction(); if (trans != null && (trans.getStatus() == Status.STATUS_NO_TRANSACTION || trans.getStatus() == Status.STATUS_UNKNOWN)) trans = null; if (trans == null && begin) { tm.begin(); trans = tm.getTransaction(); } else if (trans == null) return false; // synch broker and trans trans.registerSynchronization(broker); // we don't need to synchronize on brokers or guard against multiple // threads using the same trans since one JTA transaction can never // be active on multiple concurrent threads. Object txKey = mr.getTransactionKey(); Collection<Broker> brokers = _transactional.get(txKey); if (brokers == null) { brokers = new ArrayList<Broker>(2); _transactional.put(txKey, brokers); trans.registerSynchronization(new RemoveTransactionSync(txKey)); } brokers.add(broker); return true; } catch (OpenJPAException ke) { throw ke; } catch (Exception e) { throw new GeneralException(e); } }
From source file:org.compass.core.transaction.JTASyncTransaction.java
protected void doBindToTransaction(Transaction tx, InternalCompassSession session, boolean newTransaction) throws Exception { tx.registerSynchronization(new JTATransactionSynchronization(session, tx, newTransaction, commitBeforeCompletion, transactionFactory)); }
From source file:org.eclipse.ecr.core.api.TransactionalCoreSessionWrapper.java
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Boolean b = threadBound.get(); if (b == null) { // first call in thread try {/* w w w . jav a2 s . co m*/ Transaction tx = TransactionHelper.lookupTransactionManager().getTransaction(); if (tx != null && tx.getStatus() != Status.STATUS_MARKED_ROLLBACK) { tx.registerSynchronization(this); session.afterBegin(); threadBound.set(Boolean.TRUE); } } catch (NamingException e) { // no transaction manager, ignore } catch (Exception e) { log.error("Error on transaction synchronizer registration", e); } } try { return method.invoke(session, args); } catch (Throwable t) { if (TransactionHelper.isTransactionActive() && needsRollback(method, t)) { TransactionHelper.setTransactionRollbackOnly(); } if (t instanceof InvocationTargetException) { Throwable tt = ((InvocationTargetException) t).getTargetException(); if (tt != null) { throw tt; } } throw t; } }
From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java
public static void registerSynchronization(Synchronization sync) throws GenericTransactionException { if (sync == null) { return;/*from ww w .j a v a 2 s . c o m*/ } try { TransactionManager tm = TransactionFactory.getTransactionManager(); if (tm != null && tm.getStatus() == STATUS_ACTIVE) { Transaction tx = tm.getTransaction(); if (tx != null) { tx.registerSynchronization(sync); } } } catch (RollbackException e) { // This is Java 1.4 only, but useful for certain debuggins: Throwable t = // e.getCause() == null ? e : e.getCause(); throw new GenericTransactionException( "Roll Back error, could not register synchronization in transaction even though transactions are available, current transaction rolled back", e); } catch (SystemException e) { // This is Java 1.4 only, but useful for certain debuggins: Throwable t = // e.getCause() == null ? e : e.getCause(); throw new GenericTransactionException( "System error, could not register synchronization in transaction even though transactions are available", e); } }
From source file:org.nuxeo.ecm.core.work.WorkManagerImpl.java
/** * Schedule after commit. Returns {@code false} if impossible (no * transaction or transaction manager).//from w w w . j av a 2 s. c o m * * @since 5.8 */ protected boolean scheduleAfterCommit(Work work, Scheduling scheduling) { TransactionManager transactionManager; try { transactionManager = TransactionHelper.lookupTransactionManager(); } catch (NamingException e) { transactionManager = null; } if (transactionManager == null) { if (log.isDebugEnabled()) { log.debug("Not scheduling work after commit because of missing transaction manager: " + work); } return false; } try { Transaction transaction = transactionManager.getTransaction(); if (transaction == null) { if (log.isDebugEnabled()) { log.debug("Not scheduling work after commit because of missing transaction: " + work); } return false; } int status = transaction.getStatus(); if (status == Status.STATUS_ACTIVE) { if (log.isDebugEnabled()) { log.debug("Scheduling work after commit: " + work); } transaction.registerSynchronization(new WorkScheduling(work, scheduling)); return true; } else { if (log.isDebugEnabled()) { log.debug("Not scheduling work after commit because transaction is in status " + status + ": " + work); } return false; } } catch (SystemException | RollbackException e) { log.error("Cannot schedule after commit", e); return false; } }