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


Java TransactionSynchronizationManager.getResource方法代碼示例

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


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

示例1: 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

示例2: doGetTransaction

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected Object doGetTransaction() {
	JdoTransactionObject txObject = new JdoTransactionObject();
	txObject.setSavepointAllowed(isNestedTransactionAllowed());

	PersistenceManagerHolder pmHolder = (PersistenceManagerHolder)
			TransactionSynchronizationManager.getResource(getPersistenceManagerFactory());
	if (pmHolder != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Found thread-bound PersistenceManager [" +
					pmHolder.getPersistenceManager() + "] for JDO transaction");
		}
		txObject.setPersistenceManagerHolder(pmHolder, false);
	}

	if (getDataSource() != null) {
		ConnectionHolder conHolder = (ConnectionHolder)
				TransactionSynchronizationManager.getResource(getDataSource());
		txObject.setConnectionHolder(conHolder);
	}

	return txObject;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:24,代碼來源:JdoTransactionManager.java

示例3: doReleaseConnection

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Actually close the given Connection, obtained from the given DataSource.
 * Same as {@link #releaseConnection}, but throwing the original SQLException.
 * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}.
 * @param con the Connection to close if necessary
 * (if this is {@code null}, the call will be ignored)
 * @param dataSource the DataSource that the Connection was obtained from
 * (may be {@code null})
 * @throws SQLException if thrown by JDBC methods
 * @see #doGetConnection
 */
