Search in sources :

Example 11 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException 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 12 with HeuristicRollbackException

use of javax.transaction.HeuristicRollbackException in project aries by apache.

the class TxDBServlet method insertIntoTransaction.

/**
	 * This method demonstrates how to enlist JDBC connection into Transaction according OSGi enterprise specification.
	 * 
	 * @param xads XADataSource
	 * @param tm TransactionManager
	 * @param value which will be inserted into table
	 * @param toCommit Specify if the transaction will be committed or rolledback
	 * @throws SQLException
	 * @throws GenericJTAException
	 */
private void insertIntoTransaction(XADataSource xads, TransactionManager tm, String value, boolean toCommit) throws SQLException, GenericJTAException {
    XAConnection xaConnection = xads.getXAConnection();
    Connection connection = xaConnection.getConnection();
    XAResource xaResource = xaConnection.getXAResource();
    try {
        tm.begin();
        Transaction transaction = tm.getTransaction();
        transaction.enlistResource(xaResource);
        PreparedStatement insertStatement = connection.prepareStatement(INSERT_INTO_TABLE);
        insertStatement.setString(1, value);
        insertStatement.executeUpdate();
        if (toCommit) {
            transaction.commit();
        } else {
            transaction.rollback();
        }
    } catch (RollbackException e) {
        throw new GenericJTAException(e);
    } catch (SecurityException e) {
        throw new GenericJTAException(e);
    } catch (IllegalStateException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicMixedException e) {
        throw new GenericJTAException(e);
    } catch (HeuristicRollbackException e) {
        throw new GenericJTAException(e);
    } catch (SystemException e) {
        throw new GenericJTAException(e);
    } catch (NotSupportedException e) {
        throw new GenericJTAException(e);
    }
}
Also used : Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PreparedStatement(java.sql.PreparedStatement) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException) XAResource(javax.transaction.xa.XAResource) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotSupportedException(javax.transaction.NotSupportedException) XAConnection(javax.sql.XAConnection)

Aggregations

HeuristicRollbackException (javax.transaction.HeuristicRollbackException)12 HeuristicMixedException (javax.transaction.HeuristicMixedException)10 SystemException (javax.transaction.SystemException)10 RollbackException (javax.transaction.RollbackException)9 NotSupportedException (javax.transaction.NotSupportedException)5 IOException (java.io.IOException)3 XAException (javax.transaction.xa.XAException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 InvalidTransactionException (javax.transaction.InvalidTransactionException)2 Transaction (javax.transaction.Transaction)2 UserTransaction (javax.transaction.UserTransaction)2 XAResource (javax.transaction.xa.XAResource)2 Test (org.junit.Test)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 TransactionException (io.requery.TransactionException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1