List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager isSynchronizationActive
public static boolean isSynchronizationActive()
From source file:org.motechproject.server.omod.advice.ObsAdvice.java
/** * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, * java.lang.reflect.Method, java.lang.Object[], java.lang.Object) */// w w w.ja v a 2s . com public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { log.debug("intercepting method invocation: " + method.getName()); Obs obs = (Obs) returnValue; Person person = obs.getPerson(); ScheduleMaintService schedService = contextService.getScheduleMaintService(); if (TransactionSynchronizationManager.isSynchronizationActive()) { schedService.addAffectedPatient(person.getId()); schedService.requestSynch(); } else { // FIXME: Remove this when advice can exec in tx schedService.updateSchedule(person.getId()); } }
From source file:org.openvpms.component.business.dao.hibernate.im.common.Context.java
/** * Returns the context for the given assembler and session and current * thread.//from w ww .ja v a2 s . c o m * <p/> * If one does not exist, it will be created. * * @param assembler the assembler * @param session the hibernate session * @return the context */ public static Context getContext(Assembler assembler, Session session) { Context context; ResourceKey key = new ResourceKey(session); if (TransactionSynchronizationManager.isSynchronizationActive()) { if (!TransactionSynchronizationManager.hasResource(key)) { context = new Context(assembler, session, true); TransactionSynchronizationManager.bindResource(context.getResourceKey(), context); TransactionSynchronizationManager.registerSynchronization(new ContextSynchronization(context)); } else { context = (Context) TransactionSynchronizationManager.getResource(key); } } else { context = new Context(assembler, session, false); } return context; }
From source file:org.grails.datastore.mapping.core.DatastoreUtils.java
/** * Get a Datastore Session for the given Datastore. Is aware of and will * return any existing corresponding Session bound to the current thread, for * example when using {@link org.grails.datastore.mapping.transactions.DatastoreTransactionManager}. Will create a new * Session otherwise, if "allowCreate" is <code>true</code>. * * @param datastore Datastore to create the session with * Session on transaction synchronization (may be <code>null</code>) * @param allowCreate whether a non-transactional Session should be created * when no transactional Session can be found for the current thread * @return the Datastore Session//from w ww . ja v a 2s .co m * @throws IllegalStateException if no thread-bound Session found and * "allowCreate" is <code>false</code> */ public static Session doGetSession(Datastore datastore, boolean allowCreate) { Assert.notNull(datastore, "No Datastore specified"); SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(datastore); if (sessionHolder != null && !sessionHolder.isEmpty()) { // pre-bound Datastore Session Session session; if (TransactionSynchronizationManager.isSynchronizationActive() && sessionHolder.doesNotHoldNonDefaultSession()) { // Spring transaction management is active -> // register pre-bound Session with it for transactional flushing. session = sessionHolder.getValidatedSession(); if (session != null && !sessionHolder.isSynchronizedWithTransaction()) { logger.debug("Registering Spring transaction synchronization for existing Datastore Session"); TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, datastore, false)); sessionHolder.setSynchronizedWithTransaction(true); } if (session != null) { return session; } } else { session = sessionHolder.getValidatedSession(); if (session != null) { return session; } } } logger.debug("Opening Datastore Session"); Session session = datastore.connect(); // Use same Session for further Datastore actions within the transaction. // Thread object will get removed by synchronization at transaction completion. if (TransactionSynchronizationManager.isSynchronizationActive()) { // We're within a Spring-managed transaction, possibly from JtaTransactionManager. logger.debug("Registering Spring transaction synchronization for new Datastore Session"); SessionHolder holderToUse = sessionHolder; if (holderToUse == null) { holderToUse = new SessionHolder(session); } else { holderToUse.addSession(session); } TransactionSynchronizationManager .registerSynchronization(new SpringSessionSynchronization(holderToUse, datastore, true)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(datastore, holderToUse); } } // Check whether we are allowed to return the Session. if (!allowCreate && !isSessionTransactional(session, datastore)) { closeSession(session); throw new IllegalStateException("No Datastore Session bound to thread, " + "and configuration does not allow creation of non-transactional one here"); } return session; }
From source file:org.sakaiproject.orm.ibatis.support.AbstractLobTypeHandler.java
/** * This implementation delegates to setParameterInternal, * passing in a transaction-synchronized LobCreator for the * LobHandler of this type.// w w w . j ava2 s. co m * @see #setParameterInternal */ public final void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException { if (!TransactionSynchronizationManager.isSynchronizationActive()) { throw new IllegalStateException("Spring transaction synchronization needs to be active for " + "setting values in iBATIS TypeHandlers that delegate to a Spring LobHandler"); } final LobCreator lobCreator = this.lobHandler.getLobCreator(); try { setParameterInternal(ps, i, parameter, jdbcType, lobCreator); } catch (IOException ex) { throw new SQLException("I/O errors during LOB access: " + ex.getMessage()); } TransactionSynchronizationManager.registerSynchronization(new LobCreatorSynchronization(lobCreator)); }
From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java
@After public void verifyTransactionSynchronizationManagerState() { assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertNull(TransactionSynchronizationManager.getCurrentTransactionName()); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertNull(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel()); assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); }
From source file:org.openeos.hibernate.internal.SessionFactoryProxyHandler.java
private Object currentSession(Object proxy) { Object value = TransactionSynchronizationManager.getResource(proxy); if (value instanceof Session) { return (Session) value; } else if (value instanceof SessionHolder) { SessionHolder sessionHolder = (SessionHolder) value; Session session = sessionHolder.getSession(); if (TransactionSynchronizationManager.isSynchronizationActive() && !sessionHolder.isSynchronizedWithTransaction()) { throw new UnsupportedOperationException(); }/*from w w w .ja va2s .c o m*/ return session; } else { throw new HibernateException("No Session found for current thread"); } }
From source file:org.vader.common.spring.TransactionScope.java
@Override public void registerDestructionCallback(String name, Runnable callback) { if (!TransactionSynchronizationManager.isSynchronizationActive()) { switch (nonTransactionalBehaviour) { case PROTOTYPE: break; case NOT_ALLOWED: default:// w w w . j a v a 2 s .c om throw new IllegalStateException( String.format("Unable register destruction callback for bean=%s within transaction scope. " + "No active transaction found", name)); } } getOrCreateScopeEntry(name).getCallbacks().add(callback); }
From source file:org.zenoss.zep.dao.impl.DaoCacheImpl.java
private <T> T getNameFromId(final DaoTableCache<T> cache, final int id) { T cached = cache.getCache().getNameFromId(id); if (cached == null) { final T name = cache.findNameFromId(id); if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override/*ww w . j av a 2 s. c o m*/ public void afterCommit() { cache.getCache().cache(name, id); } }); } else { // Not part of a transaction - can safely cache cache.getCache().cache(name, id); } cached = name; } return cached; }
From source file:com.github.rholder.spring.transaction.TransactionBindingSupport.java
/** * @return Returns the read-write state of the current transaction * @since 2.1.4/*from www . java 2s . co m*/ */ public static TxnReadState getTransactionReadState() { if (!TransactionSynchronizationManager.isSynchronizationActive()) { return TxnReadState.TXN_NONE; } // Find the read-write state of the txn if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { return TxnReadState.TXN_READ_ONLY; } else { return TxnReadState.TXN_READ_WRITE; } }