本文整理匯總了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);
}
示例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");
}
}
示例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;
}
}
示例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();
}
}
示例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();
}
}