Java Code Examples for org.springframework.transaction.TransactionStatus#isCompleted()
The following examples show how to use
org.springframework.transaction.TransactionStatus#isCompleted() .
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: TransactionalTestExecutionListener.java From spring-analysis-note with MIT License | 6 votes |
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if (transactionStatus != null && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }
Example 2
Source File: TransactionalTestExecutionListener.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if ((transactionStatus != null) && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }
Example 3
Source File: TransactionHandlerInterceptor.java From rice with Educational Community License v2.0 | 6 votes |
/** * Completes the request transaction if needed. * * @param ex any exception that might have been thrown, will cause a rollback */ protected void completeTransaction(Exception ex) { TransactionStatus status = context.get(); if (status == null) { return; } try { if (!status.isCompleted()) { if (ex == null && !status.isRollbackOnly()) { txManager.commit(status); } else { txManager.rollback(status); } } } finally { context.remove(); } }
Example 4
Source File: TransactionalTestExecutionListener.java From java-technology-stack with MIT License | 6 votes |
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if (transactionStatus != null && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }
Example 5
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 6
Source File: AbstractPlatformTransactionManager.java From spring-analysis-note with MIT License | 5 votes |
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * @see #doRollback * @see #doSetRollbackOnly */ @Override public final void rollback(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; processRollback(defStatus, false); }
Example 7
Source File: RetriableTransactionInterceptor.java From HA-DB with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Around("@annotation(com.hitler.common.aop.annotation.RetriableTransaction)") public Object retry(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); RetriableTransaction annotation = method.getAnnotation(RetriableTransaction.class); int maxAttempts = annotation.maxRetries(); int attemptCount = 0; List<Class<? extends Throwable>> exceptions = Arrays.asList(annotation.retryFor()); Throwable failure = null; TransactionStatus currentTransactionStatus = null; String businessName = pjp.getTarget().toString(); businessName = businessName.substring(0, businessName.lastIndexOf("@")) + "." + method.getName(); do { attemptCount++; try { DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); currentTransactionStatus = transactionManager.getTransaction(transactionDefinition); Object returnValue = pjp.proceed(); transactionManager.commit(currentTransactionStatus); return returnValue; } catch (Throwable t) { if (!exceptions.contains(t.getClass())) { throw t; } if (currentTransactionStatus != null && !currentTransactionStatus.isCompleted()) { transactionManager.rollback(currentTransactionStatus); failure = t; } LOGGER.debug("事务重试:["+businessName+":"+attemptCount+"/"+maxAttempts+"]"); } } while (attemptCount < maxAttempts); LOGGER.debug("事务重试:["+businessName+":已达最大重试次数]"); throw failure; }
Example 8
Source File: HibernateJtaTransactionManagerAdapter.java From gorm-hibernate5 with Apache License 2.0 | 5 votes |
protected static int convertToJtaStatus(TransactionStatus status) { if (status != null) { if (status.isCompleted()) { return Status.STATUS_UNKNOWN; } else if (status.isRollbackOnly()) { return Status.STATUS_MARKED_ROLLBACK; } else { return Status.STATUS_ACTIVE; } } else { return Status.STATUS_NO_TRANSACTION; } }
Example 9
Source File: TestTransaction.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Determine whether a test-managed transaction is currently <em>active</em>. * @return {@code true} if a test-managed transaction is currently active * @see #start() * @see #end() */ public static boolean isActive() { TransactionContext transactionContext = TransactionContextHolder.getCurrentTransactionContext(); if (transactionContext != null) { TransactionStatus transactionStatus = transactionContext.getTransactionStatus(); return (transactionStatus != null) && (!transactionStatus.isCompleted()); } // else return false; }
Example 10
Source File: AbstractPlatformTransactionManager.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * @see #doRollback * @see #doSetRollbackOnly */ @Override public final void rollback(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; processRollback(defStatus); }
Example 11
Source File: AbstractPlatformTransactionManager.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}. * @see org.springframework.transaction.TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override public final void commit(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; if (defStatus.isLocalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Transactional code has requested rollback"); } processRollback(defStatus); return; } if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus); // Throw UnexpectedRollbackException only at outermost transaction boundary // or if explicitly asked to. if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) { throw new UnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); } return; } processCommit(defStatus); }
Example 12
Source File: AbstractPlatformTransactionManager.java From lams with GNU General Public License v2.0 | 5 votes |
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * @see #doRollback * @see #doSetRollbackOnly */ @Override public final void rollback(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; processRollback(defStatus); }
Example 13
Source File: AbstractPlatformTransactionManager.java From lams with GNU General Public License v2.0 | 5 votes |
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}. * @see org.springframework.transaction.TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override public final void commit(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; if (defStatus.isLocalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Transactional code has requested rollback"); } processRollback(defStatus); return; } if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus); // Throw UnexpectedRollbackException only at outermost transaction boundary // or if explicitly asked to. if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) { throw new UnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); } return; } processCommit(defStatus); }
Example 14
Source File: AbstractPlatformTransactionManager.java From spring-analysis-note with MIT License | 5 votes |
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}. * @see org.springframework.transaction.TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override public final void commit(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; if (defStatus.isLocalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Transactional code has requested rollback"); } processRollback(defStatus, false); return; } if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus, true); return; } processCommit(defStatus); }
Example 15
Source File: IndexPerformance.java From sakai with Educational Community License v2.0 | 4 votes |
public void loadLotsOfData() { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = tm.getTransaction(def); try { String asId = "IndexPerf AS"; cmAdmin.createAcademicSession(asId, asId, asId, null, null); String csId = "IndexPerf CS"; cmAdmin.createCourseSet(csId, csId, csId, "DEPT", null); String ccId = "IndexPerf CC"; cmAdmin.createCanonicalCourse(ccId, ccId, ccId); String coId = "IndecPerf CO"; cmAdmin.createCourseOffering(coId, csId, csId, "open", asId, ccId, null, null); for(int i = 1; i <= secCount; i++) { String esId = esPrefix + i; Set<String> instructors = new HashSet<String>(); instructors.add("instructor_A_" + i); instructors.add("instructor_B_" + i); instructors.add("instructor_C_" + i); cmAdmin.createEnrollmentSet(esId, esId, esId, "lecture", "3", coId, instructors); } for(int i = 1; i <= secCount; i++) { String secId = secPrefix + i; cmAdmin.createSection(secId, secId, secId, "lecture", null, coId, (esPrefix + i)); } for(int i = 1; i <= secCount; i++) { for(int j = 1; j <= enrollmentsPerEnrollmentSet; j++) { cmAdmin.addOrUpdateEnrollment("student" + j, esPrefix + i, "enrolled", "3", "letter grade"); } } for(int i = 1; i <= secCount; i++) { for(int j = 1; j <= membersPerSection; j++) { cmAdmin.addOrUpdateSectionMembership("student" + j, "some role", secPrefix + i, "some status"); } } } catch (Exception e) { tm.rollback(status); } finally { if(!status.isCompleted()) { tm.commit(status); } } }
Example 16
Source File: SpringTransactionHandler.java From micronaut-sql with Apache License 2.0 | 4 votes |
@Override public boolean isInTransaction(Handle handle) { TransactionStatus status = getTransactionStatus(handle); return status != null && !status.isCompleted(); }
Example 17
Source File: TransactionUtil.java From mPass with Apache License 2.0 | 4 votes |
/** * 回滚事务 */ public static void rollback(TransactionStatus status) { if (!status.isCompleted()) { getTransactionManager().rollback(status); } }
Example 18
Source File: TransactionUtil.java From mPaaS with Apache License 2.0 | 4 votes |
/** * 回滚事务 */ public static void rollback(TransactionStatus status) { if (!status.isCompleted()) { getTransactionManager().rollback(status); } }
Example 19
Source File: IndexPerformance.java From sakai with Educational Community License v2.0 | 4 votes |
public void loadLotsOfData() { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = tm.getTransaction(def); try { String asId = "IndexPerf AS"; cmAdmin.createAcademicSession(asId, asId, asId, null, null); String csId = "IndexPerf CS"; cmAdmin.createCourseSet(csId, csId, csId, "DEPT", null); String ccId = "IndexPerf CC"; cmAdmin.createCanonicalCourse(ccId, ccId, ccId); String coId = "IndecPerf CO"; cmAdmin.createCourseOffering(coId, csId, csId, "open", asId, ccId, null, null); for(int i = 1; i <= secCount; i++) { String esId = esPrefix + i; Set<String> instructors = new HashSet<String>(); instructors.add("instructor_A_" + i); instructors.add("instructor_B_" + i); instructors.add("instructor_C_" + i); cmAdmin.createEnrollmentSet(esId, esId, esId, "lecture", "3", coId, instructors); } for(int i = 1; i <= secCount; i++) { String secId = secPrefix + i; cmAdmin.createSection(secId, secId, secId, "lecture", null, coId, (esPrefix + i)); } for(int i = 1; i <= secCount; i++) { for(int j = 1; j <= enrollmentsPerEnrollmentSet; j++) { cmAdmin.addOrUpdateEnrollment("student" + j, esPrefix + i, "enrolled", "3", "letter grade"); } } for(int i = 1; i <= secCount; i++) { for(int j = 1; j <= membersPerSection; j++) { cmAdmin.addOrUpdateSectionMembership("student" + j, "some role", secPrefix + i, "some status"); } } } catch (Exception e) { tm.rollback(status); } finally { if(!status.isCompleted()) { tm.commit(status); } } }
Example 20
Source File: FixCoreferenceFeatures.java From webanno with Apache License 2.0 | 4 votes |
private void doMigration() { long start = System.currentTimeMillis(); DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setName("migrationRoot"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = null; try { status = txManager.getTransaction(def); Query q = entityManager.createQuery( "UPDATE AnnotationFeature \n" + "SET type = :fixedType \n" + "WHERE type = :oldType \n" + "AND name in (:featureNames)"); // This condition cannot be applied: // "AND layer.type = :layerType" // q.setParameter("layerType", CHAIN_TYPE); // http://stackoverflow.com/questions/16506759/hql-is-generating-incomplete-cross-join-on-executeupdate // However, the risk that the migration upgrades the wrong featuers is still very low // even without this additional condition q.setParameter("featureNames", Arrays.asList(COREFERENCE_RELATION_FEATURE, COREFERENCE_TYPE_FEATURE)); q.setParameter("oldType", "de.tudarmstadt.ukp.dkpro.core.api.coref.type.Coreference"); q.setParameter("fixedType", CAS.TYPE_NAME_STRING); int changed = q.executeUpdate(); if (changed > 0) { log.info("DATABASE UPGRADE PERFORMED: [{}] coref chain features had their " + "type fixed.", changed); } txManager.commit(status); } finally { if (status != null && !status.isCompleted()) { txManager.rollback(status); } } log.info("Migration [" + getClass().getSimpleName() + "] took {}ms", System.currentTimeMillis() - start); }