Java Code Examples for org.springframework.transaction.PlatformTransactionManager#commit()

The following examples show how to use org.springframework.transaction.PlatformTransactionManager#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScheduleConsumer.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
private ScheduleInstanceConsumerDTO invoke(final ScheduleInstanceConsumerDTO data) {
    final JobTaskInvokeBean invokeBean = invokeBeanMap.get(data.getMethod());
    final JobTask jobTask = invokeBean.jobTask;
    PlatformTransactionManager platformTransactionManager = getSagaTaskTransactionManager(jobTask.transactionManager());
    TransactionStatus status = createTransactionStatus(transactionManager, jobTask.transactionIsolation().value());
    beforeInvoke(data.getUserDetails());
    try {
        invokeBean.method.setAccessible(true);
        Object result = invokeBean.method.invoke(invokeBean.object, getInputMap(data.getExecuteParams()));
        if (result != null) {
            result = objectMapper.writeValueAsString(result);
        }
        scheduleConsumerClient.updateStatus(data.getId(), new UpdateStatusDTO(data.getId(), QuartzDefinition.InstanceStatus.COMPLETED.name(),
                resultToJson(result, objectMapper), null, data.getObjectVersionNumber()));
        platformTransactionManager.commit(status);
        runningTasks.remove(data.getId());
    } catch (Exception e) {
        String errorMsg = getErrorInfoFromException(e);
        LOGGER.info("@JobTask method: {}, id: {} invoke error", data.getMethod(), data.getId(), getLoggerException(e));
        invokeError(platformTransactionManager, status, data, errorMsg);
    } finally {
        afterInvoke();
    }
    return data;
}
 
Example 2
Source File: StartCompensationHandler.java    From Raincat with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 补偿的时候,不走分布式事务处理.
 *
 * @param point point 切点
 * @param info  信息
 * @return Object
 * @throws Throwable ex
 */
@Override
public Object handler(final ProceedingJoinPoint point, final TxTransactionInfo info) throws Throwable {
    TxTransactionLocal.getInstance().setTxGroupId(CommonConstant.COMPENSATE_ID);

    PlatformTransactionManager platformTransactionManager =
            TransactionManagerHelper.getTransactionManager(info.getTransactionManager());
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def);
    try {
        final Object proceed = point.proceed();
        platformTransactionManager.commit(transactionStatus);
        return proceed;
    } catch (Throwable e) {
        platformTransactionManager.rollback(transactionStatus);
        throw e;
    } finally {
        TxTransactionLocal.getInstance().removeTxGroupId();
        CompensationLocal.getInstance().removeCompensationId();
    }
}
 
Example 3
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTran(String groovyName, String methodName,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 4
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    if(nestDef==null){
	    	nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
	    }
	    def.setPropagationBehavior(nestDef);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 5
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTran(String groovyName, String methodName,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 6
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    if(nestDef==null){
	    	nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
	    }
	    def.setPropagationBehavior(nestDef);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 7
Source File: SampleDataLoader.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void init() {
	log.info("Initializing " + getClass().getName());
	if(cmAdmin == null) {
		return;
	}
	if(loadSampleData) {
		loginToSakai();
		PlatformTransactionManager tm = (PlatformTransactionManager)beanFactory.getBean("org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager");
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = tm.getTransaction(def);
		try {
			load();
		} catch (Exception e) {
			log.error("Unable to load CM data: " + e);
			tm.rollback(status);
		} finally {
			if(!status.isCompleted()) {
				tm.commit(status);
			}
		}
		logoutFromSakai();
	} else {
		if(log.isInfoEnabled()) log.info("Skipped CM data load");
	}
}
 
Example 8
Source File: SampleDataLoader.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void init() {
	log.info("Initializing " + getClass().getName());
	if(cmAdmin == null) {
		return;
	}
	if(loadSampleData) {
		loginToSakai();
		PlatformTransactionManager tm = (PlatformTransactionManager)beanFactory.getBean("org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager");
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = tm.getTransaction(def);
		try {
			load();
		} catch (Exception e) {
			log.error("Unable to load CM data: " + e);
			tm.rollback(status);
		} finally {
			if(!status.isCompleted()) {
				tm.commit(status);
			}
		}
		logoutFromSakai();
	} else {
		if(log.isInfoEnabled()) log.info("Skipped CM data load");
	}
}
 
Example 9
Source File: SagaConsumer.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * 执行@SagaTask注解的方法
 */
private SagaTaskInstanceDTO invoke(final SagaTaskInstanceDTO data) {
    final SagaTaskInvokeBean invokeBean = invokeBeanMap.get(data.getSagaCode() + data.getTaskCode());
    final SagaTask sagaTask = invokeBean.sagaTask;
    PlatformTransactionManager platformTransactionManager = getSagaTaskTransactionManager(sagaTask.transactionManager());
    TransactionStatus status = createTransactionStatus(transactionManager, sagaTask.transactionIsolation().value());
    beforeInvoke(data.getUserDetails());
    try {
        invokeBean.method.setAccessible(true);
        final Object result = invokeBean.method.invoke(invokeBean.object, data.getInput());
        consumerClient.updateStatus(data.getId(),
                UpdateStatusDTO.UpdateStatusDTOBuilder.newInstance()
                        .withStatus(SagaDefinition.TaskInstanceStatus.COMPLETED.name())
                        .withOutput(resultToJson(result, objectMapper))
                        .withId(data.getId())
                        .withObjectVersionNumber(data.getObjectVersionNumber()).build());
        runningTasks.remove(data.getId());
        platformTransactionManager.commit(status);
    } catch (Exception e) {
        LOGGER.info("@SagaTask method code: {}, id: {} invoke error", data.getTaskCode(), data.getId(), getLoggerException(e));
        String errorMsg = getErrorInfoFromException(e);
        invokeError(platformTransactionManager, status, data, errorMsg);
    } finally {
        afterInvoke();
    }
    return data;
}
 
Example 10
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public Integer getSeqByMysql(String seqKey){

		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
			String sql="select get_micro_seq('"+seqKey+"') as seq";
			List retList=getInnerDao().queryObjJoinByCondition(sql);
			if(retList==null){
				transactionManager.commit(status);
				return null;
			}
			Map retMap=(Map) retList.get(0);
			Integer seq=(Integer) retMap.get("seq");
			transactionManager.commit(status);
			return seq;	    	
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw new RuntimeException("getseq error",ex);
	    }


	}
 
Example 11
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
public Integer getSeqByMysql(String seqKey){

		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
			String sql="select get_micro_seq('"+seqKey+"') as seq";
			List retList=getInnerDao().queryObjJoinByCondition(sql);
			if(retList==null){
				transactionManager.commit(status);
				return null;
			}
			Map retMap=(Map) retList.get(0);
			Integer seq=(Integer) retMap.get("seq");
			transactionManager.commit(status);
			return seq;	    	
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw new RuntimeException("getseq error",ex);
	    }


	}
 
