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


Java TransactionSynchronizationManager.hasResource方法代碼示例

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


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

示例1: flushResource

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected void flushResource(EntityManagerHolder resourceHolder) {
	EntityManager em = resourceHolder.getEntityManager();
	if (em instanceof EntityManagerProxy) {
		EntityManager target = ((EntityManagerProxy) em).getTargetEntityManager();
		if (TransactionSynchronizationManager.hasResource(target)) {
			// ExtendedEntityManagerSynchronization active after joinTransaction() call:
			// flush synchronization already registered.
			return;
		}
	}
	try {
		em.flush();
	}
	catch (RuntimeException ex) {
		if (this.jpaDialect != null) {
			throw this.jpaDialect.translateExceptionIfPossible(ex);
		}
		else {
			throw convertJpaAccessExceptionIfPossible(ex);
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:24,代碼來源:EntityManagerFactoryUtils.java

示例2: preHandle

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
		// Do not modify the PersistenceManager: just mark the request accordingly.
		String participateAttributeName = getParticipateAttributeName();
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
		PersistenceManager pm =
				PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
		TransactionSynchronizationManager.bindResource(
				getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:18,代碼來源:OpenPersistenceManagerInViewInterceptor.java

示例3: invoke

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	SessionFactory sf = getSessionFactory();
	if (!TransactionSynchronizationManager.hasResource(sf)) {
		// New Session to be bound for the current method's scope...
		Session session = openSession();
		try {
			TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
			return invocation.proceed();
		}
		finally {
			SessionFactoryUtils.closeSession(session);
			TransactionSynchronizationManager.unbindResource(sf);
		}
	}
	else {
		// Pre-bound Session found -> simply proceed.
		return invocation.proceed();
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:21,代碼來源:OpenSessionInterceptor.java

示例4: clearSynchronization

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Cleans out transaction resources if present
 */
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);
        }
    }
}
 
開發者ID:Alfresco,項目名稱:alfresco-core,代碼行數:16,代碼來源:TransactionSupportUtil.java

示例5: preHandle

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
public void preHandle(WebRequest request) throws DataAccessException {

	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// do not modify the EntityManager: just mark the request accordingly
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:35,代碼來源:OpenEntityManagerInViewInterceptor.java

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

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

示例8: doFilterInternal

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected void doFilterInternal(
		HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	PersistenceManagerFactory pmf = lookupPersistenceManagerFactory(request);
	boolean participate = false;

	if (TransactionSynchronizationManager.hasResource(pmf)) {
		// Do not modify the PersistenceManager: just set the participate flag.
		participate = true;
	}
	else {
		logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewFilter");
		PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
		TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
	}

	try {
		filterChain.doFilter(request, response);
	}

	finally {
		if (!participate) {
			PersistenceManagerHolder pmHolder = (PersistenceManagerHolder)
					TransactionSynchronizationManager.unbindResource(pmf);
			logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewFilter");
			PersistenceManagerFactoryUtils.releasePersistenceManager(pmHolder.getPersistenceManager(), pmf);
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:32,代碼來源:OpenPersistenceManagerInViewFilter.java

示例9: preHandle

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:34,代碼來源:OpenSessionInViewInterceptor.java

示例10: doResume

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
@Override
protected void doResume(Object transaction, Object suspendedResources) {
	SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources;
	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// From non-transactional code running in active transaction synchronization
		// -> can be safely removed, will be closed on transaction completion.
		TransactionSynchronizationManager.unbindResource(getSessionFactory());
	}
	TransactionSynchronizationManager.bindResource(getSessionFactory(), resourcesHolder.getSessionHolder());
	if (getDataSource() != null) {
		TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder());
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:14,代碼來源:HibernateTransactionManager.java

示例11: preHandle

import org.springframework.transaction.support.TransactionSynchronizationManager; //導入方法依賴的package包/類
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
		SessionFactoryUtils.isDeferredCloseActive(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			SessionHolder sessionHolder = new SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:46,代碼來源:OpenSessionInViewInterceptor.java


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