List of usage examples for org.hibernate FlushMode AUTO
FlushMode AUTO
To view the source code for org.hibernate FlushMode AUTO.
Click Source Link
From source file:org.springframework.beans.BeanWrapperTests.java
License:Apache License
public void testEnumByFieldName() { EnumTest et = new EnumTest(); BeanWrapper bw = new BeanWrapperImpl(et); bw.setPropertyValue("flushMode", "NEVER"); assertEquals(FlushMode.NEVER, et.getFlushMode()); bw.setPropertyValue("flushMode", " AUTO "); assertEquals(FlushMode.AUTO, et.getFlushMode()); try {/*from ww w.j a v a2 s .c o m*/ bw.setPropertyValue("flushMode", "EVER"); fail("Should have thrown TypeMismatchException"); } catch (TypeMismatchException ex) { // expected } }
From source file:org.springframework.orm.hibernate3.HibernateAccessor.java
License:Apache License
/** * Apply the flush mode that's been specified for this accessor * to the given Session.// w w w .j a va 2s.com * @param session the current Hibernate Session * @param existingTransaction if executing within an existing transaction * @return the previous flush mode to restore after the operation, * or {@code null} if none * @see #setFlushMode * @see org.hibernate.Session#setFlushMode */ protected FlushMode applyFlushMode(Session session, boolean existingTransaction) { if (getFlushMode() == FLUSH_NEVER) { if (existingTransaction) { FlushMode previousFlushMode = session.getFlushMode(); if (!previousFlushMode.lessThan(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.MANUAL); return previousFlushMode; } } else { session.setFlushMode(FlushMode.MANUAL); } } else if (getFlushMode() == FLUSH_EAGER) { if (existingTransaction) { FlushMode previousFlushMode = session.getFlushMode(); if (!previousFlushMode.equals(FlushMode.AUTO)) { session.setFlushMode(FlushMode.AUTO); return previousFlushMode; } } else { // rely on default FlushMode.AUTO } } else if (getFlushMode() == FLUSH_COMMIT) { if (existingTransaction) { FlushMode previousFlushMode = session.getFlushMode(); if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) { session.setFlushMode(FlushMode.COMMIT); return previousFlushMode; } } else { session.setFlushMode(FlushMode.COMMIT); } } else if (getFlushMode() == FLUSH_ALWAYS) { if (existingTransaction) { FlushMode previousFlushMode = session.getFlushMode(); if (!previousFlushMode.equals(FlushMode.ALWAYS)) { session.setFlushMode(FlushMode.ALWAYS); return previousFlushMode; } } else { session.setFlushMode(FlushMode.ALWAYS); } } return null; }
From source file:org.springframework.orm.hibernate3.HibernateInterceptorTests.java
License:Apache License
@Test public void testInterceptorWithThreadBoundAndFlushEager() throws HibernateException { given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER); interceptor.setSessionFactory(sessionFactory); try {/*from w w w. j ava2 s .c o m*/ interceptor.invoke(invocation); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); } verify(session).flush(); }
From source file:org.springframework.orm.hibernate3.HibernateInterceptorTests.java
License:Apache License
@Test public void testInterceptorWithThreadBoundAndFlushEagerSwitch() throws HibernateException { given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.NEVER); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER); interceptor.setSessionFactory(sessionFactory); try {/*from ww w. jav a2 s. c om*/ interceptor.invoke(invocation); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); } InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.AUTO); ordered.verify(session).flush(); ordered.verify(session).setFlushMode(FlushMode.NEVER); }
From source file:org.springframework.orm.hibernate3.HibernateInterceptorTests.java
License:Apache License
@Test public void testInterceptorWithThreadBoundAndFlushCommit() { given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sessionFactory); interceptor.setFlushMode(HibernateInterceptor.FLUSH_COMMIT); try {//from w w w. jav a2 s. c o m interceptor.invoke(invocation); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); } InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.COMMIT); ordered.verify(session).setFlushMode(FlushMode.AUTO); verify(session, never()).flush(); }
From source file:org.springframework.orm.hibernate3.HibernateInterceptorTests.java
License:Apache License
@Test public void testInterceptorWithThreadBoundAndFlushAlways() { given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); HibernateInterceptor interceptor = new HibernateInterceptor(); interceptor.setSessionFactory(sessionFactory); interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS); try {//w w w.j a v a 2s .c o m interceptor.invoke(invocation); } catch (Throwable t) { fail("Should not have thrown Throwable: " + t.getMessage()); } finally { TransactionSynchronizationManager.unbindResource(sessionFactory); } InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.ALWAYS); ordered.verify(session).setFlushMode(FlushMode.AUTO); verify(session, never()).flush(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) private void doTestJtaTransactionCommit(int status, final boolean readOnly) throws Exception { UserTransaction ut = mock(UserTransaction.class); final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); Query query = mock(Query.class); if (status == Status.STATUS_NO_TRANSACTION) { given(ut.getStatus()).willReturn(status, Status.STATUS_ACTIVE); } else {/*from w w w .j a v a2 s .c om*/ given(ut.getStatus()).willReturn(status); } final List list = new ArrayList(); list.add("test"); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); given(session.isOpen()).willReturn(true); given(session.createQuery("some query string")).willReturn(query); given(query.list()).willReturn(list); given(session.getFlushMode()).willReturn(FlushMode.AUTO); JtaTransactionManager ptm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setReadOnly(readOnly); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); ht = new HibernateTemplate(sf); List htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); return sess.createQuery("some query string").list(); } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); return htl; } catch (Error err) { err.printStackTrace(); throw err; } } }); assertTrue("Correct result list", result == list); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); if (status == Status.STATUS_NO_TRANSACTION) { InOrder ordered = inOrder(ut); ordered.verify(ut).begin(); ordered.verify(ut).commit(); } if (readOnly) { verify(session).setFlushMode(FlushMode.MANUAL); } else { verify(session).flush(); } verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) private void doTestJtaTransactionCommitWithJtaTm(int status) throws Exception { UserTransaction ut = mock(UserTransaction.class); if (status == Status.STATUS_NO_TRANSACTION) { given(ut.getStatus()).willReturn(status, status, Status.STATUS_ACTIVE); } else {/*from ww w . ja v a2 s. co m*/ given(ut.getStatus()).willReturn(status); } 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(sf.openSession()).willReturn(session); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); JtaTransactionManager ptm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); final List l = new ArrayList(); l.add("test"); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); List htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); return htl; } catch (Error err) { err.printStackTrace(); throw err; } } }); assertTrue("Correct result list", result == l); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); if (status == Status.STATUS_NO_TRANSACTION) { InOrder ordered = inOrder(ut); ordered.verify(ut).begin(); ordered.verify(ut).commit(); } verify(session).flush(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testJtaTransactionWithFlushFailure() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE); final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); JtaTransactionManager ptm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); final List l = new ArrayList(); l.add("test"); final HibernateException flushEx = new HibernateException("flush failure"); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); willThrow(flushEx).given(session).flush(); try {/*from ww w .j a v a 2s. com*/ tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); List htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); return htl; } catch (Error err) { err.printStackTrace(); throw err; } } }); } catch (DataAccessException ex) { // expected assertTrue(flushEx == ex.getCause()); } assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).begin(); verify(ut).rollback(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void doTestJtaTransactionCommitWithPreBound(boolean jtaTm, final boolean flushNever, final boolean readOnly) throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); TransactionManager tm = mock(TransactionManager.class); if (jtaTm) {//from www . j av a 2 s .c om MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); } final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final ExtendedSession session = mock(ExtendedSession.class); given(sf.getTransactionManager()).willReturn(jtaTm ? tm : null); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(flushNever ? FlushMode.MANUAL : FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); try { JtaTransactionManager ptm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setReadOnly(readOnly); final List l = new ArrayList(); l.add("test"); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); Object result = tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); List htl = null; for (int i = 0; i < 5; i++) { htl = ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return l; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } return htl; } catch (Error err) { err.printStackTrace(); throw err; } } }); assertTrue("Correct result list", result == l); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); } finally { TransactionSynchronizationManager.unbindResource(sf); } verify(ut).begin(); verify(ut).commit(); if (flushNever) { if (!readOnly) { InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.AUTO); ordered.verify(session).setFlushMode(FlushMode.MANUAL); } } if (!flushNever && !readOnly) { verify(session).flush(); } verify(session).afterTransactionCompletion(true, null); verify(session).disconnect(); }