Example 12
Source File: ChainedTransactionManager.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 * {@inheritDoc}
 */
public void commit( TransactionStatus status )
{
    for ( PlatformTransactionManager transactionManager : _transactionManagers )
    {
        TransactionStatus transactionStatus = null;

        try
        {
            transactionStatus = ( (MultiTransactionStatus) status ).getTransactionStatus( transactionManager );
            transactionManager.commit( transactionStatus );
        }
        catch( Exception e )
        {
            _log.error( e.getMessage( ), e );
        }
    }

    if ( _log.isDebugEnabled( ) )
    {
        _log.debug( "Ending transaction : " + status.toString( ) );
    }

    if ( ( (MultiTransactionStatus) status ).isNewSynchonization( ) )
    {
        TransactionSynchronizationManager.clear( );
    }
}
 
Example 13
Source File: MultipleDataSourcesTransactionManager.java    From cobarclient with Apache License 2.0 5 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
	@SuppressWarnings("unchecked")
	List<DefaultTransactionStatus> list = 
	      (List<DefaultTransactionStatus>) status.getTransaction();

	logger.info("prepare to commit transactions on multiple data sources.");
	Validate.isTrue(list.size() <= this.getTransactionManagers().size());

	TransactionException lastException = null;
	for(int i=list.size()-1; i>=0;i--){
		PlatformTransactionManager transactionManager=this.getTransactionManagers().get(i);
		TransactionStatus localTransactionStatus=list.get(i);
		
		try{
			transactionManager.commit(localTransactionStatus);
		}
		catch (TransactionException e) {
			lastException=e;
			logger.error("Error in commit", e);

		}
	}
	if (lastException != null) {
		throw lastException;
		// Rollback will ensue as long as rollbackOnCommitFailure=true
	}

}
 
Example 14
Source File: BrowseExecute.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void deleteMessage(IMessageBrowser mb, String messageId, PlatformTransactionManager transactionManager) throws Throwable {
	TransactionStatus txStatus = null;
	try {
		txStatus = transactionManager.getTransaction(TXNEW);
		mb.deleteMessage(messageId);
	} catch (Throwable e) {
		txStatus.setRollbackOnly();
		error(", ", "errors.generic", "error occured deleting message", e);
		throw e;
	} finally { 
		transactionManager.commit(txStatus);
	}
}
 
Example 15
Source File: SpringTransaction.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
public void run(PlatformTransactionManager transactionManager, DataSource dataSource, TransactionDefinition def) throws Exception {
    TransactionStatus txnStatus = transactionManager.getTransaction(def);
    try {
        Connection con = DataSourceUtils.getConnection(dataSource);
        try {
            execute(con);
        } finally {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
    } catch (Exception e) {
        transactionManager.rollback(txnStatus);
        throw e;
    }
    transactionManager.commit(txnStatus);
}
 
Example 16
Source File: JpaFixture.java    From spring-data-dev-tools with Apache License 2.0 3 votes vote down vote up
private void withTransactionalEntityManager(Consumer<EntityManager> consumer) {

		PlatformTransactionManager manager = context.getBean(PlatformTransactionManager.class);
		TransactionStatus status = manager.getTransaction(new DefaultTransactionDefinition());

		EntityManager em = context.getBean(EntityManager.class);

		consumer.accept(em);

		em.flush();
		manager.commit(status);
		em.close();
	}