Example usage for org.hibernate FlushMode ALWAYS

List of usage examples for org.hibernate FlushMode ALWAYS

Introduction

In this page you can find the example usage for org.hibernate FlushMode ALWAYS.

Prototype

FlushMode ALWAYS

To view the source code for org.hibernate FlushMode ALWAYS.

Click Source Link

Document

The Session is flushed before every query.

Usage

From source file:org.grails.orm.hibernate.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (getFlushMode()) {
    case GrailsHibernateTemplate.FLUSH_EAGER:
    case GrailsHibernateTemplate.FLUSH_AUTO:
        hibernateFlushMode = FlushMode.AUTO;
        break;/* ww w . j  a  va  2  s  .c  om*/
    case GrailsHibernateTemplate.FLUSH_NEVER:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case GrailsHibernateTemplate.FLUSH_COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case GrailsHibernateTemplate.FLUSH_ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (flushMode) {
    case AUTO://from   w w  w  .ja v a2 s . c  om
        hibernateFlushMode = FlushMode.AUTO;
        break;
    case MANUAL:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

public void setFlushMode(int flushMode) {
    if (AbstractHibernateDatastore.FlushMode.AUTO.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.AUTO;
    } else if (AbstractHibernateDatastore.FlushMode.MANUAL.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.MANUAL;
    } else if (AbstractHibernateDatastore.FlushMode.COMMIT.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.COMMIT;
    } else if (AbstractHibernateDatastore.FlushMode.ALWAYS.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.ALWAYS;
    }/*  w  w  w  .j  av  a 2  s  . co  m*/
}

From source file:org.gridgain.examples.cache.store.hibernate.GridCacheHibernatePersonStore.java

License:GNU General Public License

/** {@inheritDoc} */
@SuppressWarnings({ "JpaQueryApiInspection" })
@Override/*from   w w  w  .  j a v a  2 s .c  o m*/
public void remove(@Nullable String cacheName, @Nullable GridCacheTx tx, UUID key) throws GridException {
    X.println("Store remove [key=" + key + ", tx=" + tx + ']');

    Session ses = session(tx);

    try {
        ses.createQuery("delete " + Person.class.getSimpleName() + " where key = :key").setParameter("key", key)
                .setFlushMode(FlushMode.ALWAYS).executeUpdate();
    } catch (HibernateException e) {
        rollback(ses, tx);

        throw new GridException("Failed to remove value from cache store with key: " + key, e);
    } finally {
        end(ses, tx);
    }
}

From source file:org.gridgain.examples.datagrid.store.hibernate.CacheHibernatePersonStore.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings({ "JpaQueryApiInspection" })
@Override/*  w  w w  .ja  v a 2  s. c om*/
public void remove(@Nullable GridCacheTx tx, Long key) throws GridException {
    System.out.println(">>> Store remove [key=" + key + ", xid=" + (tx == null ? null : tx.xid()) + ']');

    Session ses = session(tx);

    try {
        ses.createQuery("delete " + Person.class.getSimpleName() + " where key = :key").setParameter("key", key)
                .setFlushMode(FlushMode.ALWAYS).executeUpdate();
    } catch (HibernateException e) {
        rollback(ses, tx);

        throw new GridException("Failed to remove value from cache store with key: " + key, e);
    } finally {
        end(ses, tx);
    }
}

From source file:org.gridgain.grid.cache.store.hibernate.GridCacheHibernateBlobStoreSelfTest.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   w w  w  .j  a v a  2  s.  c o m*/
protected void afterTest() throws Exception {
    super.afterTest();

    Session s = store.session(null);

    if (s == null)
        return;

    try {
        s.createQuery("delete from " + GridCacheHibernateBlobStoreEntry.class.getSimpleName())
                .setFlushMode(FlushMode.ALWAYS).executeUpdate();

        Transaction hTx = s.getTransaction();

        if (hTx != null && hTx.isActive())
            hTx.commit();
    } finally {
        s.close();
    }
}

From source file:org.springframework.orm.hibernate3.HibernateAccessor.java

License:Apache License

/**
 * Apply the flush mode that's been specified for this accessor
 * to the given Session.//  w  w w .jav  a  2s .co m
 * @param session the current Hibernate Session
 * @param existingTransaction if executing within an existing transaction
 * @return the previous flush mode to restore after the operation,
 * or {@code null} if none
 * @see #setFlushMode
 * @see org.hibernate.Session#setFlushMode
 */
protected FlushMode applyFlushMode(Session session, boolean existingTransaction) {
    if (getFlushMode() == FLUSH_NEVER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = session.getFlushMode();
            if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
                session.setFlushMode(FlushMode.MANUAL);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.MANUAL);
        }
    } else if (getFlushMode() == FLUSH_EAGER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = session.getFlushMode();
            if (!previousFlushMode.equals(FlushMode.AUTO)) {
                session.setFlushMode(FlushMode.AUTO);
                return previousFlushMode;
            }
        } else {
            // rely on default FlushMode.AUTO
        }
    } else if (getFlushMode() == FLUSH_COMMIT) {
        if (existingTransaction) {
            FlushMode previousFlushMode = session.getFlushMode();
            if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.COMMIT);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.COMMIT);
        }
    } else if (getFlushMode() == FLUSH_ALWAYS) {
        if (existingTransaction) {
            FlushMode previousFlushMode = session.getFlushMode();
            if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.ALWAYS);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.ALWAYS);
        }
    }
    return null;
}

From source file:org.springframework.orm.hibernate3.HibernateInterceptorTests.java

License:Apache License

@Test
public void testInterceptorWithThreadBoundAndFlushAlways() {
    given(session.isOpen()).willReturn(true);
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);

    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sessionFactory);
    interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS);
    try {//from w  w w .j a va 2 s  .  co m
        interceptor.invoke(invocation);
    } catch (Throwable t) {
        fail("Should not have thrown Throwable: " + t.getMessage());
    } finally {
        TransactionSynchronizationManager.unbindResource(sessionFactory);
    }

    InOrder ordered = inOrder(session);
    ordered.verify(session).setFlushMode(FlushMode.ALWAYS);
    ordered.verify(session).setFlushMode(FlushMode.AUTO);
    verify(session, never()).flush();
}

From source file:pwfms.PWFMPersistentManager.java

private PWFMPersistentManager() throws PersistentException {
    super(_connectionSetting, _sessionType, _timeToAlive, new String[] {}, _extraProperties,
            _configurationFile);/*  ww w .ja  v  a 2  s .com*/
    setFlushMode(FlushMode.ALWAYS);
}