Example usage for org.hibernate FlushMode COMMIT

List of usage examples for org.hibernate FlushMode COMMIT

Introduction

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

Prototype

FlushMode COMMIT

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

Click Source Link

Document

The Session is flushed when Transaction#commit is called.

Usage

From source file:org.hl7.hibernate.Persistence.java

License:Open Source License

public Session getSession() {
    if (_session == null) {
        _session = openSession();/*from  ww  w.  jav a 2  s. c  om*/
        Transaction tx = _session.beginTransaction();
        Transaction tx2 = _session.getTransaction();
        if (tx != tx2)
            throw new Error("assertion failed about " + tx + " == " + tx2);
    }
    // only flush on commits
    _session.setFlushMode(FlushMode.COMMIT);
    return _session;
}

From source file:org.jasig.ssp.dao.PersonDao.java

License:Apache License

public Person fromUsername(@NotNull final String username) {
    if (!StringUtils.isNotBlank(username)) {
        throw new IllegalArgumentException("username can not be empty.");
    }/*from w  w  w  .  jav  a 2  s.c om*/

    Dialect dialect = ((SessionFactoryImplementor) sessionFactory).getDialect();
    Session currentSession = sessionFactory.getCurrentSession();
    final Criteria query = currentSession.createCriteria(Person.class);
    //Sqlserver does a case insensitive string compare so we don't do the string
    //normalization for sqlserver so that it hits index on person table idx_person_username
    if (dialect instanceof SQLServerDialect) {
        query.add(Restrictions.eq("username", username)).setFlushMode(FlushMode.COMMIT);
        return (Person) query.uniqueResult();

    } else {
        //Postgres has an index on lower(username): idx_func_username_person
        query.add(Restrictions.eq("username", StringUtils.lowerCase(username))).setFlushMode(FlushMode.COMMIT);
    }
    return (Person) query.uniqueResult();
}

From source file:org.jboss.dashboard.database.DataSourceManager.java

License:Apache License

/**
 * Get all registered datasource entries
 *///from  ww  w .j  a  va  2 s.  c  om
public List<DataSourceEntry> getDataSourceEntries() {
    final List result = new ArrayList();
    try {
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                Query query = session.createQuery(" from " + DataSourceEntry.class.getName());
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.COMMIT);
                query.setCacheable(true);
                result.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    return result;
}

From source file:org.jboss.dashboard.database.DataSourceManager.java

License:Apache License

public DataSourceEntry getDataSourceEntry(final String name) throws Exception {
    if (name == null)
        return null;

    final List results = new ArrayList();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("select dse ");
            sql.append("from ").append(DataSourceEntry.class.getName()).append(" as dse ");
            sql.append("where dse.name = :name");

            Query query = session.createQuery(sql.toString());
            query.setString("name", name);
            query.setCacheable(true);/*ww w .  ja va2  s  .  c o m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0) {
        if (results.size() > 1)
            log.error("There are " + results.size() + " data sources with name=" + name);
        return (DataSourceEntry) results.get(0);
    } else {
        log.debug("Does not exists data source with name: " + name);
        return null;
    }
}

From source file:org.jboss.dashboard.database.hibernate.HibernateTransaction.java

License:Apache License

protected final void executeFragment(HibernateTxFragment fragment) throws Exception {
    FlushMode flushMode = session.getFlushMode();
    boolean flushChanged = false;
    try {/*  ww w. j ava 2  s  . co  m*/
        // Change the current fragment.
        fragment.parentFragment = currentFragment;
        currentFragment = fragment;

        // Disable flush if the fragment's flush is set.
        HibernateTxFragment flusherFragment = getFlusherFragment();
        if (fragment == flusherFragment) {
            session.setFlushMode(FlushMode.COMMIT);
            flushChanged = true;
        }

        // Execute the fragment.
        fragment.txFragment(session);

        // Flush the fragment if required.
        if (fragment == flusherFragment) {
            log.debug("Flush transaction. Id=" + getId());
            session.flush();
        }
    } catch (Throwable t) {
        // Rollback the tx.
        error(t);

        // Propagate the exception.
        if (t instanceof Exception)
            throw (Exception) t;
        else
            throw new Exception(t);
    } finally {
        if (flushChanged)
            session.setFlushMode(flushMode);
        currentFragment = fragment.parentFragment;
        if (fragment.callbacksEnabled) {
            listeners.add(fragment);

            // When completing the tx notify the fragment right now.
            if (completing)
                notifyListener(true, fragment);
        }
    }
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

public Set<KPI> getAllKPIs() throws Exception {
    final Set<KPI> results = new HashSet<KPI>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);
            Query query = session.createQuery("from " + KPIImpl.class.getName() + " order by id");
            query.setCacheable(true);/*ww w .j  a  v  a  2s  .  c  om*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    return results;
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

public KPI getKPIById(final Long id) throws Exception {
    final List<KPI> results = new ArrayList<KPI>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(KPIImpl.class.getName()).append(" as instance ");
            sql.append("where instance.id = :id");

            Query query = session.createQuery(sql.toString());
            if (id != null)
                query.setLong("id", id.longValue());
            query.setCacheable(true);/*from  w  w  w .  j  a  v  a2  s . c o  m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (KPIImpl) results.get(0);
    else
        log.debug("KPI with id =" + id + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

public KPI getKPIByCode(final String code) throws Exception {
    final List<KPI> results = new ArrayList<KPI>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(KPIImpl.class.getName()).append(" as instance ");
            sql.append("where instance.code = :code");

            Query query = session.createQuery(sql.toString());
            if (code != null)
                query.setString("code", code);
            query.setCacheable(true);//w w w  . j  a  v  a2  s .  c  om
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (KPIImpl) results.get(0);
    else
        log.debug("KPI with code=" + code + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.provider.DataProviderManagerImpl.java

License:Apache License

public Set<DataProvider> getAllDataProviders() throws Exception {
    final Set<DataProvider> results = new HashSet<DataProvider>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);
            Query query = session.createQuery("from " + DataProviderImpl.class.getName() + " order by id");
            query.setCacheable(true);/*  w w w.j  a v a2s.c  o m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    return results;
}

From source file:org.jboss.dashboard.provider.DataProviderManagerImpl.java

License:Apache License

public DataProvider getDataProviderById(final Long id) throws Exception {
    final List<DataProvider> results = new ArrayList<DataProvider>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(DataProviderImpl.class.getName()).append(" as instance ");
            sql.append("where instance.id = :id");

            Query query = session.createQuery(sql.toString());
            if (id != null)
                query.setLong("id", id.longValue());
            query.setCacheable(true);//  w  w w. j ava 2s  .c  o  m
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (DataProviderImpl) results.get(0);
    else
        log.debug("Data provider with id =" + id + " does not exist.");
    return null;
}