本文整理汇总了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();
}
}