当前位置: 首页>>代码示例>>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;未经允许,请勿转载。