List of usage examples for org.springframework.transaction TransactionStatus isNewTransaction
boolean isNewTransaction();
From source file:com.chillenious.common.db.jooq.TransactionalMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { if (invocation.getThis() != null) { TransactionAttribute txAttribute = annotationSource.getTransactionAttribute(invocation.getMethod(), invocation.getThis().getClass()); TransactionStatus transaction = transactionManager.getTransaction(txAttribute); try {/* ww w . ja v a 2s . com*/ Object result = invocation.proceed(); try { if (transaction.isNewTransaction()) { transactionManager.commit(transaction); } } catch (UnexpectedRollbackException ignore) { } return result; } catch (Exception e) { if (transaction.isNewTransaction()) { transactionManager.rollback(transaction); } throw e; } } else { // not supported on static methods return invocation.proceed(); } }
From source file:com.eclecticlogic.pedal.impl.TransactionImpl.java
public <R> R _exec(Function<Context, R> runner) { TransactionStatus status = null; try {//from ww w . j a v a 2 s. c om status = getPlatformTransactionManager().getTransaction(getTransactionDefinition()); logger.trace("start: new = {}", status.isNewTransaction() ? "yes" : "no"); Context context = new ContextImpl(); R retval = runner.apply(context); if (status.isRollbackOnly()) { getPlatformTransactionManager().rollback(status); logger.trace("rollback"); } else { getPlatformTransactionManager().commit(status); logger.trace("commit"); } return retval; } catch (RuntimeException e) { if (status != null && status.isCompleted() == false) { getPlatformTransactionManager().rollback(status); logger.trace("rollback"); } throw e; } }
From source file:org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager.java
/** * Borrowed from Seam/*from w w w .j a v a 2 s . c o m*/ */ 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; try { if (currentTransaction == null) { transaction = ptm.getTransaction(td); if (transaction.isNewTransaction()) { return TransactionManager.STATUS_COMMITTED; } } else { transaction = currentTransaction; } 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 { // Using the commented-out code in means that if rollback with this manager, // I always have to catch and check the exception // because ROLLEDBACK can mean both "rolled back" and "rollback only". // if ( transaction.isRollbackOnly() ) { // return TransactionManager.STATUS_ROLLEDBACK; // } return TransactionManager.STATUS_ACTIVE; } } finally { if (currentTransaction == null) { ptm.commit(transaction); } } } return TransactionManager.STATUS_NO_TRANSACTION; }
From source file:org.drools.container.spring.beans.persistence.HumanTaskSpringTransactionManager.java
/** * Borrowed from Seam/*from w w w .j ava 2 s . c o m*/ */ 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; }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
@Test public void testJtaTransactionWithConnectionHolderStillBound() throws Exception { @SuppressWarnings("serial") JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) { @Override//from w w w . j a v a 2 s .c o m protected void doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject, final List<TransactionSynchronization> synchronizations) throws RollbackException, SystemException { Thread async = new Thread() { @Override public void run() { invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED); } }; async.start(); try { async.join(); } catch (InterruptedException ex) { ex.printStackTrace(); } } }; TransactionTemplate tt = new TransactionTemplate(ptm); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(contentSource)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE); for (int i = 0; i < 3; i++) { final boolean releaseCon = (i != 1); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is existing transaction", !status.isNewTransaction()); Session c = ContentSourceUtils.getSession(contentSource); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(contentSource)); ContentSourceUtils.releaseSession(c, contentSource); c = ContentSourceUtils.getSession(contentSource); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(contentSource)); if (releaseCon) { ContentSourceUtils.releaseSession(c, contentSource); } } }); if (!releaseCon) { assertTrue("Still has session holder", TransactionSynchronizationManager.hasResource(contentSource)); } else { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(contentSource)); } assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); } verify(session, times(3)).close(); }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
private void doTestJtaTransaction(final boolean rollback) throws Exception { if (rollback) { given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE); } else {/*from w w w .j a v a 2 s . c o m*/ given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); } JtaTransactionManager ptm = new JtaTransactionManager(userTransaction); TransactionTemplate tt = new TransactionTemplate(ptm); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(contentSource)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(contentSource)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Session c = ContentSourceUtils.getSession(contentSource); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(contentSource)); ContentSourceUtils.releaseSession(c, contentSource); c = ContentSourceUtils.getSession(contentSource); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(contentSource)); ContentSourceUtils.releaseSession(c, contentSource); if (rollback) { status.setRollbackOnly(); } } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(contentSource)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(userTransaction).begin(); if (rollback) { verify(userTransaction).rollback(); } verify(session).close(); }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
private void doTestJtaTransactionWithPropagationRequiresNew(final boolean rollback, final boolean openOuterConnection, final boolean accessAfterResume, final boolean useTransactionAwareContentSource) throws Exception { given(transactionManager.suspend()).willReturn(transaction); if (rollback) { given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE); } else {/*from w w w .j ava2 s. c o m*/ given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); } given(session.getTransactionMode()).willReturn(Session.TransactionMode.QUERY); final ContentSource dsToUse = useTransactionAwareContentSource ? new TransactionAwareContentSourceProxy(contentSource) : contentSource; JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager); final TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Session s = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); s.getTransactionMode(); ContentSourceUtils.releaseSession(s, dsToUse); s = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); if (!openOuterConnection) { ContentSourceUtils.releaseSession(s, dsToUse); } for (int i = 0; i < 5; i++) { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Session s = ContentSourceUtils.getSession(dsToUse); s.getTransactionMode(); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); ContentSourceUtils.releaseSession(s, dsToUse); s = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); ContentSourceUtils.releaseSession(s, dsToUse); } }); } if (rollback) { status.setRollbackOnly(); } if (accessAfterResume) { if (!openOuterConnection) { s = ContentSourceUtils.getSession(dsToUse); } assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); s.getTransactionMode(); ContentSourceUtils.releaseSession(s, dsToUse); s = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); ContentSourceUtils.releaseSession(s, dsToUse); } else { if (openOuterConnection) { ContentSourceUtils.releaseSession(s, dsToUse); } } } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(userTransaction, times(6)).begin(); verify(transactionManager, times(5)).resume(transaction); if (rollback) { verify(userTransaction, times(5)).commit(); verify(userTransaction).rollback(); } else { verify(userTransaction, times(6)).commit(); } if (accessAfterResume && !openOuterConnection) { verify(session, times(7)).close(); } else { verify(session, times(6)).close(); } }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException, final boolean openOuterConnection, final boolean useTransactionAwareContentSource) throws Exception { given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); if (suspendException) { given(transactionManager.suspend()).willThrow(new SystemException()); } else {// w w w .j a va 2 s . c om given(transactionManager.suspend()).willReturn(transaction); willThrow(new SystemException()).given(userTransaction).begin(); } given(session.getTransactionMode()).willReturn(Session.TransactionMode.QUERY); final ContentSource dsToUse = useTransactionAwareContentSource ? new TransactionAwareContentSourceProxy(contentSource) : contentSource; if (dsToUse instanceof TransactionAwareContentSourceProxy) { ((TransactionAwareContentSourceProxy) dsToUse).setReobtainTransactionalSessions(true); } JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager); final TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); c.getTransactionMode(); ContentSourceUtils.releaseSession(c, dsToUse); c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); if (!openOuterConnection) { ContentSourceUtils.releaseSession(c, dsToUse); } try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Is new transaction", status.isNewTransaction()); Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); ContentSourceUtils.releaseSession(c, dsToUse); c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); ContentSourceUtils.releaseSession(c, dsToUse); } }); } finally { if (openOuterConnection) { c.getTransactionMode(); ContentSourceUtils.releaseSession(c, dsToUse); } } } }); fail("Should have thrown TransactionException"); } catch (TransactionException ex) { // expected } assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(userTransaction).begin(); if (suspendException) { verify(userTransaction).rollback(); } if (suspendException) { verify(session, atLeastOnce()).close(); } else { verify(session, never()).close(); } }
From source file:se.vgregion.service.innovationsslussen.idea.IdeaServiceImpl.java
@Override //@Transactional(rollbackFor = BariumException.class) public UpdateFromBariumResult updateFromBarium(Idea idea) throws UpdateIdeaException { // Do the transaction manually since we may run this in a separate thread. TransactionStatus transaction = transactionManager .getTransaction(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRES_NEW)); try {// w ww.ja v a 2 s . co m UpdateFromBariumResult result = new UpdateFromBariumResult(); result.setOldIdea(find(idea.getId())); result.getOldIdea().getIdeaContentPrivate(); result.getOldIdea().getIdeaContentPublic(); LOGGER.info(" Update from Barium, idea: " + idea.getTitle()); // Idea idea = findIdeaByBariumId(id); final String ideaId = idea.getId(); String oldTitle = idea.getTitle(); // To know whether it has changed, in case we need to update in Barium // Make two calls asynchronously and simultaneously to speed up. Future<IdeaObjectFields> ideaObjectFieldsFuture = bariumService.asyncGetIdeaObjectFields(ideaId); Future<String> bariumIdeaPhase = bariumService.asyncGetIdeaPhaseFuture(ideaId); int bariumPhase = 0; int currentPhase = Integer.parseInt(idea.getPhase() == null ? "0" : idea.getPhase()); IdeaStatus oldStatus = idea.getStatus(); try { if (bariumIdeaPhase.get() == null) { throw new BariumException("bariumIdeaPhase is null"); } bariumPhase = Integer.parseInt(bariumIdeaPhase.get()); populateIdea(ideaObjectFieldsFuture.get(), idea); //Sync files if (idea.getIdeaContentPrivate() != null) { populateFile(idea, idea.getIdeaContentPrivate(), LIFERAY_CLOSED_DOCUMENTS); } if (idea.getIdeaContentPublic() != null) { populateFile(idea, idea.getIdeaContentPublic(), LIFERAY_OPEN_DOCUMENTS); } } catch (InterruptedException e) { throw new UpdateIdeaException(e); } catch (ExecutionException e) { throw new UpdateIdeaException(e); } result.setChanged(!isIdeasTheSame(idea, result.getOldIdea())); final String finalUrlTitle; if (!oldTitle.equals(idea.getTitle())) { finalUrlTitle = generateNewUrlTitle(idea.getTitle()); idea.setUrlTitle(finalUrlTitle); } else { idea.setUrlTitle(result.getOldIdea().getUrlTitle()); finalUrlTitle = null; } if (idea.getOriginalUserId() == null) { idea.setOriginalUserId(0L); } idea = ideaRepository.merge(idea); result.setNewIdea(idea); if (finalUrlTitle != null) { final Idea finalIdea = idea; // We may just as well do this asynchronously since we don't throw anything and don't return anything // from // here. executor.submit(new Runnable() { @Override public void run() { // We need to update the ideaSiteLink in Barium. String ideaSiteLink = generateIdeaSiteLink(schemeServerNameUrl, finalUrlTitle); try { bariumService.updateIdea(finalIdea.getId(), "siteLank", ideaSiteLink); } catch (BariumException e) { LOGGER.error("Failed to update idea " + finalIdea.getId() + " with new site link: " + ideaSiteLink, e); } } }); } if (currentPhase != bariumPhase) { idea.setPhase("" + (bariumPhase)); result.setChanged(true); } // Add auto-comments to the idea. idea = generateAutoComments(idea, oldStatus, currentPhase, bariumPhase); // Sync comments count idea.setCommentsCount(getPrivateCommentsCount(idea)); idea = ideaRepository.merge(idea); // Sync last comment date List<CommentItemVO> privateComments = getComments(idea.getIdeaContentPrivate()); if (privateComments.size() > 0) { Collections.sort(privateComments, new Comparator<CommentItemVO>() { @Override public int compare(CommentItemVO o1, CommentItemVO o2) { return -o1.getCreateDate().compareTo(o2.getCreateDate()); } }); idea.setLastPrivateCommentDate(privateComments.get(0).getCreateDate()); } if (transaction.isNewTransaction()) { transactionManager.commit(transaction); } return result; } catch (BariumException be) { transactionManager.rollback(transaction); LOGGER.warn(be.getMessage()); } finally { if (!transaction.isCompleted()) { //If this happens, a runtimeexception has likley occurred. transactionManager.rollback(transaction); LOGGER.warn("Rolledback transaction because of likely RunTimeException"); } } return null; }