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:ajia.jpa.enforcement.JpaJdbcPolicyEnforcement.java

License:Apache License

@Before("jdbcExecution()")
public void checkFlushedSession() {
    // ... unchanged since listing 11.16
    Session currentSession = (Session) em.getDelegate();
    if (currentSession != null && currentSession.isOpen() && currentSession.isDirty()) {
        logger.error("Dirty session detected before " + "a JDBC call, use EntityManager.flush()",
                new Throwable());
    }//from   www. j a va 2s .  c o m
}

From source file:at.ac.tuwien.ifs.tita.dao.PersistenceContextProvider.java

License:Apache License

/** {@inheritDoc} */
public boolean isDataChanged() {
    Session s = getSession();
    if (s.isOpen()) {
        return s.isDirty();
    }//  w w  w .j a va2 s  . c o  m
    return false;
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#flush()}.
 *//*from  w w w  . ja  v a 2 s.  c o  m*/
public void testFlush() {
    Config conf = this.dao.persist(new Config("flush_conf_key", "val"));
    assertNotNull(conf);

    /* Make dirty. */
    conf.setValue("new_val");

    Session ses = this.dao.getSession();
    assertNotNull(ses);
    assertTrue(ses.isDirty());

    /* Close which should flush. */
    this.dao.flush();
    assertFalse(ses.isDirty());

    ses = DataAccessActivator.getNewSession();
    Config c = (Config) ses.load(Config.class, conf.getId());
    assertEquals("new_val", c.getValue());

    ses.getTransaction().begin();
    ses.delete(c);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#closeSession()}.
 *//*w  w  w  .  jav a  2  s. co m*/
public void testCloseSession() {
    Config conf = this.dao.persist(new Config("close_conf_key", "val"));
    assertNotNull(conf);

    /* Make dirty. */
    conf.setValue("new_val");

    Session ses = this.dao.getSession();
    assertNotNull(ses);
    assertTrue(ses.isOpen());
    assertTrue(ses.isDirty());

    /* Close which should flush. */
    this.dao.closeSession();
    assertFalse(ses.isOpen());

    ses = DataAccessActivator.getNewSession();
    Config c = (Config) ses.load(Config.class, conf.getId());
    assertEquals("new_val", c.getValue());

    ses.beginTransaction();
    ses.delete(c);
    ses.getTransaction().commit();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * //from   w  w  w  .j a va  2s .c  o  m
 */
public static void flush() {
    try {
        Session ssn = getSession();
        if (ssn.isDirty()) {
            beginTransaction();
            ssn.flush();
            commit();
        }
    } catch (HibernateException e) {
        rollback();
        throw e;
    }
}

From source file:com.nec.crud.hibernate.HibernateSessionManager.java

License:Open Source License

/**
 * Gets the active session for this request, creating it as necessary. When
 * the session is first created, a transaction is started.
 * /* ww w .  jav a2  s  . c o m*/
 * @return the request's session
 */
public static Session getSession() {
    // ??????
    Session session = getSessionFactory().openSession();

    // No Hibernate Session bound to thread
    Assert.notNull(session, "No Hibernate Session bound to thread");

    // We're having some difficult to diagnose connection issues at the moment.
    // To experiment lets test the connection when opened, if it fails do a rollback
    if (!isConnected(session) || session.isDirty()) {
        // ?
        closeSession(session);

        // ??????
        session = getSession();
    }
    return session;
}

From source file:com.nec.crud.hibernate4.HibernateSessionManager.java

License:Open Source License

/**
 * ??????//  w  w w  .j a  va2s  .  co  m
 * 
 * @return Session
 * @throws SQLException
 */
public static Session getSession() {
    // Gets the active session for this request, creating it as necessary. When
    // the session is first created, a transaction is started.
    Session session = SESSIONS.get();
    if (session == null) {
        // ??????
        session = getSessionFactory().openSession();
        SESSIONS.set(session);
    } else {
        if (!isConnected(session) || session.isDirty()) {
            // ?
            disconnectAll();

            // ??????
            session = getSession();
        }
    }

    return session;
}

From source file:cyrille.hibernate.context.DisconnectedSessionOpenInviewFilter.java

License:Apache License

public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpSession httpSession = request.getSession(false);
    if (httpSession == null) {
        logger.debug("No http session found");
    } else {//w ww  . jav a 2 s  .  c  om
        Session session = (Session) httpSession.getAttribute(Session.class.getName());
        if (session == null) {
            logger.debug("No Hibernate session found in http session");
        } else {
            // no need to reconnect the session, automatically done by Hibernate

            if (logger.isDebugEnabled()) {
                logger.debug("Bind " + session + " to ThreadLocalSessionContext");
            }
            ThreadLocalSessionContext.bind(session);
        }
    }

    try {
        // CHAIN
        chain.doFilter(request, response);

    } finally {
        // UNBIND HIBERNATE SESSION FROM HIBERNATE_THREAD_LOCAL_SESSION_CONTEXT, DISCONNECT AND BIND TO HTTP_SESSION

        Session session = ThreadLocalSessionContext.unbind(sessionFactory);
        if (session == null) {
            logger.debug("No Hibernate session unbound by ThreadLocalSessionContext");
        } else {
            if (session.isDirty()) {
                try {
                    logger.warn("Hibernate session is dirty !");
                    // TODO what is the policy ? Session will be disconnected and thus modifications will come back during reconnect
                    if (session.getTransaction().isActive()) {
                        logger.warn("Rollback active transaction for dirty session");
                        session.getTransaction().rollback();
                    }
                    logger.warn("Close dirty session");
                    session.close();
                } finally {
                    logger.warn("Remove Hibernate session from http session");
                    request.getSession().removeAttribute(Session.class.getName());

                }
            } else {
                logger.debug(
                        "Hibernate session unbound from ThreadLocalSessionContext, disconnected and bound to http session");

                try {
                    session.disconnect();
                    // force creation of the HttpSession even if it did not exist at the beginning of the filter
                    request.getSession().setAttribute(Session.class.getName(), session);
                } catch (RuntimeException e) {
                    logger.error(
                            "Exception disconnecting hibernate session. Remove Hibernate session from http session",
                            e);
                    request.getSession().removeAttribute(Session.class.getName());
                    throw e;
                }
            }

        }
    }
}

From source file:dk.netarkivet.wayback.indexer.HibernateUtilTest.java

License:Open Source License

/**
 * Tests that we can create an open session.
 *//*from   www .  j  ava  2  s  .  c o m*/
@Test
public void testGetSession() {
    Session session = HibernateUtil.getSession();
    assertTrue("Session should be connected.", session.isConnected());
    assertTrue("Session should be open.", session.isOpen());
    assertFalse("Session should not be dirty.", session.isDirty());
}

From source file:dk.netarkivet.wayback.indexer.HibernateUtilTester.java

License:Open Source License

/**
 * Tests that we can create an open session.
 *///from   w w  w.  j  a va  2s  . c  om
public void testGetSession() {
    Session session = HibernateUtil.getSession();
    assertTrue("Session should be connected.", session.isConnected());
    assertTrue("Session should be open.", session.isOpen());
    assertFalse("Session should not be dirty.", session.isDirty());
}