/** {@inheritDoc} */ @Override public void beforeProcess() throws Throwable { // prepare execution context AuthenticationUtil.pushAuthentication(); AuthenticationUtil.setFullyAuthenticatedUser(this.fullyAuthenticatedUser); if (this.runAsUser != null && !this.runAsUser.equals(this.fullyAuthenticatedUser)) { AuthenticationUtil.setRunAsUser(this.runAsUser); } I18NUtil.setLocale(this.locale); if (this.contentLocale != null) { I18NUtil.setContentLocale(this.contentLocale); } try { try { super.doBeforeProcess(); } catch (final WrappedException ex) { // super should already handle unwrap runtime exceptions final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { // super should have handled this throw (RuntimeException) wrappedException; } throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException); } catch (final Throwable ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } catch (final Throwable ex) { /* * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be * caused by execution of the provided script callback without passing through a service with its transaction interceptor which * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco * SpringAwareUserTransaction) */ final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute(); transactionAttribute.setReadOnly(true); transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); // this never creates a new "real" transaction due to our propagation behavior final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute); try { if (!transaction.isRollbackOnly()) { LOGGER.debug( "Marking transaction as rollback-only due to exception during batch processing", ex); transaction.setRollbackOnly(); } } finally { // this never actually commits a "real" transaction - it just clears transaction // synchronizations this.txnManager.commit(transaction); } throw ex; } }
/** Borrowed from Seam */ public int getStatus() { if (ptm == null) { return TransactionManager.STATUS_NO_TRANSACTION; } // logger.debug( "Current TX name (According to TransactionSynchronizationManager) : " + // TransactionSynchronizationManager.getCurrentTransactionName() ); if (TransactionSynchronizationManager.isActualTransactionActive()) { TransactionStatus transaction = null; boolean commitNewTransaction = false; try { if (currentTransaction.size() == 0) { transaction = ptm.getTransaction(td); currentTransaction.push(transaction); commitNewTransaction = true; if (transaction.isNewTransaction()) { return TransactionManager.STATUS_COMMITTED; } } else { transaction = currentTransaction.peek(); } logger.debug("Current TX: " + transaction); // If SynchronizationManager thinks it has an active transaction but // our transaction is a new one // then we must be in the middle of committing if (transaction.isCompleted()) { if (transaction.isRollbackOnly()) { return TransactionManager.STATUS_ROLLEDBACK; } return TransactionManager.STATUS_COMMITTED; } else { if (transaction.isRollbackOnly()) { return 5; } return TransactionManager.STATUS_ACTIVE; } } finally { if (commitNewTransaction) { ptm.commit(transaction); } } } return TransactionManager.STATUS_NO_TRANSACTION; }
private void stubTransactionStatus() { given(txStatus.isRollbackOnly()).willReturn(false); PlatformTransactionManager txManager = mock(PlatformTransactionManager.class); given(txManager.getTransaction((TransactionDefinition) Mockito.anyObject())) .willReturn(txStatus); AnnotationTransactionAspect txAspect = AnnotationTransactionAspect.aspectOf(); txAspect.setTransactionManager(txManager); }
/** * Discription:[获取待办请款任务] * * @return * @author 大牙-小白 * @update 2012-9-8 大牙-小白 [变更描述] */ public String todoTask() { Map<String, Object> resultMap = new HashMap<String, Object>(); // 定义TransactionDefinition并设置好事务的隔离级别和传播方式。 DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); // 代价最大、可靠性最高的隔离级别,所有的事务都是按顺序一个接一个地执行 definition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); // 开始事务 TransactionStatus status = transactionManager.getTransaction(definition); PrintWriter out = null; try { out = super.getPrintWriter(); if (cashAdvanceInfo == null || cashAdvanceInfo.getCashUserId() == null || cashAdvanceInfo.getCashUserName() == null || "".equals(cashAdvanceInfo.getCashUserId()) || "".equals(cashAdvanceInfo.getCashUserName())) { resultMap.put("success", false); resultMap.put("msg", "当前用户信息为空,请检查!"); } else { resultMap = this.cashAdvanceService.getTodoRequestCash( this.springJTM.getUserTransaction(), roleService, jbpmService, cashAdvanceInfo, start, limit); resultMap.put("success", true); } } catch (Exception e) { LOG.error(e.getMessage()); status.setRollbackOnly(); resultMap.put("success", false); resultMap.put("msg", "系统错误,错误代码:" + e.getMessage()); } finally { if (status.isRollbackOnly()) { this.transactionManager.rollback(status); } else { this.transactionManager.commit(status); } // try { // jbpmService.disconnectJbpmServer(); // } catch (Exception e) { // e.printStackTrace(); // } if (out != null) { out.print(getJsonString(resultMap)); out.flush(); out.close(); } } return null; }
/** * Discription:[通过或者驳回请款请求] * * @return * @author 大牙-小白 * @update 2012-9-15 大牙-小白 [变更描述] */ public String doRequestTask() { Map<String, Object> resultMap = new HashMap<String, Object>(); // 定义TransactionDefinition并设置好事务的隔离级别和传播方式。 DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); // 代价最大、可靠性最高的隔离级别,所有的事务都是按顺序一个接一个地执行 definition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); // 开始事务 TransactionStatus status = transactionManager.getTransaction(definition); PrintWriter out = null; try { out = super.getPrintWriter(); if (this.taskIds == null || "".equals(taskIds) || this.loanIds == null || "".equals(loanIds)) { resultMap.put("success", false); resultMap.put("msg", "请选择需要处理的请款请求!"); } else { this.cashAdvanceService.doRequest( this.springJTM.getUserTransaction(), roleService, jbpmService, taskIds, loanIds, currentUserId, currentUserName, doType, checkResult, approveResult, reason); } } catch (Exception e) { LOG.error(e.getMessage(), e); status.setRollbackOnly(); resultMap.put("success", false); resultMap.put("msg", e.getMessage()); } finally { if (status.isRollbackOnly()) { this.transactionManager.rollback(status); } else { this.transactionManager.commit(status); } if (out != null) { out.print(getJsonString(resultMap)); out.flush(); out.close(); } } return null; }
protected void finalizeTransaction(TransactionStatus status, boolean isError) { boolean isActive = false; try { if (!status.isRollbackOnly()) { isActive = true; } } catch (Exception e) { // do nothing } if (isActive) { if (isError) { transactionManager.rollback(status); } else { transactionManager.commit(status); } } }
/** {@inheritDoc} */ @Override public void process(final Object entry) throws Throwable { try { try { super.doProcess(entry); } catch (final WrappedException ex) { // super should already handle unwrap runtime exceptions final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { // super should have handled this throw (RuntimeException) wrappedException; } throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException); } catch (final Throwable ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } catch (final Throwable ex) { /* * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be * caused by execution of the provided script callback without passing through a service with its transaction interceptor which * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco * SpringAwareUserTransaction) */ final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute(); transactionAttribute.setReadOnly(true); transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY); final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute); if (!transaction.isRollbackOnly()) { LOGGER.debug( "Marking transaction as rollback-only due to exception during batch processing", ex); transaction.setRollbackOnly(); } throw ex; } }