Java Code Examples for org.springframework.transaction.PlatformTransactionManager#getTransaction()
The following examples show how to use
org.springframework.transaction.PlatformTransactionManager#getTransaction() .
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: SampleDataLoader.java From sakai with Educational Community License v2.0 | 6 votes |
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 2
Source File: StartCompensationHandler.java From Raincat with GNU Lesser General Public License v3.0 | 6 votes |
/** * 补偿的时候,不走分布式事务处理. * * @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 |
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 4
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 6 votes |
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 |
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: IbisTransaction.java From iaf with Apache License 2.0 | 6 votes |
public IbisTransaction(PlatformTransactionManager txManager, TransactionDefinition txDef, String object) { this.txManager = txManager; this.txDef = txDef; this.object = object; txClientIsActive = TransactionSynchronizationManager.isActualTransactionActive(); txClientName = TransactionSynchronizationManager.getCurrentTransactionName(); txStatus = txManager.getTransaction(txDef); txIsActive = TransactionSynchronizationManager.isActualTransactionActive(); txIsNew = txStatus.isNewTransaction(); if (txIsNew) { txName = Misc.createSimpleUUID(); TransactionSynchronizationManager.setCurrentTransactionName(txName); int txTimeout = txDef.getTimeout(); log.debug("Transaction manager ["+getRealTransactionManager()+"] created a new transaction ["+txName+"] for " + object + " with timeout [" + (txTimeout<0?"system default(=120s)":""+txTimeout) + "]"); } else { txName = TransactionSynchronizationManager.getCurrentTransactionName(); if (txClientIsActive && !txIsActive) { log.debug("Transaction manager ["+getRealTransactionManager()+"] suspended the transaction [" + txClientName + "] for " + object); } } }
Example 7
Source File: SampleDataLoader.java From sakai with Educational Community License v2.0 | 6 votes |
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: TransactionAspectSupport.java From java-technology-stack with MIT License | 5 votes |
/** * Create a transaction if necessary based on the given TransactionAttribute. * <p>Allows callers to perform custom TransactionAttribute lookups through * the TransactionAttributeSource. * @param txAttr the TransactionAttribute (may be {@code null}) * @param joinpointIdentification the fully qualified method name * (used for monitoring and logging purposes) * @return a TransactionInfo object, whether or not a transaction was created. * The {@code hasTransaction()} method on TransactionInfo can be used to * tell if there was a transaction created. * @see #getTransactionAttributeSource() */ @SuppressWarnings("serial") protected TransactionInfo createTransactionIfNecessary(@Nullable PlatformTransactionManager tm, @Nullable TransactionAttribute txAttr, final String joinpointIdentification) { // If no name specified, apply method identification as transaction name. if (txAttr != null && txAttr.getName() == null) { txAttr = new DelegatingTransactionAttribute(txAttr) { @Override public String getName() { return joinpointIdentification; } }; } TransactionStatus status = null; if (txAttr != null) { if (tm != null) { status = tm.getTransaction(txAttr); } else { if (logger.isDebugEnabled()) { logger.debug("Skipping transactional joinpoint [" + joinpointIdentification + "] because no transaction manager has been configured"); } } } return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); }
Example 9
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 5 votes |
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 10
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 5 votes |
public void dbTranNestRollbackAndThrow() throws Exception{ PlatformTransactionManager transactionManager=MicroTranManagerHolder.getTransactionManager(dbName); DefaultTransactionDefinition def =new DefaultTransactionDefinition(); def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status=transactionManager.getTransaction(def); transactionManager.rollback(status); throw new RuntimeException("dbTranNestRollbackAndThrow"); }
Example 11
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 5 votes |
public void dbTranNestRollback() throws Exception{ PlatformTransactionManager transactionManager=MicroTranManagerHolder.getTransactionManager(dbName); DefaultTransactionDefinition def =new DefaultTransactionDefinition(); def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status=transactionManager.getTransaction(def); transactionManager.rollback(status); }
Example 12
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 5 votes |
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 13
Source File: MicroServiceTemplateSupport.java From nh-micro with Apache License 2.0 | 5 votes |
public void dbTranNestRollback() throws Exception{ PlatformTransactionManager transactionManager=MicroTranManagerHolder.getTransactionManager(dbName); DefaultTransactionDefinition def =new DefaultTransactionDefinition(); def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status=transactionManager.getTransaction(def); transactionManager.rollback(status); }
Example 14
Source File: SpringTransaction.java From copper-engine with Apache License 2.0 | 5 votes |
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 15
Source File: BrowseExecute.java From iaf with Apache License 2.0 | 5 votes |
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 16
Source File: MultipleDataSourcesTransactionManager.java From cobarclient with Apache License 2.0 | 5 votes |
/** * We need to disable transaction synchronization so that the shared * transaction synchronization state will not collide with each other. BUT, * for LOB creators to use, we have to pay attention here: * <ul> * <li>if the LOB creator use standard preparedStatement methods, this * transaction synchronization setting is OK;</li> * <li>if the LOB creator don't use standard PS methods, you have to find * other way to make sure the resources your LOB creator used should be * cleaned up after the transaction.</li> * </ul> */ @Override protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException { @SuppressWarnings("unchecked") List<DefaultTransactionStatus> list = (List<DefaultTransactionStatus>) transactionObject; for (PlatformTransactionManager transactionManager : transactionManagers) { DefaultTransactionStatus element = (DefaultTransactionStatus) transactionManager .getTransaction(transactionDefinition); list.add(element); } }
Example 17
Source File: AbstractAsgardConsumer.java From choerodon-starters with Apache License 2.0 | 5 votes |
protected TransactionStatus createTransactionStatus(final PlatformTransactionManager transactionManager, final int isolationLevel) { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); def.setIsolationLevel(isolationLevel); return transactionManager.getTransaction(def); }
Example 18
Source File: PayrateImportServiceImpl.java From kfs with GNU Affero General Public License v3.0 | 5 votes |
/** * If retrieving budget locks fails, this method rolls back previous changes * */ protected void doRollback() { PlatformTransactionManager transactionManager = SpringContext.getBean(PlatformTransactionManager.class); DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transactionStatus = transactionManager.getTransaction(defaultTransactionDefinition); transactionManager.rollback( transactionStatus ); }
Example 19
Source File: MultiDataSourcesTransactionManager.java From uncode-dal-all with GNU General Public License v2.0 | 4 votes |
@Override public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { MultiDataSourcesTransactionStatus transactionStatus = new MultiDataSourcesTransactionStatus(); log.debug("Operation '" + definition.getName() + "' starting transaction."); for (DataSource dataSource : dataSources) { DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(definition); defaultTransactionDefinition.setName(definition.getName()); PlatformTransactionManager txManager = this.transactionManagers.get(dataSource); TransactionStatus status = txManager.getTransaction(defaultTransactionDefinition); TransactionSynchronizationManager.setCurrentTransactionName(defaultTransactionDefinition.getName()); transactionStatus.put(dataSource, status); } return transactionStatus; }
Example 20
Source File: JpaFixture.java From spring-data-dev-tools with Apache License 2.0 | 3 votes |
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(); }