Search in sources :

Example 81 with SystemException

use of javax.transaction.SystemException in project wildfly by wildfly.

the class TransactionalServiceImpl method isTransactionActive.

@Override
public boolean isTransactionActive() {
    LOG.debug("TransactionalServiceImpl.isTransactionActive()");
    Transaction transaction = null;
    try {
        transaction = TransactionManager.transactionManager().getTransaction();
    } catch (SystemException e) {
    }
    if (transaction == null) {
        return false;
    }
    try {
        return transaction.getStatus() == Status.STATUS_ACTIVE;
    } catch (SystemException e) {
        return false;
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException)

Example 82 with SystemException

use of javax.transaction.SystemException in project Activiti by Activiti.

the class JtaTransactionContext method addTransactionListener.

public void addTransactionListener(TransactionState transactionState, final TransactionListener transactionListener) {
    Transaction transaction = getTransaction();
    CommandContext commandContext = Context.getCommandContext();
    try {
        transaction.registerSynchronization(new TransactionStateSynchronization(transactionState, transactionListener, commandContext));
    } catch (IllegalStateException e) {
        throw new ActivitiException("IllegalStateException while registering synchronization ", e);
    } catch (RollbackException e) {
        throw new ActivitiException("RollbackException while registering synchronization ", e);
    } catch (SystemException e) {
        throw new ActivitiException("SystemException while registering synchronization ", e);
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Transaction(javax.transaction.Transaction) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 83 with SystemException

use of javax.transaction.SystemException in project galley by Commonjava.

the class FastLocalCacheProvider method copy.

@Override
public void copy(ConcreteResource from, ConcreteResource to) throws IOException {
    final String fromNFSPath = getKeyForResource(from);
    final String toNFSPath = getKeyForResource(to);
    //FIXME: there is no good solution here for thread locking as there are two resource needs to be locked. If handled not correctly, will cause dead lock
    InputStream nfsFrom = null;
    OutputStream nfsTo = null;
    try {
        //FIXME: need to think about this lock of the re-entrant way and ISPN lock wait
        nfsOwnerCache.beginTransaction();
        nfsOwnerCache.lock(fromNFSPath, toNFSPath);
        plCacheProvider.copy(from, to);
        nfsFrom = new FileInputStream(getNFSDetachedFile(from));
        File nfsToFile = getNFSDetachedFile(to);
        if (!nfsToFile.exists() && !nfsToFile.isDirectory()) {
            if (!nfsToFile.getParentFile().exists()) {
                nfsToFile.getParentFile().mkdirs();
            }
            try {
                nfsToFile.createNewFile();
            } catch (IOException e) {
                logger.error("[galley] New nfs file created not properly.", e);
            }
        }
        nfsTo = new FileOutputStream(nfsToFile);
        IOUtils.copy(nfsFrom, nfsTo);
        //FIXME: need to use put?
        nfsOwnerCache.putIfAbsent(toNFSPath, getCurrentNodeIp());
        nfsOwnerCache.commit();
    } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
        logger.error("[galley] Transaction error for nfs cache during file copying.", e);
        try {
            nfsOwnerCache.rollback();
        } catch (SystemException se) {
            final String errorMsg = "[galley] Transaction rollback error for nfs cache during file copying.";
            logger.error(errorMsg, se);
            throw new IllegalStateException(errorMsg, se);
        }
    } finally {
        IOUtils.closeQuietly(nfsFrom);
        IOUtils.closeQuietly(nfsTo);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) FileInputStream(java.io.FileInputStream) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) SystemException(javax.transaction.SystemException) FileOutputStream(java.io.FileOutputStream) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) File(java.io.File)

Example 84 with SystemException

use of javax.transaction.SystemException in project galley by Commonjava.

the class FastLocalCacheProvider method delete.

@Override
public boolean delete(ConcreteResource resource) throws IOException {
    final File nfsFile = getNFSDetachedFile(resource);
    final String pathKey = getKeyForPath(nfsFile.getCanonicalPath());
    synchronized (getTransfer(resource)) {
        boolean localDeleted = false;
        try {
            // must make sure the local file is not in reading/writing status
            if (!plCacheProvider.isWriteLocked(resource) && !plCacheProvider.isReadLocked(resource)) {
                logger.debug("[galley] Local cache file is not locked, will be deleted now.");
                localDeleted = plCacheProvider.delete(resource);
            } else {
                logger.warn("Resource {} is locked by other threads for waiting and writing, can not be deleted now", resource);
            }
            if (!localDeleted) {
                // if local deletion not success, no need to delete NFS to keep data consistency
                logger.info("local file deletion failed for {}", resource);
                return false;
            }
            lockByISPN(resource);
            nfsOwnerCache.remove(pathKey);
            final boolean nfsDeleted = nfsFile.delete();
            if (!nfsDeleted) {
                logger.info("nfs file deletion failed for {}", nfsFile);
            }
            return nfsDeleted;
        } catch (NotSupportedException | SystemException e) {
            final String errorMsg = String.format("[galley] Cache TransactionManager got error, locking key is %s", pathKey);
            logger.error(errorMsg, e);
            throw new IllegalStateException(errorMsg, e);
        } finally {
            if (localDeleted) {
                try {
                    nfsOwnerCache.rollback();
                } catch (SystemException e) {
                    final String errorMsg = String.format("[galley] Cache TransactionManager rollback got error, locking key is %s", pathKey);
                    logger.error(errorMsg, e);
                }
            }
        }
    }
}
Also used : SystemException(javax.transaction.SystemException) NotSupportedException(javax.transaction.NotSupportedException) File(java.io.File)

Example 85 with SystemException

use of javax.transaction.SystemException in project galley by Commonjava.

the class FastLocalCacheProvider method openOutputStream.

/**
     * For file writing, will wrapping two output streams to caller - one for local cache file, another for nfs file -,
     * and the caller can write to these two streams in the meantime. <br />
     * For the local part, because it uses {@link org.commonjava.maven.galley.cache.partyline.PartyLineCacheProvider} as
     * i/o provider, this supports the R/W on the same resource in the meantime. For details, please see
     * {@link org.commonjava.maven.galley.cache.partyline.PartyLineCacheProvider}.
     *
     * @param resource - the resource will be read
     * @return - the output stream for further writing
     * @throws IOException
     */
@Override
public OutputStream openOutputStream(ConcreteResource resource) throws IOException {
    OutputStream dualOut;
    final String nodeIp = getCurrentNodeIp();
    final String pathKey = getKeyForResource(resource);
    final File nfsFile = getNFSDetachedFile(resource);
    synchronized (getTransfer(resource)) {
        try {
            lockByISPN(resource);
            nfsOwnerCache.put(pathKey, nodeIp);
            logger.debug("Start to get output stream from local cache through partyline to do join stream");
            final OutputStream localOut = plCacheProvider.openOutputStream(resource);
            logger.debug("The output stream from local cache through partyline is got successfully");
            if (!nfsFile.exists() && !nfsFile.isDirectory()) {
                try {
                    if (!nfsFile.getParentFile().exists()) {
                        nfsFile.getParentFile().mkdirs();
                    }
                    nfsFile.createNewFile();
                } catch (IOException e) {
                    logger.error("[galley] New nfs file created not properly.", e);
                    throw e;
                }
            }
            final OutputStream nfsOutputStream = new FileOutputStream(nfsFile);
            logger.debug("The output stream from NFS is got successfully");
            // will wrap the cache manager in stream wrapper, and let it do tx commit in stream close to make sure
            // the two streams writing's consistency.
            dualOut = new DualOutputStreamsWrapper(localOut, nfsOutputStream, nfsOwnerCache, pathKey, resource);
            if (nfsOwnerCache.getLockOwner(pathKey) != null) {
                logger.trace("ISPN locker for key {} with resource {} is {}", pathKey, resource, nfsOwnerCache.getLockOwner(pathKey));
            }
            ThreadContext streamHolder = ThreadContext.getContext(true);
            Set<WeakReference<OutputStream>> streams = (Set<WeakReference<OutputStream>>) streamHolder.get(FAST_LOCAL_STREAMS);
            if (streams == null) {
                streams = new HashSet<>(10);
            }
            streams.add(new WeakReference<>(dualOut));
            streamHolder.put(FAST_LOCAL_STREAMS, streams);
        } catch (NotSupportedException | SystemException e) {
            logger.error("[galley] Transaction error for nfs cache during file writing.", e);
            throw new IllegalStateException(String.format("[galley] Output stream for resource %s open failed.", resource.toString()), e);
        }
        logger.debug("The dual output stream wrapped and returned successfully");
        return dualOut;
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ThreadContext(org.commonjava.cdi.util.weft.ThreadContext) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) FileOutputStream(java.io.FileOutputStream) WeakReference(java.lang.ref.WeakReference) NotSupportedException(javax.transaction.NotSupportedException) File(java.io.File)

Aggregations

SystemException (javax.transaction.SystemException)102 Transaction (javax.transaction.Transaction)34 RollbackException (javax.transaction.RollbackException)29 NotSupportedException (javax.transaction.NotSupportedException)22 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)18 IOException (java.io.IOException)16 HeuristicMixedException (javax.transaction.HeuristicMixedException)16 UserTransaction (javax.transaction.UserTransaction)14 XAException (javax.transaction.xa.XAException)13 Test (org.junit.Test)12 TransactionManager (javax.transaction.TransactionManager)11 SQLException (java.sql.SQLException)10 LogWriterI18n (org.apache.geode.i18n.LogWriterI18n)10 InvalidTransactionException (javax.transaction.InvalidTransactionException)9 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 XAResource (javax.transaction.xa.XAResource)7 Synchronization (javax.transaction.Synchronization)6 File (java.io.File)5