Example usage for org.hibernate FlushMode AUTO

List of usage examples for org.hibernate FlushMode AUTO

Introduction

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

Prototype

FlushMode AUTO

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

Click Source Link

Document

The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

Usage

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

License:Apache License

@SuppressWarnings("rawtypes")
private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    given(tm.getTransaction()).willReturn(transaction);
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);
    if (flushNever) {
        given(session.getFlushMode()).willReturn(FlushMode.MANUAL, FlushMode.AUTO, FlushMode.MANUAL);
    } else {// ww  w . java2 s .c  o m
        given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    }

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    try {
        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        HibernateTemplate ht = new HibernateTemplate(sf);
        ht.setExposeNativeSession(true);
        for (int i = 0; i < 5; i++) {
            ht.executeFind(new HibernateCallback() {

                @Override
                public Object doInHibernate(org.hibernate.Session sess) {
                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
                    assertEquals(session, sess);
                    return null;
                }
            });
        }

        Synchronization synchronization = transaction.getSynchronization();
        assertTrue("JTA synchronization registered", synchronization != null);
        synchronization.beforeCompletion();
        synchronization.afterCompletion(Status.STATUS_COMMITTED);
        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
    } finally {
        TransactionSynchronizationManager.unbindResource(sf);
    }
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));

    InOrder ordered = inOrder(session);
    if (flushNever) {
        ordered.verify(session).setFlushMode(FlushMode.AUTO);
        ordered.verify(session).setFlushMode(FlushMode.MANUAL);
    } else {
        ordered.verify(session).flush();
    }
    ordered.verify(session).disconnect();
}

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

License:Apache License

@Test
@SuppressWarnings("rawtypes")
public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception {

    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction transaction = new MockJtaTransaction();
    final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
    final Session session = mock(Session.class);
    given(tm.getTransaction()).willReturn(transaction);
    given(sf.openSession()).willReturn(session);
    given(sf.getTransactionManager()).willReturn(tm);
    given(session.isOpen()).willReturn(true);
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);

    for (int j = 0; j < 2; j++) {

        if (j == 0) {
            assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
        } else {//from  w w w.j  av a  2s .  c  o  m
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        }

        HibernateTemplate ht = new HibernateTemplate(sf);
        ht.setExposeNativeSession(true);
        for (int i = 0; i < 5; i++) {
            ht.executeFind(new HibernateCallback() {

                @Override
                public Object doInHibernate(org.hibernate.Session sess) {
                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
                    assertEquals(session, sess);
                    return null;
                }
            });
        }

        final Synchronization synchronization = transaction.getSynchronization();
        assertTrue("JTA synchronization registered", synchronization != null);

        // Call synchronization in a new thread, to simulate a
        // synchronization
        // triggered by a new remote call from a remote transaction
        // coordinator.
        Thread synch = new Thread() {

            @Override
            public void run() {
                synchronization.beforeCompletion();
                synchronization.afterCompletion(Status.STATUS_COMMITTED);
            }
        };
        synch.start();
        synch.join();

        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
        assertTrue("Thread session holder empty", sessionHolder.isEmpty());
        assertTrue("JTA synchronizations not active",
                !TransactionSynchronizationManager.isSynchronizationActive());
    }

    verify(session, times(2)).flush();
    verify(session, times(2)).close();
    TransactionSynchronizationManager.unbindResource(sf);
}

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

License:Apache License

@Test
public void testExecuteWithThreadBoundAndFlushEager() throws HibernateException {
    given(session.isOpen()).willReturn(true);
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.setFlushModeName("FLUSH_EAGER");
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    try {//ww  w.  ja v a2 s.  co m
        final List l = new ArrayList();
        l.add("test");
        List result = hibernateTemplate.executeFind(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
                return l;
            }
        });
        assertTrue("Correct result list", result == l);
    } finally {
        TransactionSynchronizationManager.unbindResource(sessionFactory);
    }
    verify(session).flush();
}

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

License:Apache License

@Test
public void testSave() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    given(session.save(tb)).willReturn(0);
    assertEquals("Correct return value", hibernateTemplate.save(tb), 0);
    verify(session).flush();//  w  w w .  ja  v a 2  s .c  om
    verify(session).close();
}

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

License:Apache License

@Test
public void testSaveWithEntityName() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    given(session.save("myEntity", tb)).willReturn(0);
    assertEquals("Correct return value", hibernateTemplate.save("myEntity", tb), 0);
    verify(session).flush();/*from   w  w  w.j  a  v a2 s  . co  m*/
    verify(session).close();
}

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

License:Apache License

@Test
public void testUpdate() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.update(tb);/*from   w ww  .ja  v a2  s  .  co  m*/
    verify(session).update(tb);
    verify(session).flush();
    verify(session).close();
}

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

License:Apache License

@Test
public void testUpdateWithLockMode() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.update(tb, LockMode.UPGRADE);
    verify(session).update(tb);/*from   ww w .  j  av  a  2 s. co m*/
    verify(session).lock(tb, LockMode.UPGRADE);
    verify(session).flush();
    verify(session).close();
}

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

License:Apache License

@Test
public void testUpdateWithEntityName() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.update("myEntity", tb);
    verify(session).update("myEntity", tb);
    verify(session).flush();//from  w ww  .j  av a  2s .c  om
    verify(session).close();
}

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

License:Apache License

@Test
public void testUpdateWithEntityNameAndLockMode() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.update("myEntity", tb, LockMode.UPGRADE);
    verify(session).update("myEntity", tb);
    verify(session).lock(tb, LockMode.UPGRADE);
    verify(session).flush();//ww w.j  ava2s.c o m
    verify(session).close();
}

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

License:Apache License

@Test
public void testSaveOrUpdate() throws HibernateException {
    TestBean tb = new TestBean();
    given(session.getFlushMode()).willReturn(FlushMode.AUTO);
    hibernateTemplate.saveOrUpdate(tb);/*from   www .  j  av a2s.  c om*/
    verify(session).saveOrUpdate(tb);
    verify(session).flush();
    verify(session).close();
}