List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager hasResource
public static boolean hasResource(Object key)
From source file:org.guzz.web.context.spring.GuzzTransactionManager.java
@Override protected void doResume(Object transaction, Object suspendedResources) { SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources; if (TransactionSynchronizationManager.hasResource(getTransactionManager())) { // From non-transactional code running in active transaction synchronization // -> can be safely removed, will be closed on transaction completion. TransactionSynchronizationManager.unbindResource(getTransactionManager()); }// ww w . jav a2 s. c o m TransactionSynchronizationManager.bindResource(getTransactionManager(), resourcesHolder.getSessionHolder()); }
From source file:com.github.rholder.spring.transaction.TransactionBindingSupport.java
/** * Cleans out transaction resources if present *//*w w w . java 2s .co m*/ private static void clearSynchronization() { if (TransactionSynchronizationManager.hasResource(RESOURCE_KEY_TXN_SYNCH)) { Object txnSynch = TransactionSynchronizationManager.unbindResource(RESOURCE_KEY_TXN_SYNCH); // done if (logger.isDebugEnabled()) { logger.debug("Unbound txn synch:" + txnSynch); } } }
From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java
protected boolean shouldPassReadOnlyToHibernate() { if ((passReadOnlyToHibernate || osivReadOnly) && TransactionSynchronizationManager.hasResource(getSessionFactory())) { if (TransactionSynchronizationManager.isActualTransactionActive()) { return passReadOnlyToHibernate && TransactionSynchronizationManager.isCurrentTransactionReadOnly(); } else {/*from ww w . j a v a 2s.com*/ return osivReadOnly; } } else { return false; } }
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 {//from w w w . ja v a 2s. co m 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:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
@Test public void testJtaTransactionWithConnectionHolderStillBound() throws Exception { @SuppressWarnings("serial") JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) { @Override// w w w .j a v a2s . co 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
@Test public void testJtaTransactionWithIsolationLevelContentSourceAdapter() throws Exception { given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); final IsolationLevelContentSourceAdapter dsToUse = new IsolationLevelContentSourceAdapter(); dsToUse.setTargetContentSource(contentSource); dsToUse.afterPropertiesSet();/* w ww .j av a 2 s . co m*/ JtaTransactionManager ptm = new JtaTransactionManager(userTransaction); ptm.setAllowCustomIsolationLevels(true); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(session, c); ContentSourceUtils.releaseSession(c, dsToUse); } }); tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); tt.setReadOnly(true); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(session, c); ContentSourceUtils.releaseSession(c, dsToUse); } }); verify(userTransaction, times(2)).begin(); verify(userTransaction, times(2)).commit(); verify(session).setTransactionMode(Session.TransactionMode.QUERY); verify(session, times(2)).close(); }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
private void doTestJtaTransactionWithIsolationLevelContentSourceRouter() throws Exception { given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); final ContentSource contentSource1 = mock(ContentSource.class); final Session session1 = mock(Session.class); given(contentSource1.newSession()).willReturn(session1); final ContentSource contentSource2 = mock(ContentSource.class); final Session session2 = mock(Session.class); given(contentSource2.newSession()).willReturn(session2); final IsolationLevelContentSourceRouter dsToUse = new IsolationLevelContentSourceRouter(); Map<Object, Object> targetContentSources = new HashMap<Object, Object>(); targetContentSources.put("ISOLATION_REPEATABLE_READ", contentSource2); dsToUse.setDefaultTargetContentSource(contentSource1); dsToUse.setTargetContentSources(targetContentSources); dsToUse.afterPropertiesSet();//from w w w . j a v a 2 s .co m JtaTransactionManager ptm = new JtaTransactionManager(userTransaction); ptm.setAllowCustomIsolationLevels(true); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(session1, c); ContentSourceUtils.releaseSession(c, dsToUse); } }); tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { Session c = ContentSourceUtils.getSession(dsToUse); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse)); assertSame(session2, c); ContentSourceUtils.releaseSession(c, dsToUse); } }); verify(userTransaction, times(2)).begin(); verify(userTransaction, times(2)).commit(); verify(session1).close(); verify(session2).close(); }
From source file:nl.strohalm.cyclos.utils.TransactionHelperImpl.java
@Override public boolean hasActiveTransaction() { return TransactionSynchronizationManager.hasResource(sessionFactory); }
From source file:ome.tools.hibernate.SessionStatus.java
public void cleanThread() { if (TransactionSynchronizationManager.hasResource(factory)) { SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory); if (holder == null) { throw new IllegalStateException("Can't be null."); } else if (holder == DUMMY) { TransactionSynchronizationManager.unbindResource(factory); } else {/*from w w w .java2s. c o m*/ throw new IllegalStateException("Thread corrupted."); } } }
From source file:ome.tools.hibernate.SessionStatus.java
private void bindSession(Session session) { debug("Binding session to thread."); SessionHolder sessionHolder = new SessionHolder(session); sessionHolder.setTransaction(sessionHolder.getSession().beginTransaction()); // FIXME TODO // If we reach this point, it's ok to bind the new SessionHolder, // however the DUMMY EmptySessionHolder may be present so unbind // just in case. if (TransactionSynchronizationManager.hasResource(factory)) { TransactionSynchronizationManager.unbindResource(factory); }/*www.j a v a 2 s. c o m*/ TransactionSynchronizationManager.bindResource(factory, sessionHolder); if (!TransactionSynchronizationManager.isSynchronizationActive()) { throw new InternalException("Synchronization not active for " + "TransactionSynchronizationManager"); } }