public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException {
	if (con == null) {
		return;
	}
	if (dataSource != null) {
		ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
		if (conHolder != null && connectionEquals(conHolder, con)) {
			// It's the transactional Connection: Don't close it.
			conHolder.released();
			return;
		}
	}
	logger.debug("Returning JDBC Connection to DataSource");
	doCloseConnection(con, dataSource);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:27,代碼來源:DataSourceUtils.java

示例4: getLogProcessContext

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * get log context
 * @return
 */
public LogProcessContext getLogProcessContext() {
	LogProcessContext logProcessContext = (LogProcessContext) TransactionSynchronizationManager.getResource(LOG_PROCESS_CONTEXT);
	if(logProcessContext == null){
		throw new RuntimeException("please call TransController.startSoftTrans() before executeMethods!");
	}
	return logProcessContext;
}
 
開發者ID:QNJR-GROUP,項目名稱:EasyTransaction,代碼行數:12,代碼來源:EasyTransSynchronizer.java

示例5: 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

示例6: applyTransactionTimeout

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Apply the current transaction timeout, if any, to the given JDO Query object.
 * @param query the JDO Query object
 * @param pmf JDO PersistenceManagerFactory that the Query was created for
 * @throws JDOException if thrown by JDO methods
 */
public static void applyTransactionTimeout(Query query, PersistenceManagerFactory pmf) throws JDOException {
	Assert.notNull(query, "No Query object specified");
	PersistenceManagerHolder pmHolder =
			(PersistenceManagerHolder) TransactionSynchronizationManager.getResource(pmf);
	if (pmHolder != null && pmHolder.hasTimeout() &&
			pmf.supportedOptions().contains("javax.jdo.option.DatastoreTimeout")) {
		int timeout = (int) pmHolder.getTimeToLiveInMillis();
		query.setDatastoreReadTimeoutMillis(timeout);
		query.setDatastoreWriteTimeoutMillis(timeout);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:18,代碼來源:PersistenceManagerFactoryUtils.java

示例7: doCleanupAfterCompletion

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected void doCleanupAfterCompletion(Object transaction) {
    // Remove the connection holder from the thread, if exposed.
    if (checkNewConnectionHolder(transaction)&&null!= TransactionSynchronizationManager.getResource(getDataSource())) {
        TransactionSynchronizationManager.unbindResource(getDataSource());
    }
}
 
開發者ID:zhangkewei,項目名稱:dubbo-transaction,代碼行數:8,代碼來源:DubboTransactionDataSourceTransactonManager.java

示例8: isSessionTransactional

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Return whether the given Hibernate Session is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * @param session the Hibernate Session to check
 * @param sessionFactory Hibernate SessionFactory that the Session was created with
 * (may be {@code null})
 * @return whether the Session is transactional
 */
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory) {
	if (sessionFactory == null) {
		return false;
	}
	SessionHolder sessionHolder =
			(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
	return (sessionHolder != null && sessionHolder.containsSession(session));
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:17,代碼來源:SessionFactoryUtils.java

示例9: doGetTransaction

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected Object doGetTransaction() {
	HibernateTransactionObject txObject = new HibernateTransactionObject();
	txObject.setSavepointAllowed(isNestedTransactionAllowed());

	SessionHolder sessionHolder =
			(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
	if (sessionHolder != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Found thread-bound Session [" +
					SessionFactoryUtils.toString(sessionHolder.getSession()) + "] for Hibernate transaction");
		}
		txObject.setSessionHolder(sessionHolder);
	}
	else if (this.hibernateManagedSession) {
		try {
			Session session = getSessionFactory().getCurrentSession();
			if (logger.isDebugEnabled()) {
				logger.debug("Found Hibernate-managed Session [" +
						SessionFactoryUtils.toString(session) + "] for Spring-managed transaction");
			}
			txObject.setExistingSession(session);
		}
		catch (HibernateException ex) {
			throw new DataAccessResourceFailureException(
					"Could not obtain Hibernate-managed Session for Spring-managed transaction", ex);
		}
	}

	if (getDataSource() != null) {
		ConnectionHolder conHolder = (ConnectionHolder)
				TransactionSynchronizationManager.getResource(getDataSource());
		txObject.setConnectionHolder(conHolder);
	}

	return txObject;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:38,代碼來源:HibernateTransactionManager.java

示例10: getConnection

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
public Connection getConnection() {
    ConnectionHolder conHolder =
        (ConnectionHolder) TransactionSynchronizationManager.getResource(
        dataSource);

    if (conHolder == null) {
        throw new IllegalStateException("It seems not to be existing a transaction.");
    }

    return conHolder.getConnection();
}
 
開發者ID:mirage-sql,項目名稱:mirage-integration,代碼行數:12,代碼來源:SpringConnectionProvider.java

示例11: closeEntityManager

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * close the entity manager.
 * Use it with caution! This is only intended for use with async request, which Spring won't
 * close the entity manager until the async request is finished.
 */
public void closeEntityManager() {
  EntityManagerHolder emHolder = (EntityManagerHolder)
      TransactionSynchronizationManager.getResource(getEntityManagerFactory());
  if (emHolder == null) {
    return;
  }
  logger.debug("Closing JPA EntityManager in EntityManagerUtil");
  EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
 
開發者ID:dewey-its,項目名稱:apollo-custom,代碼行數:15,代碼來源:EntityManagerUtil.java

示例12: applyTransactionTimeout

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Apply the current transaction timeout, if any, to the given
 * Hibernate Query object.
 * @param query the Hibernate Query object
 * @param sessionFactory Hibernate SessionFactory that the Query was created for
 * (may be {@code null})
 * @see org.hibernate.Query#setTimeout
 */
public static void applyTransactionTimeout(Query query, SessionFactory sessionFactory) {
	Assert.notNull(query, "No Query object specified");
	if (sessionFactory != null) {
		SessionHolder sessionHolder =
				(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
		if (sessionHolder != null && sessionHolder.hasTimeout()) {
			query.setTimeout(sessionHolder.getTimeToLiveInSeconds());
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:19,代碼來源:SessionFactoryUtils.java

示例13: applyTransactionTimeout

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Apply the current transaction timeout, if any, to the given JPA Query object.
 * <p>This method sets the JPA 2.0 query hint "javax.persistence.query.timeout" accordingly.
 * @param query the JPA Query object
 * @param emf JPA EntityManagerFactory that the Query was created for
 */
public static void applyTransactionTimeout(Query query, EntityManagerFactory emf) {
	EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
	if (emHolder != null && emHolder.hasTimeout()) {
		int timeoutValue = (int) emHolder.getTimeToLiveInMillis();
		try {
			query.setHint("javax.persistence.query.timeout", timeoutValue);
		}
		catch (IllegalArgumentException ex) {
			// oh well, at least we tried...
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:19,代碼來源:EntityManagerFactoryUtils.java

示例14: getNewSession

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Get a new Hibernate Session from the given SessionFactory.
 * Will return a new Session even if there already is a pre-bound
 * Session for the given SessionFactory.
 * <p>Within a transaction, this method will create a new Session
 * that shares the transaction's JDBC Connection. More specifically,
 * it will use the same JDBC Connection as the pre-bound Hibernate Session.
 * @param sessionFactory Hibernate SessionFactory to create the session with
 * @param entityInterceptor Hibernate entity interceptor, or {@code null} if none
 * @return the new Session
 */
@SuppressWarnings("deprecation")
public static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor) {
	Assert.notNull(sessionFactory, "No SessionFactory specified");

	try {
		SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
		if (sessionHolder != null && !sessionHolder.isEmpty()) {
			if (entityInterceptor != null) {
				return sessionFactory.openSession(sessionHolder.getAnySession().connection(), entityInterceptor);
			}
			else {
				return sessionFactory.openSession(sessionHolder.getAnySession().connection());
			}
		}
		else {
			if (entityInterceptor != null) {
				return sessionFactory.openSession(entityInterceptor);
			}
			else {
				return sessionFactory.openSession();
			}
		}
	}
	catch (HibernateException ex) {
		throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:SessionFactoryUtils.java

示例15: invoke

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	// Invocation on EntityManager interface coming in...

	if (method.getName().equals("equals")) {
		// Only consider equal when proxies are identical.
		return (proxy == args[0]);
	}
	else if (method.getName().equals("hashCode")) {
		// Use hashCode of EntityManager proxy.
		return hashCode();
	}
	else if (method.getName().equals("getTargetEntityManager")) {
		// Handle EntityManagerProxy interface.
		return this.target;
	}
	else if (method.getName().equals("unwrap")) {
		// Handle JPA 2.0 unwrap method - could be a proxy match.
		Class<?> targetClass = (Class<?>) args[0];
		if (targetClass == null || targetClass.isInstance(proxy)) {
			return proxy;
		}
	}
	else if (method.getName().equals("isOpen")) {
		if (this.containerManaged) {
			return true;
		}
	}
	else if (method.getName().equals("close")) {
		if (this.containerManaged) {
			throw new IllegalStateException("Invalid usage: Cannot close a container-managed EntityManager");
		}
		ExtendedEntityManagerSynchronization synch = (ExtendedEntityManagerSynchronization)
				TransactionSynchronizationManager.getResource(this.target);
		if (synch != null) {
			// Local transaction joined - don't actually call close() before transaction completion
			synch.closeOnCompletion = true;
			return null;
		}
	}
	else if (method.getName().equals("getTransaction")) {
		if (this.synchronizedWithTransaction) {
			throw new IllegalStateException(
					"Cannot obtain local EntityTransaction from a transaction-synchronized EntityManager");
		}
	}
	else if (method.getName().equals("joinTransaction")) {
		doJoinTransaction(true);
		return null;
	}
	else if (method.getName().equals("isJoinedToTransaction")) {
		// Handle JPA 2.1 isJoinedToTransaction method for the non-JTA case.
		if (!this.jta) {
			return TransactionSynchronizationManager.hasResource(this.target);
		}
	}

	// Do automatic joining if required. Excludes toString, equals, hashCode calls.
	if (this.synchronizedWithTransaction && method.getDeclaringClass().isInterface()) {
		doJoinTransaction(false);
	}

	// Invoke method on current EntityManager.
	try {
		return method.invoke(this.target, args);
	}
	catch (InvocationTargetException ex) {
		throw ex.getTargetException();
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:71,代碼來源:ExtendedEntityManagerCreator.java


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