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