Example usage for org.hibernate Session isDirty

List of usage examples for org.hibernate Session isDirty

Introduction

In this page you can find the example usage for org.hibernate Session isDirty.

Prototype

boolean isDirty() throws HibernateException;

Source Link

Document

Does this session contain any changes which must be synchronized with the database?

Usage

From source file:ome.security.basic.EventHandler.java

License:Open Source License

/**
 * invocation interceptor for prepairing this {@link Thread} for execution
 * and subsequently reseting it.//w w w .  jav a  2 s .  c  o  m
 * 
 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
 */
public Object invoke(MethodInvocation arg0) throws Throwable {
    boolean readOnly = checkReadOnly(arg0);
    boolean stateful = StatefulServiceInterface.class.isAssignableFrom(arg0.getThis().getClass());
    boolean isClose = stateful && "close".equals(arg0.getMethod().getName());

    if (!readOnly && this.readOnly) {
        throw new ApiUsageException("This instance is read-only");
    }

    // ticket:1254
    // and ticket:1266
    final Session session = factory.getSession();

    if (!readOnly) {
        sql.deferConstraints();
    }
    if (!doLogin(readOnly, isClose)) {
        return null;
    }

    boolean failure = false;
    Object retVal = null;
    try {
        secSys.enableReadFilter(session);
        retVal = arg0.proceed();
        saveLogs(readOnly, session);
        secSys.cd.loadPermissions(session);
        return retVal;
    } catch (Throwable ex) {
        failure = true;
        throw ex;
    } finally {
        try {

            // on failure, we want to make sure that no one attempts
            // any further changes.
            if (failure) {
                // TODO we should probably do some forced clean up here.
            }

            // stateful services should NOT be flushed, because that's part
            // of the state that should hang around.
            else if (stateful) {
                // we don't want to do anything, really.
            }

            // read-only sessions should not have anything changed.
            else if (readOnly) {
                if (session.isDirty()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Clearing dirty session.");
                    }
                    session.clear();
                }
            }

            // stateless services, don't keep their sesssions about.
            else {
                session.flush();
                if (session.isDirty()) {
                    throw new InternalException("Session is dirty. Cannot properly "
                            + "reset security system. Must rollback.\n Session=" + session);
                }
                secSys.disableReadFilter(session);
                session.clear();
            }

        } finally {
            secSys.disableReadFilter(session);
            secSys.invalidateEventContext();
        }
    }

}

From source file:org.apache.ignite.cache.store.hibernate.CacheHibernateStoreSessionListener.java

License:Apache License

/** {@inheritDoc} */
@Override/*from   www .  j a  v  a2  s  .co m*/
public void onSessionEnd(CacheStoreSession ses, boolean commit) {
    Session hibSes = ses.attach(null);

    if (hibSes != null) {
        try {
            Transaction tx = hibSes.getTransaction();

            if (commit) {
                if (hibSes.isDirty())
                    hibSes.flush();

                if (tx.getStatus() == TransactionStatus.ACTIVE)
                    tx.commit();
            } else if (tx.getStatus().canRollback())
                tx.rollback();
        } catch (HibernateException e) {
            throw new CacheWriterException("Failed to end store session [tx=" + ses.transaction() + ']', e);
        } finally {
            hibSes.close();
        }
    }
}

From source file:org.conventionsframework.service.impl.BaseServiceImpl.java

License:Apache License

public void flushSession() {
    if (isConventionsFlushSession()) {
        Session session = crud().getSession();
        if (session.isOpen() && session.isDirty()) {
            session.flush();//from  w  w w. ja  v  a  2 s.c  o  m
        }
    }
}

From source file:org.gbif.portal.service.AbstractServiceTest.java

License:Open Source License

/**
 * One time teardown which closes the session opened for the tests.
 *///from  w w w.j a  v  a2s . c  o  m
protected void onTearDown() throws Exception {
    //logger.trace("onTearDown(): entered method.");
    super.onTearDown();

    SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    Session session = holder.getSession();
    if (session.isOpen()) {
        if (session.isDirty()) {
            session.flush();
        }
        session.close();
    }
    if (session.isConnected()) {
        session.connection().close();
    }
    holder.removeSession(session);
    TransactionSynchronizationManager.unbindResource(sessionFactory);
}

From source file:org.shept.org.springframework.orm.hibernate3.HibernateTemplateExtended.java

License:Apache License

public boolean isDirty() {
    Boolean executeWithNativeSession = executeWithNativeSession((new HibernateCallback<Boolean>() {
        public Boolean doInHibernate(final Session session) throws HibernateException {
            return session.isDirty();
        }//from  w  w  w  . ja  v a  2s . c  o  m
    }));
    return executeWithNativeSession.booleanValue();
}

From source file:org.xchain.namespaces.hibernate.DebugSessionCommand.java

License:Apache License

private void logSession(Session session) {
    log.debug("Connected, Dirty, Open:  {}",
            new boolean[] { session.isConnected(), session.isDirty(), session.isOpen() });
    log.debug("CacheMode:  {}", session.getCacheMode().toString());
    log.debug("EntityMode: {}", session.getEntityMode().toString());
    log.debug("FlushMode:  {}", session.getFlushMode().toString());
}