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 nimbus by nimbus-org.
the class TransactionSynchronizerService method synchronize.
public synchronized int synchronize() throws Exception {
int executeLogCount = 0;
Connection sourceConnection1 = sourceConnectionFactory.getConnection();
TransactionManager transactionManager = null;
if (transactionManagerFactory != null) {
transactionManager = transactionManagerFactory.getTransactionManager();
}
try {
final StringBuilder sql = new StringBuilder();
sql.append("select ");
sql.append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_QUERY).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_QUERY_TYPE);
sql.append(" from ").append(getTransactionTableName());
if (!isDeleteOnSynchronize) {
sql.append(" where ").append(getSynchronizeColumnName()).append(" <> '1'");
}
sql.append(" order by ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO);
final Statement selectTransactionLog = sourceConnection1.createStatement();
final ResultSet rsTransactionLog = selectTransactionLog.executeQuery(sql.toString());
sql.setLength(0);
sql.append("select ");
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_INDEX).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_NAME).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_TYPE).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_LENGTH).append(',');
sql.append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM);
sql.append(" from ").append(getTransactionParamTableName());
if (!isDeleteOnSynchronize) {
sql.append(" where ").append(getSynchronizeColumnName()).append(" <> '1'");
}
sql.append(" order by ").append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO);
final Statement selectTransactionParamsLog = sourceConnection1.createStatement();
final ResultSet rsTransactionParamsLog = selectTransactionParamsLog.executeQuery(sql.toString());
sql.setLength(0);
if (isDeleteOnSynchronize) {
sql.append("delete from ").append(getTransactionTableName());
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO).append("=?");
} else {
sql.append("update ").append(getTransactionTableName());
sql.append(" set ").append(getSynchronizeColumnName()).append("='1'");
sql.append(", ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_UPDATE_TIME).append("=?");
sql.append(", ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_UPDATE_USER).append("=?");
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO).append("=?");
}
String updateTransactionQuery = sql.toString();
sql.setLength(0);
if (isDeleteOnSynchronize) {
sql.append("delete from ").append(getTransactionParamTableName());
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO).append("=?");
} else {
sql.append("update ").append(getTransactionParamTableName());
sql.append(" set ").append(getSynchronizeColumnName()).append("='1'");
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO).append("=?");
}
String updateTransactionParamsQuery = sql.toString();
Transaction oldTransaction = transactionManager == null ? null : transactionManager.getTransaction();
if (transactionManager != null && oldTransaction != null) {
transactionManager.suspend();
}
try {
String preQuery = null;
int preQueryType = 0;
Statement preTransactionQuery = null;
int batchCount = 0;
Connection destConnection = null;
Connection sourceConnection2 = null;
PreparedStatement updateTransaction = null;
PreparedStatement updateTransactionParams = null;
String paramSeqNo = null;
try {
while (rsTransactionLog.next()) {
final String seqNo = rsTransactionLog.getString(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO);
final String query = rsTransactionLog.getString(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_QUERY);
final int queryType = rsTransactionLog.getInt(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_QUERY_TYPE);
if (transactionManager != null) {
if (transactionManager.getTransaction() == null) {
transactionManager.begin();
destConnection = destinationConnectionFactory.getConnection();
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
if (updateTransactionParamsQuery.length() != 0) {
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
}
}
} else {
if (destConnection == null) {
destConnection = destinationConnectionFactory.getConnection();
}
if (sourceConnection2 == null) {
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
if (updateTransactionParamsQuery.length() != 0) {
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
}
}
}
if (isDeleteOnSynchronize) {
updateTransaction.setString(1, seqNo);
} else {
updateTransaction.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
updateTransaction.setString(2, updateUser);
updateTransaction.setString(3, seqNo);
}
if (updateTransactionParams != null) {
updateTransactionParams.setString(1, seqNo);
}
Statement transactionQuery = null;
if (query.equals(preQuery) && queryType == preQueryType) {
transactionQuery = preTransactionQuery;
} else {
switch(queryType) {
case TransactionLoggingConnection.QUERY_TYPE_PREPARED_STATEMENT:
case TransactionLoggingConnection.QUERY_TYPE_CALLABLE_STATEMENT:
if (batchCount > 0) {
final boolean isTransactionCommit = executeBatch(transactionManager, destConnection, sourceConnection2, preTransactionQuery, updateTransactionParams, updateTransaction);
if (isTransactionCommit) {
preTransactionQuery = null;
preQuery = null;
preQueryType = 0;
transactionQuery = null;
updateTransactionParams = null;
updateTransaction = null;
destConnection = null;
sourceConnection2 = null;
transactionManager.begin();
destConnection = destinationConnectionFactory.getConnection();
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
if (isDeleteOnSynchronize) {
updateTransaction.setString(1, seqNo);
} else {
updateTransaction.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
updateTransaction.setString(2, updateUser);
updateTransaction.setString(3, seqNo);
}
if (updateTransactionParamsQuery.length() != 0) {
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
updateTransactionParams.setString(1, seqNo);
}
}
executeLogCount += batchCount;
batchCount = 0;
}
if (queryType == TransactionLoggingConnection.QUERY_TYPE_CALLABLE_STATEMENT) {
transactionQuery = destConnection.prepareCall(query);
} else {
transactionQuery = destConnection.prepareStatement(query);
}
break;
case TransactionLoggingConnection.QUERY_TYPE_STATEMENT:
default:
if (preQueryType != TransactionLoggingConnection.QUERY_TYPE_STATEMENT) {
if (batchCount > 0) {
final boolean isTransactionCommit = executeBatch(transactionManager, destConnection, sourceConnection2, preTransactionQuery, updateTransactionParams, updateTransaction);
if (isTransactionCommit) {
preTransactionQuery = null;
preQuery = null;
preQueryType = 0;
transactionQuery = null;
updateTransactionParams = null;
updateTransaction = null;
destConnection = null;
sourceConnection2 = null;
transactionManager.begin();
destConnection = destinationConnectionFactory.getConnection();
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
if (isDeleteOnSynchronize) {
updateTransaction.setString(1, seqNo);
} else {
updateTransaction.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
updateTransaction.setString(2, updateUser);
updateTransaction.setString(3, seqNo);
}
if (updateTransactionParamsQuery.length() != 0) {
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
updateTransactionParams.setString(1, seqNo);
}
}
executeLogCount += batchCount;
batchCount = 0;
}
transactionQuery = destConnection.createStatement();
} else {
transactionQuery = preTransactionQuery;
}
}
}
if (queryType == TransactionLoggingConnection.QUERY_TYPE_STATEMENT) {
transactionQuery.addBatch(query);
} else {
while (paramSeqNo != null || rsTransactionParamsLog.next()) {
paramSeqNo = rsTransactionParamsLog.getString(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO);
if (seqNo.equals(paramSeqNo)) {
paramSeqNo = null;
} else {
break;
}
final int paramIndex = rsTransactionParamsLog.getInt(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_INDEX);
String paramName = rsTransactionParamsLog.getString(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_NAME);
if (rsTransactionParamsLog.wasNull()) {
paramName = null;
}
int paramType = rsTransactionParamsLog.getInt(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_TYPE);
if (rsTransactionParamsLog.wasNull()) {
paramType = Integer.MIN_VALUE;
}
final int paramLength = rsTransactionParamsLog.getInt(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM_LENGTH);
final InputStream paramIs = rsTransactionParamsLog.getBinaryStream(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_PARAM);
if (!rsTransactionParamsLog.wasNull()) {
ObjectInputStream ois = null;
Calendar cal = null;
switch(paramType) {
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.BLOB:
if (paramName == null) {
((PreparedStatement) transactionQuery).setBinaryStream(paramIndex, paramIs, paramLength);
} else {
((CallableStatement) transactionQuery).setBinaryStream(paramName, paramIs, paramLength);
}
break;
case Types.CLOB:
case Types.LONGVARCHAR:
if (paramName == null) {
((PreparedStatement) transactionQuery).setAsciiStream(paramIndex, paramIs, paramLength);
} else {
((CallableStatement) transactionQuery).setAsciiStream(paramName, paramIs, paramLength);
}
break;
case Types.DATE:
ois = new ObjectInputStream(paramIs);
cal = (Calendar) ois.readObject();
if (paramName == null) {
((PreparedStatement) transactionQuery).setDate(paramIndex, new Date(cal.getTimeInMillis()));
} else {
((CallableStatement) transactionQuery).setDate(paramName, new Date(cal.getTimeInMillis()));
}
break;
case Types.TIME:
ois = new ObjectInputStream(paramIs);
cal = (Calendar) ois.readObject();
if (paramName == null) {
((PreparedStatement) transactionQuery).setTime(paramIndex, new Time(cal.getTimeInMillis()));
} else {
((CallableStatement) transactionQuery).setTime(paramName, new Time(cal.getTimeInMillis()));
}
break;
case Types.TIMESTAMP:
ois = new ObjectInputStream(paramIs);
cal = (Calendar) ois.readObject();
if (paramName == null) {
((PreparedStatement) transactionQuery).setTimestamp(paramIndex, new Timestamp(cal.getTimeInMillis()));
} else {
((CallableStatement) transactionQuery).setTimestamp(paramName, new Timestamp(cal.getTimeInMillis()));
}
break;
default:
ois = new ObjectInputStream(paramIs);
if (paramType != Integer.MIN_VALUE) {
if (paramName == null) {
((PreparedStatement) transactionQuery).setObject(paramIndex, ois.readObject(), paramType);
} else {
((CallableStatement) transactionQuery).setObject(paramName, ois.readObject(), paramType);
}
} else {
if (paramName == null) {
((PreparedStatement) transactionQuery).setObject(paramIndex, ois.readObject());
} else {
((CallableStatement) transactionQuery).setObject(paramName, ois.readObject());
}
}
}
} else {
if (paramType != Integer.MIN_VALUE) {
if (paramName == null) {
((PreparedStatement) transactionQuery).setNull(paramIndex, paramType);
} else {
((CallableStatement) transactionQuery).setNull(paramName, paramType);
}
} else {
if (paramName == null) {
((PreparedStatement) transactionQuery).setObject(paramIndex, null);
} else {
((CallableStatement) transactionQuery).setObject(paramName, null);
}
}
}
}
((PreparedStatement) transactionQuery).addBatch();
}
updateTransaction.addBatch();
if (updateTransactionParams != null) {
updateTransactionParams.addBatch();
}
batchCount++;
if (batchCount >= maxBatchCount) {
final boolean isTransactionCommit = executeBatch(transactionManager, destConnection, sourceConnection2, transactionQuery, updateTransactionParams, updateTransaction);
if (isTransactionCommit) {
preTransactionQuery = null;
preQuery = null;
preQueryType = 0;
transactionQuery = null;
updateTransactionParams = null;
updateTransaction = null;
destConnection = null;
sourceConnection2 = null;
}
executeLogCount += batchCount;
batchCount = 0;
} else {
preQuery = query;
preQueryType = queryType;
preTransactionQuery = transactionQuery;
}
}
if (batchCount > 0) {
executeBatch(transactionManager, destConnection, sourceConnection2, preTransactionQuery, updateTransactionParams, updateTransaction);
executeLogCount += batchCount;
}
rsTransactionLog.close();
if (updateTransaction != null) {
updateTransaction.close();
}
if (updateTransactionParams != null) {
updateTransactionParams.close();
}
if (!isDeleteOnSynchronize && garbageTime > 0) {
sql.setLength(0);
sql.append("select ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO).append(" from ").append(getTransactionTableName());
sql.append(" where ").append(getSynchronizeColumnName()).append("='1'");
if (getGarbageSynchronizeColumnNames() != null) {
String[] columnNames = getGarbageSynchronizeColumnNames();
for (int i = 0; i < columnNames.length; i++) {
sql.append(" and ").append(columnNames[i]).append("='1'");
}
}
sql.append(" and ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_UPDATE_TIME).append("<?");
sql.append(" order by ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO);
final String selectTransactionQuery = sql.toString();
sql.setLength(0);
sql.append("delete from ").append(getTransactionTableName());
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO).append("=?");
updateTransactionQuery = sql.toString();
sql.setLength(0);
sql.append("delete from ").append(getTransactionParamTableName());
sql.append(" where ").append(TransactionLoggingConnection.TRANSACTION_PARAM_TABLE_COLUMN_NAME_SEQNO).append("=?");
updateTransactionParamsQuery = sql.toString();
PreparedStatement selectGarbageSeqNo = sourceConnection1.prepareStatement(selectTransactionQuery);
selectGarbageSeqNo.setTimestamp(1, new Timestamp(System.currentTimeMillis() - garbageTime));
final ResultSet rsGarbageSeqNo = selectGarbageSeqNo.executeQuery();
batchCount = 0;
while (rsGarbageSeqNo.next()) {
final String seqNo = rsGarbageSeqNo.getString(TransactionLoggingConnection.TRANSACTION_TABLE_COLUMN_NAME_SEQNO);
if (transactionManager != null) {
if (transactionManager.getTransaction() == null) {
transactionManager.begin();
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
}
} else {
if (sourceConnection2 == null) {
sourceConnection2 = sourceConnectionFactory.getConnection();
updateTransaction = sourceConnection2.prepareStatement(updateTransactionQuery);
updateTransactionParams = sourceConnection2.prepareStatement(updateTransactionParamsQuery);
}
}
updateTransaction.setString(1, seqNo);
updateTransactionParams.setString(1, seqNo);
updateTransaction.addBatch();
updateTransactionParams.addBatch();
batchCount++;
if (batchCount >= maxBatchCount) {
final boolean isTransactionCommit = executeBatch(transactionManager, null, sourceConnection2, null, updateTransactionParams, updateTransaction);
if (isTransactionCommit) {
updateTransactionParams = null;
updateTransaction = null;
sourceConnection2 = null;
}
batchCount = 0;
}
}
if (batchCount > 0) {
executeBatch(transactionManager, null, sourceConnection2, null, updateTransactionParams, updateTransaction);
}
rsGarbageSeqNo.close();
updateTransaction.close();
updateTransactionParams.close();
}
} catch (RollbackException e) {
throw e;
} catch (HeuristicMixedException e) {
throw e;
} catch (HeuristicRollbackException e) {
throw e;
} catch (SystemException e) {
throw e;
} catch (Exception e) {
if (transactionManager != null && transactionManager.getTransaction() != null) {
transactionManager.rollback();
}
throw e;
} catch (Error err) {
if (transactionManager != null && transactionManager.getTransaction() != null) {
transactionManager.rollback();
}
throw err;
} finally {
if (destConnection != null) {
try {
destConnection.close();
} catch (SQLException e) {
}
destConnection = null;
}
if (sourceConnection2 != null) {
try {
sourceConnection2.close();
} catch (SQLException e) {
}
sourceConnection2 = null;
}
}
} finally {
if (transactionManager != null && oldTransaction != null) {
transactionManager.resume(oldTransaction);
}
}
} finally {
try {
sourceConnection1.close();
} catch (SQLException e) {
}
sourceConnection1 = null;
}
return executeLogCount;
}
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);
}
}
use of javax.transaction.HeuristicRollbackException in project partyline by Commonjava.
the class InfinispanJFS method updateDominantLocks.
@Override
public void updateDominantLocks(String path, UnlockStatus unlockStatus) {
// Do nothing if dominance did not change
if (!unlockStatus.isDominanceChanged()) {
return;
}
TransactionManager transactionManager = metadataCache.getAdvancedCache().getTransactionManager();
try {
transactionManager.begin();
FileMeta meta = metadataCache.get(path);
if (unlockStatus.getDominantLockLevel() == null) {
meta.removeLock(this.nodeKey);
} else {
meta.setLock(this.nodeKey, unlockStatus.getDominantLockLevel());
}
metadataCache.put(path, meta);
} catch (NotSupportedException | SystemException e) {
try {
transactionManager.rollback();
} catch (SystemException e1) {
LoggerFactory.getLogger(getClass().getName()).error("System Exception during transaction rollback involving path: " + path, e1);
}
} finally {
try {
transactionManager.commit();
} catch (RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException e) {
LoggerFactory.getLogger(getClass().getName()).error("Exception during transaction commit involving path: " + path, e);
}
}
}
use of javax.transaction.HeuristicRollbackException in project ovirt-engine by oVirt.
the class TransactionSupport method executeInNewTransaction.
/**
* Forces "REQUIRES_NEW" and executes given code in that scope
*/
public static <T> T executeInNewTransaction(TransactionMethod<T> code) {
T result = null;
Transaction transaction = null;
TransactionManager tm = findTransactionManager();
try {
// suspend existing if exists
transaction = tm.getTransaction();
if (transaction != null) {
transaction = tm.suspend();
}
// start new transaction
tm.begin();
Transaction newTransaction = tm.getTransaction();
// run the code
try {
result = code.runInTransaction();
} catch (RuntimeException rte) {
tm.rollback();
log.info("transaction rolled back");
throw rte;
} catch (Exception e) {
// code failed need to rollback
tm.rollback();
log.info("transaction rolled back");
log.error("executeInNewTransaction - Wrapping Exception: {} with RunTimeException", e.getClass().getName());
throw new RuntimeException("Failed executing code", e);
}
// commit or rollback according to state
if (needToRollback(newTransaction.getStatus())) {
tm.rollback();
} else {
tm.commit();
}
} catch (SystemException | NotSupportedException | HeuristicRollbackException | HeuristicMixedException | RollbackException | IllegalStateException | SecurityException e) {
throw new RuntimeException("Failed managing transaction", e);
} finally {
// check if we need to resume previous transaction
if (transaction != null) {
try {
tm.resume(transaction);
} catch (Exception e) {
log.error("Unable to resume transaction", e);
}
}
}
// and we are done...
return result;
}
Aggregations