Example usage for javax.persistence FlushModeType AUTO

List of usage examples for javax.persistence FlushModeType AUTO

Introduction

In this page you can find the example usage for javax.persistence FlushModeType AUTO.

Prototype

FlushModeType AUTO

To view the source code for javax.persistence FlushModeType AUTO.

Click Source Link

Document

(Default) Flushing to occur at query execution.

Usage

From source file:org.grails.datastore.mapping.query.Query.java

/**
 * Default behavior is the flush the session before a query in the case of FlushModeType.AUTO.
 * Subclasses can override this method to disable that.
 *///from  ww  w  .j  a v a 2 s.c o m
protected void flushBeforeQuery() {
    // flush before query execution in FlushModeType.AUTO
    if (session.getFlushMode() == FlushModeType.AUTO) {
        session.flush();
    }
}

From source file:com.impetus.kundera.persistence.PersistenceDelegator.java

public void commit() {
    if (!flushMode.equals(FlushModeType.AUTO)) {
        enableFlush = true;//from w w w.j ava  2s  .c  o  m
        flush();
        isTransactionInProgress = false;
        enableFlush = false;
        flushManager.commit();
        flushManager.clearFlushStack();
    }
}

From source file:org.apache.openjpa.persistence.EntityManagerImpl.java

/**
 * Translate our internal flush constant to a flush mode enum value.
 *///  www .  j a  v a 2  s . co m
static FlushModeType fromFlushBeforeQueries(int flush) {
    switch (flush) {
    case QueryFlushModes.FLUSH_TRUE:
        return FlushModeType.AUTO;
    case QueryFlushModes.FLUSH_FALSE:
        return FlushModeType.COMMIT;
    default:
        return null;
    }
}

From source file:org.apache.openjpa.persistence.EntityManagerImpl.java

/**
 * Translate a flush mode enum value to our internal flush constant.
 *///from   w  w  w .  ja  v a 2  s  .c  o  m
static int toFlushBeforeQueries(FlushModeType flushMode) {
    // choose default for null
    if (flushMode == null)
        return QueryFlushModes.FLUSH_WITH_CONNECTION;
    if (flushMode == FlushModeType.AUTO)
        return QueryFlushModes.FLUSH_TRUE;
    if (flushMode == FlushModeType.COMMIT)
        return QueryFlushModes.FLUSH_FALSE;
    throw new ArgumentException(flushMode.toString(), null, null, false);
}

From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java

/**
 * {@inheritDoc}/*from   w w w  .  j  a  v a2  s .  com*/
 * 
 */
@Override
public int executeUpdate() {
    // flush if specified
    if (!this.q.isInternal() && this.em.hasActiveTransaction()
            && ((this.flushMode == FlushModeType.AUTO) || (this.em.getFlushMode() == FlushModeType.AUTO))) {
        this.em.flush();
    }

    final Connection connection = this.em.getConnection();
    final Object[] parameters = this.applyParameters(connection);

    try {
        this.em.assertTransaction();

        return new QueryRunner(this.em.getJdbcAdaptor(), false).update(connection, this.sql, parameters);
    } catch (final SQLException e) {
        QueryImpl.LOG.error(e, "Query failed" + QueryImpl.LOG.lazyBoxed(this.sql, parameters));

        this.em.setRollbackOnly();

        throw new PersistenceException("Query failed", e);
    }
}

From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java

/**
 * {@inheritDoc}//from  www.j a va2s. co m
 * 
 */
@Override
public List<X> getResultList() {
    // flush if specified
    if (!this.q.isInternal() && this.em.hasActiveTransaction()
            && ((this.flushMode == FlushModeType.AUTO) || (this.em.getFlushMode() == FlushModeType.AUTO))) {
        this.em.flush();
    }

    ManagedInstance.LOCK_CONTEXT.set(this.getLockMode());
    try {
        return this.getResultListImpl();
    } finally {
        ManagedInstance.LOCK_CONTEXT.set(null);
    }
}

From source file:org.batoo.jpa.core.impl.manager.EntityManagerImpl.java

/**
 * @param entityManagerFactory//w ww  . ja  v a  2 s. co m
 *            the entity manager factory
 * @param metamodel
 *            the metamodel
 * @param datasource
 *            the datasource
 * @param properties
 *            properties for the entity manager
 * @param jdbcAdaptor
 *            the JDBC adaptor
 * 
 * @since 2.0.0
 */
public EntityManagerImpl(EntityManagerFactoryImpl entityManagerFactory, MetamodelImpl metamodel,
        DataSource datasource, Map<String, Object> properties, JdbcAdaptor jdbcAdaptor) {
    super();

    this.emf = entityManagerFactory;
    this.metamodel = metamodel;
    this.datasource = datasource;
    this.jdbcAdaptor = jdbcAdaptor;
    this.session = new SessionImpl(this, metamodel);
    this.criteriaBuilder = this.emf.getCriteriaBuilder();

    this.properties = properties;
    this.flushMode = FlushModeType.AUTO;

    this.open = true;
}

From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java

/**
 * {@inheritDoc}//ww  w.j  a va  2s.  com
 * 
 */
@Override
public int executeUpdate() {
    this.em.assertTransaction();

    // flush if specified
    if ((this.flushMode == FlushModeType.AUTO) || (this.em.getFlushMode() == FlushModeType.AUTO)) {
        this.em.flush();
    }

    try {
        if (!this.parameters.isEmpty()) {
            final Object[] parameters = new Object[this.parameters.size()];

            for (int i = 0; i < this.parameters.size(); i++) {
                parameters[i] = this.getParameterValue(i);
            }

            return new QueryRunner(this.em.getJdbcAdaptor(), false).update(this.query, parameters);
        }

        return new QueryRunner(this.em.getJdbcAdaptor(), false).update(this.query);

    } catch (final SQLException e) {
        throw new PersistenceException("Native query execution has failed!", e);
    }
}

From source file:org.grails.datastore.gorm.support.AbstractDatastorePersistenceContextInterceptor.java

public void init() {

    final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(datastore);//w w w. ja  v  a2  s .c om
    if (sessionHolder == null) {
        LOG.debug("Opening single Datastore session in DatastorePersistenceContextInterceptor");
        Session session = getSession();
        session.setFlushMode(FlushModeType.AUTO);
        try {
            DatastoreUtils.bindSession(session, this);
        } catch (IllegalStateException e) {
            // ignore, already bound
        }
    }
}

From source file:org.springframework.datastore.mapping.web.support.OpenSessionInViewInterceptor.java

public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
    // Only potentially flush in single session mode.
    if (hasSessionBound()) {

        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                .getResource(getDatastore());
        LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor");
        final Session session = sessionHolder.getSession();

        if (session.getFlushMode() == FlushModeType.AUTO) {
            session.flush();// w w  w.j  a  v a  2 s.  c o  m
        }
    }
}