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


Java Session.getFlushMode方法代碼示例

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


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

示例1: prepareTransaction

import org.hibernate.Session; //導入方法依賴的package包/類
@Override
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
		throws PersistenceException {

	Session session = getSession(entityManager);
	FlushMode flushMode = session.getFlushMode();
	FlushMode previousFlushMode = null;
	if (readOnly) {
		// We should suppress flushing for a read-only transaction.
		session.setFlushMode(FlushMode.MANUAL);
		previousFlushMode = flushMode;
	}
	else {
		// We need AUTO or COMMIT for a non-read-only transaction.
		if (flushMode.lessThan(FlushMode.COMMIT)) {
			session.setFlushMode(FlushMode.AUTO);
			previousFlushMode = flushMode;
		}
	}
	return new SessionTransactionData(session, previousFlushMode);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:22,代碼來源:HibernateJpaDialect.java

示例2: currentSession

import org.hibernate.Session; //導入方法依賴的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

示例3: currentSession

import org.hibernate.Session; //導入方法依賴的package包/類
/**
    * Binds the configured session to Spring's transaction manager strategy if there's no session.
    *
    * @return Returns the configured session, or the one managed by Spring. Never returns null.
    */
   @Override
   public Session currentSession() {
try {
    Session s = defaultSessionContext.currentSession();
    return s;
} catch (HibernateException cause) {

    // There's no session bound to the current thread. Let's open one if
    // needed.
    if (ManagedSessionContext.hasBind(sessionFactory)) {
	return localSessionContext.currentSession();
    }

    Session session;
    session = sessionFactory.openSession();
    TransactionAwareSessionContext.logger.warn("No Session bound to current Thread. Opened new Session ["
	    + session + "]. Transaction: " + session.getTransaction());

    if (registerSynchronization(session)) {
	// Normalizes Session flush mode, defaulting it to AUTO. Required for
	// synchronization.
	FlushMode flushMode = session.getFlushMode();

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

    return session;
}
   }
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:TransactionAwareSessionContext.java

示例4: invoke

import org.hibernate.Session; //導入方法依賴的package包/類
public Object invoke(MethodInvocation mi) throws Throwable {
	if (persistManager.getSessionFactory() != null) {
		unitOfWork.begin();
		try {
			Session session = unitOfWork.getSession();
			if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
				return mi.proceed();
			} else {
				Transaction tx = session.beginTransaction();
				FlushMode previousMode = session.getFlushMode();
				session.setFlushMode(FlushMode.COMMIT);
				try {
					Object result = mi.proceed();
					tx.commit();
					return result;
				} catch (Throwable t) {
					try {
						tx.rollback();
					} catch (Throwable t2) {
					}
					throw t;
				} finally {
					session.setFlushMode(previousMode);
				}
			}
			
		} finally {
			unitOfWork.end();
		}
	} else {
		return mi.proceed();
	}
}
 
開發者ID:jmfgdev,項目名稱:gitplex-mit,代碼行數:34,代碼來源:TransactionInterceptor.java

示例5: getJtaSynchronizedSession

import org.hibernate.Session; //導入方法依賴的package包/類
/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization.
 * @param sessionHolder the SessionHolder to check
 * @param sessionFactory the SessionFactory to get the JTA TransactionManager from
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 * @return the associated Session, if any
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 */
private static Session getJtaSynchronizedSession(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		// Check whether JTA transaction management is active ->
		// fetch pre-bound Session for the current JTA transaction, if any.
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction)
		try {
			// Look for transaction-specific Session.
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					Session session = sessionHolder.getValidatedSession(jtaTx);
					if (session == null && !sessionHolder.isSynchronizedWithTransaction()) {
						// No transaction-specific Session found: If not already marked as
						// synchronized with transaction, register the default thread-bound
						// Session as JTA-transactional. If there is no default Session,
						// we're a new inner JTA transaction with an outer one being suspended:
						// In that case, we'll return null to trigger opening of a new Session.
						session = sessionHolder.getValidatedSession();
						if (session != null) {
							logger.debug("Registering JTA transaction synchronization for existing Hibernate Session");
							sessionHolder.addSession(jtaTx, session);
							jtaTx.registerSynchronization(
									new SpringJtaSynchronizationAdapter(
											new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false),
											jtaTm));
							sessionHolder.setSynchronizedWithTransaction(true);
							// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
							// with FlushMode.NEVER, which needs to allow flushing within the transaction.
							FlushMode flushMode = session.getFlushMode();
							if (flushMode.lessThan(FlushMode.COMMIT)) {
								session.setFlushMode(FlushMode.AUTO);
								sessionHolder.setPreviousFlushMode(flushMode);
							}
						}
					}
					return session;
				}
			}
			// No transaction active -> simply return default thread-bound Session, if any
			// (possibly from OpenSessionInViewFilter/Interceptor).
			return sessionHolder.getValidatedSession();
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException("Could not check JTA transaction", ex);
		}
	}
	else {
		// No JTA TransactionManager -> simply return default thread-bound Session, if any
		// (possibly from OpenSessionInViewFilter/Interceptor).
		return sessionHolder.getValidatedSession();
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:72,代碼來源:SessionFactoryUtils.java


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