當前位置: 首頁>>代碼示例>>Java>>正文


Java TransactionSynchronizationManager.isSynchronizationActive方法代碼示例

本文整理匯總了Java中org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionSynchronizationManager.isSynchronizationActive方法的具體用法?Java TransactionSynchronizationManager.isSynchronizationActive怎麽用?Java TransactionSynchronizationManager.isSynchronizationActive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.transaction.support.TransactionSynchronizationManager的用法示例。


在下文中一共展示了TransactionSynchronizationManager.isSynchronizationActive方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getTransactionReadState

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * @return      Returns the read-write state of the current transaction
 * @since 2.1.4
 */
public static TxnReadState getTransactionReadState()
{
    if (!TransactionSynchronizationManager.isSynchronizationActive())
    {
        return TxnReadState.TXN_NONE;
    }
    // Find the read-write state of the txn
    if (getResource(RESOURCE_KEY_TXN_COMPLETING) != null)
    {
        // Transaction is completing.  For all intents and purposes, we are not in a transaction.
        return TxnReadState.TXN_NONE;
    }
    else if (TransactionSynchronizationManager.isCurrentTransactionReadOnly())
    {
        return TxnReadState.TXN_READ_ONLY;
    }
    else
    {
        return TxnReadState.TXN_READ_WRITE;
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:26,代碼來源:AlfrescoTransactionSupport.java

示例2: intercept

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {

    boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
    //如果沒有事務
    if (!synchronizationActive) {
        Object[] objects = invocation.getArgs();
        MappedStatement ms = (MappedStatement) objects[0];

        DynamicDataSourceGlobal dynamicDataSourceGlobal;

        if ((dynamicDataSourceGlobal = CACHE_MAP.get(ms.getId())) == null) {
            dynamicDataSourceGlobal = getDynamicDataSource(ms, objects[1]);
            LOGGER.warn("設置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
            CACHE_MAP.put(ms.getId(), dynamicDataSourceGlobal);
        }
        DynamicDataSourceHolder.putDataSource(dynamicDataSourceGlobal);
    }

    return invocation.proceed();
}
 
開發者ID:ruyangit,項目名稱:angit,代碼行數:22,代碼來源:DynamicPlugin.java

示例3: getTransactionId

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Get the transaction identifier used to store it in the transaction map.
 * 
 * @param tx Transaction
 * @param storeRef StoreRef
 * @return - the transaction id
 */
@SuppressWarnings("unchecked")
private String getTransactionId(Transaction tx, StoreRef storeRef)
{
    if (tx instanceof SimpleTransaction)
    {
        SimpleTransaction simpleTx = (SimpleTransaction) tx;
        return simpleTx.getGUID();
    }
    else if (TransactionSynchronizationManager.isSynchronizationActive())
    {
        Map<StoreRef, LuceneIndexer> indexers = (Map<StoreRef, LuceneIndexer>) AlfrescoTransactionSupport.getResource(indexersKey);
        if (indexers != null)
        {
            LuceneIndexer indexer = indexers.get(storeRef);
            if (indexer != null)
            {
                return indexer.getDeltaId();
            }
        }
    }
    return null;
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:30,代碼來源:AbstractLuceneIndexerAndSearcherFactory.java

示例4: getTransactionStartTime

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * @return Returns the system time when the transaction started, or -1 if there is no current transaction.
 */
public static long getTransactionStartTime()
{
    /*
     * This method can be called outside of a transaction, so we can go direct to the synchronizations.
     */
    TransactionSynchronizationImpl txnSynch =
        (TransactionSynchronizationImpl) TransactionSynchronizationManager.getResource(RESOURCE_KEY_TXN_SYNCH);
    if (txnSynch == null)
    {
        if (TransactionSynchronizationManager.isSynchronizationActive())
        {
            // need to lazily register synchronizations
            return registerSynchronizations().getTransactionStartTime();
        }
        else
        {
            return -1;   // not in a transaction
        }
    }
    else
    {
        return txnSynch.getTransactionStartTime();
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:28,代碼來源:TransactionSupportUtil.java

示例5: doGetConnection

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Actually obtain a CCI Connection from the given ConnectionFactory.
 * Same as {@link #getConnection}, but throwing the original ResourceException.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link CciLocalTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * <p>Directly accessed by {@link TransactionAwareConnectionFactoryProxy}.
 * @param cf the ConnectionFactory to obtain Connection from
 * @return a CCI Connection from the given ConnectionFactory
 * @throws ResourceException if thrown by CCI API methods
 * @see #doReleaseConnection
 */
public static Connection doGetConnection(ConnectionFactory cf) throws ResourceException {
	Assert.notNull(cf, "No ConnectionFactory specified");

	ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(cf);
	if (conHolder != null) {
		return conHolder.getConnection();
	}

	logger.debug("Opening CCI Connection");
	Connection con = cf.getConnection();

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering transaction synchronization for CCI Connection");
		conHolder = new ConnectionHolder(con);
		conHolder.setSynchronizedWithTransaction(true);
		TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(conHolder, cf));
		TransactionSynchronizationManager.bindResource(cf, conHolder);
	}

	return con;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:34,代碼來源:ConnectionFactoryUtils.java

示例6: getTxMap

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Returns a transactionally scoped Map that is used to provide repeatable lock store queries
 * for a given NodeRef. If no transaction is present, then null is returned.
 * 
 * @return Transactional Map or null if not available.
 */
protected Map<NodeRef, LockState> getTxMap()
{
    if (!TransactionSynchronizationManager.isSynchronizationActive())
    {
        return null;
    }
    Map<NodeRef, LockState> map = TransactionalResourceHelper.getMap(getClass().getName()+".repeatableReadMap");
    return map;
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:16,代碼來源:AbstractLockStore.java

示例7: getTransactionId

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Get a unique identifier associated with each transaction of each thread.  Null is returned if
 * no transaction is currently active.
 * 
 * @return Returns the transaction ID, or null if no transaction is present
 */
public static String getTransactionId()
{
    /*
     * Go direct to the synchronizations as we don't want to register a resource if one doesn't exist.
     * This method is heavily used, so the simple Map lookup on the ThreadLocal is the fastest.
     */
    
    TransactionSynchronizationImpl txnSynch =
            (TransactionSynchronizationImpl) TransactionSynchronizationManager.getResource(RESOURCE_KEY_TXN_SYNCH);
    if (txnSynch == null)
    {
        if (TransactionSynchronizationManager.isSynchronizationActive())
        {
            // need to lazily register synchronizations
            return registerSynchronizations().getTransactionId();
        }
        else
        {
            return null;   // not in a transaction
        }
    }
    else
    {
        return txnSynch.getTransactionId();
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:33,代碼來源:TransactionSupportUtil.java

示例8: registerSynchronizations

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Binds the Alfresco-specific to the transaction resources
 * 
 * @return Returns the current or new synchronization implementation
 */
private static TransactionSynchronizationImpl registerSynchronizations()
{
    /*
     * No thread synchronization or locking required as the resources are all threadlocal
     */
    if (!TransactionSynchronizationManager.isSynchronizationActive())
    {
        Thread currentThread = Thread.currentThread();
        throw new AlfrescoRuntimeException("Transaction must be active and synchronization is required: " + currentThread);
    }
    TransactionSynchronizationImpl txnSynch =
        (TransactionSynchronizationImpl) TransactionSynchronizationManager.getResource(RESOURCE_KEY_TXN_SYNCH);
    if (txnSynch != null)
    {
        // synchronization already registered
        return txnSynch;
    }
    // we need a unique ID for the transaction
    String txnId = GUID.generate();
    // register the synchronization
    txnSynch = new TransactionSynchronizationImpl(txnId);
    TransactionSynchronizationManager.registerSynchronization(txnSynch);
    // register the resource that will ensure we don't duplication the synchronization
    TransactionSynchronizationManager.bindResource(RESOURCE_KEY_TXN_SYNCH, txnSynch);
    // done
    if (logger.isDebugEnabled())
    {
        logger.debug("Bound txn synch: " + txnSynch);
    }
    return txnSynch;
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:37,代碼來源:TransactionSupportUtil.java

示例9: intercept

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
    boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
    if (!synchronizationActive) {
        Object[] objects = invocation.getArgs();
        MappedStatement ms = (MappedStatement) objects[0];
        DynamicDataSourceType dynamicDataSourceType;
        if ((dynamicDataSourceType = cacheMap.get(ms.getId())) == null) {
            //讀方法
            if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
                //!selectKey 為自增id查詢主鍵(SELECT LAST_INSERT_ID() )方法,使用主庫
                if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
                    dynamicDataSourceType = DynamicDataSourceType.WRITE;
                } else {
                    BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
                    String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
                    if (sql.matches(REGEX)) {
                        dynamicDataSourceType = DynamicDataSourceType.WRITE;
                    } else {
                        dynamicDataSourceType = DynamicDataSourceType.READ;
                    }
                }
            } else {
                dynamicDataSourceType = DynamicDataSourceType.WRITE;
            }
            log.warn("設置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(),
                    dynamicDataSourceType.name(), ms.getSqlCommandType().name());
            cacheMap.put(ms.getId(), dynamicDataSourceType);
        }
        DynamicDataSourceHolder.putDataSource(dynamicDataSourceType);
    }

    return invocation.proceed();
}
 
開發者ID:geeker-lait,項目名稱:tasfe-framework,代碼行數:35,代碼來源:DynamicDataSourcePlugin.java

示例10: currentSession

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager
            .getResource(this.sessionFactory);

    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()) {
            TransactionSynchronizationManager
                    .registerSynchronization(new SpringSessionSynchronization(
                            sessionHolder, this.sessionFactory));
            sessionHolder.setSynchronizedWithTransaction(true);

            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = session.getFlushMode();

            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager
                            .isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }

        return session;
    } else {
        throw new HibernateException("No Session found for current thread");
    }
}
 
開發者ID:zhaojunfei,項目名稱:lemon,代碼行數:35,代碼來源:SpringSessionContext.java

示例11: doGetConnection

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Actually obtain a JDBC Connection from the given DataSource.
 * Same as {@link #getConnection}, but throwing the original SQLException.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link DataSourceTransactionManager}. Will bind a Connection to the thread
 * if transaction synchronization is active (e.g. if in a JTA transaction).
 * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}.
 * @param dataSource the DataSource to obtain Connections from
 * @return a JDBC Connection from the given DataSource
 * @throws SQLException if thrown by JDBC methods
 * @see #doReleaseConnection
 */
public static Connection doGetConnection(DataSource dataSource) throws SQLException {
	Assert.notNull(dataSource, "No DataSource specified");

	ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
	if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {
		conHolder.requested();
		if (!conHolder.hasConnection()) {
			logger.debug("Fetching resumed JDBC Connection from DataSource");
			conHolder.setConnection(dataSource.getConnection());
		}
		return conHolder.getConnection();
	}
	// Else we either got no holder or an empty thread-bound holder here.

	logger.debug("Fetching JDBC Connection from DataSource");
	Connection con = dataSource.getConnection();

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering transaction synchronization for JDBC Connection");
		// Use same Connection for further JDBC actions within the transaction.
		// Thread-bound object will get removed by synchronization at transaction completion.
		ConnectionHolder holderToUse = conHolder;
		if (holderToUse == null) {
			holderToUse = new ConnectionHolder(con);
		}
		else {
			holderToUse.setConnection(con);
		}
		holderToUse.requested();
		TransactionSynchronizationManager.registerSynchronization(
				new ConnectionSynchronization(holderToUse, dataSource));
		holderToUse.setSynchronizedWithTransaction(true);
		if (holderToUse != conHolder) {
			TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
		}
	}

	return con;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:52,代碼來源:DataSourceUtils.java

示例12: registerTransactionSynchronization

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:LobCreatorUtils.java

示例13: put

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void put(final Object key, final Object value) {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
			@Override
			public void afterCommit() {
				targetCache.put(key, value);
			}
		});
	}
	else {
		this.targetCache.put(key, value);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:15,代碼來源:TransactionAwareCacheDecorator.java

示例14: evict

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void evict(final Object key) {
	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
			@Override
			public void afterCommit() {
				targetCache.evict(key);
			}
		});
	}
	else {
		this.targetCache.evict(key);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:15,代碼來源:TransactionAwareCacheDecorator.java

示例15: doJoinTransaction

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Join an existing transaction, if not already joined.
 * @param enforce whether to enforce the transaction
 * (i.e. whether failure to join is considered fatal)
 */
private void doJoinTransaction(boolean enforce) {
	if (this.jta) {
		// Let's try whether we're in a JTA transaction.
		try {
			this.target.joinTransaction();
			logger.debug("Joined JTA transaction");
		}
		catch (TransactionRequiredException ex) {
			if (!enforce) {
				logger.debug("No JTA transaction to join: " + ex);
			}
			else {
				throw ex;
			}
		}
	}
	else {
		if (TransactionSynchronizationManager.isSynchronizationActive()) {
			if (!TransactionSynchronizationManager.hasResource(this.target) &&
					!this.target.getTransaction().isActive()) {
				enlistInCurrentTransaction();
			}
			logger.debug("Joined local transaction");
		}
		else {
			if (!enforce) {
				logger.debug("No local transaction to join");
			}
			else {
				throw new TransactionRequiredException("No local transaction to join");
			}
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:40,代碼來源:ExtendedEntityManagerCreator.java


注:本文中的org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。