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.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testJtaTransactionRollbackWithPreBound() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK); RollbackException rex = new RollbackException(); willThrow(rex).given(ut).commit();/* w w w.j a v a2s . c o m*/ final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); given(session.getSessionFactory()).willReturn(sf); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); try { JtaTransactionManager ptm = new JtaTransactionManager(ut); final TransactionTemplate tt = new TransactionTemplate(ptm); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); tt.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { tt.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { status.setRollbackOnly(); try { assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true); for (int i = 0; i < 5; i++) { ht.execute(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session, sess); return null; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); } } catch (Error err) { err.printStackTrace(); throw err; } } }); } }); fail("Should have thrown UnexpectedRollbackException"); } catch (UnexpectedRollbackException ex) { // expected assertEquals(rex, ex.getCause()); } finally { TransactionSynchronizationManager.unbindResource(sf); } verify(ut).begin(); verify(ut).setRollbackOnly(); verify(session).flush(); verify(session).disconnect(); verify(session).clear(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void doTestJtaTransactionWithRequiresNew(final boolean rollback) throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); javax.transaction.Transaction tx1 = mock(javax.transaction.Transaction.class); final SessionFactory sf = mock(SessionFactory.class); final Session session1 = mock(Session.class); final Session session2 = mock(Session.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx1); given(sf.openSession()).willReturn(session1, session2); given(session1.getSessionFactory()).willReturn(sf); given(session2.getSessionFactory()).willReturn(sf); given(session1.isOpen()).willReturn(true); given(session2.isOpen()).willReturn(true); given(session2.getFlushMode()).willReturn(FlushMode.AUTO); if (!rollback) { given(session1.getFlushMode()).willReturn(FlushMode.AUTO); }/*from w w w. jav a2s. c om*/ JtaTransactionManager ptm = new JtaTransactionManager(); ptm.setUserTransaction(ut); ptm.setTransactionManager(tm); final TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); try { tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { org.hibernate.Session outerSession = SessionFactoryUtils.getSession(sf, false); assertSame(session1, outerSession); SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); assertTrue("Has thread session", holder != null); try { tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { org.hibernate.Session innerSession = SessionFactoryUtils.getSession(sf, false); assertSame(session2, innerSession); HibernateTemplate ht = new HibernateTemplate(sf); ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); return ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session innerSession) { if (rollback) { throw new HibernateException(""); } return null; } }); } }); return null; } finally { assertTrue("Same thread session as before", outerSession == SessionFactoryUtils.getSession(sf, false)); } } }); if (rollback) { fail("Should have thrown DataAccessException"); } } catch (DataAccessException ex) { if (!rollback) { throw ex; } } finally { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); } verify(ut, times(2)).begin(); verify(tm).resume(tx1); if (rollback) { verify(ut, times(2)).rollback(); } else { verify(ut, times(2)).commit(); } verify(session1).disconnect(); verify(session1).close(); if (!rollback) { verify(session1).flush(); verify(session2, atLeastOnce()).flush(); } verify(session2).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void doTestJtaTransactionWithRequiresNewAndJtaTm(final boolean rollback) throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); javax.transaction.Transaction tx1 = mock(javax.transaction.Transaction.class); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session1 = mock(Session.class); final Session session2 = mock(Session.class); MockJtaTransaction transaction1 = new MockJtaTransaction(); MockJtaTransaction transaction2 = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction1); given(tm.suspend()).willReturn(tx1); given(tm.getTransaction()).willReturn(transaction2); given(sf.getTransactionManager()).willReturn(tm); given(sf.openSession()).willReturn(session1, session2); given(session1.isOpen()).willReturn(true); given(session2.isOpen()).willReturn(true); given(session1.getFlushMode()).willReturn(FlushMode.AUTO); given(session2.getFlushMode()).willReturn(FlushMode.AUTO); JtaTransactionManager ptm = new JtaTransactionManager(); ptm.setUserTransaction(ut);/*from w ww . j a va2 s.com*/ ptm.setTransactionManager(tm); final TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); try { tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { org.hibernate.Session outerSession = SessionFactoryUtils.getSession(sf, false); assertSame(session1, outerSession); SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); assertTrue("Has thread session", holder != null); try { tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { org.hibernate.Session innerSession = SessionFactoryUtils.getSession(sf, false); assertSame(session2, innerSession); HibernateTemplate ht = new HibernateTemplate(sf); ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); return ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session innerSession) { if (rollback) { throw new HibernateException(""); } return null; } }); } }); return null; } finally { assertTrue("Same thread session as before", outerSession == SessionFactoryUtils.getSession(sf, false)); } } }); if (rollback) { fail("Should have thrown DataAccessException"); } } catch (DataAccessException ex) { if (!rollback) { throw ex; } } finally { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); } verify(ut, times(2)).begin(); verify(tm).resume(tx1); if (rollback) { verify(ut, times(2)).rollback(); } else { verify(ut, times(2)).commit(); verify(session1).flush(); verify(session2, times(2)).flush(); } verify(session1).disconnect(); verify(session1).close(); verify(session2).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testTransactionWithPropagationSupports() 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.getFlushMode()).willReturn(FlushMode.MANUAL); JtaTransactionManager tm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); tt.execute(new TransactionCallback() { @Override/*from w w w . j a va 2 s . c om*/ public Object doInTransaction(TransactionStatus status) { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("Is not new transaction", !status.isNewTransaction()); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); HibernateTemplate ht = new HibernateTemplate(sf); ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); ht.execute(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session session) { return null; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); return null; } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); InOrder ordered = inOrder(session); ordered.verify(session).setFlushMode(FlushMode.AUTO); ordered.verify(session).flush(); ordered.verify(session).setFlushMode(FlushMode.MANUAL); ordered.verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testTransactionWithPropagationSupportsAndInnerTransaction() 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 tm = new JtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(tm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); final TransactionTemplate tt2 = new TransactionTemplate(tm); tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); tt.execute(new TransactionCallback() { @Override/*ww w . j a v a2 s . c om*/ public Object doInTransaction(TransactionStatus status) { assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("Is not new transaction", !status.isNewTransaction()); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); HibernateTemplate ht = new HibernateTemplate(sf); ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); ht.execute(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session session) { return null; } }); assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); tt2.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { HibernateTemplate ht = new HibernateTemplate(sf); ht.setFlushMode(HibernateTemplate.FLUSH_EAGER); return ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session session) { assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); // assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); return null; } }); } }); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); return null; } }); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); verify(session, times(3)).flush(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronization() 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.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true);/* w ww. j a v a 2s .c o m*/ 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("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).flush(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithFlushFailure() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final HibernateException flushEx = new HibernateException("flush failure"); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); willThrow(flushEx).given(session).flush(); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true);/*from w w w . j a va 2 s . co m*/ 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); try { synchronization.beforeCompletion(); fail("Should have thrown HibernateSystemException"); } catch (HibernateSystemException ex) { assertSame(flushEx, ex.getCause()); } synchronization.afterCompletion(Status.STATUS_ROLLEDBACK); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).setRollbackOnly(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction1 = new MockJtaTransaction(); MockJtaTransaction transaction2 = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction1, transaction1, transaction2, transaction2, transaction2); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session1 = mock(Session.class); final Session session2 = mock(Session.class); given(sf.openSession()).willReturn(session1, session2); given(sf.getTransactionManager()).willReturn(tm); given(session1.getFlushMode()).willReturn(FlushMode.AUTO); given(session2.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true);//w w w . ja va 2 s . com ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session1, sess); return null; } }); ht.executeFind(new HibernateCallback() { @Override public Object doInHibernate(org.hibernate.Session sess) { assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); assertEquals(session2, sess); return null; } }); Synchronization synchronization2 = transaction2.getSynchronization(); assertTrue("JTA synchronization registered", synchronization2 != null); synchronization2.beforeCompletion(); synchronization2.afterCompletion(Status.STATUS_COMMITTED); Synchronization synchronization1 = transaction1.getSynchronization(); assertTrue("JTA synchronization registered", synchronization1 != null); synchronization1.beforeCompletion(); synchronization1.afterCompletion(Status.STATUS_COMMITTED); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session1).flush(); verify(session2).flush(); verify(session1).close(); verify(session2).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception { TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(tm.getTransaction()).willReturn(transaction); final SessionFactory sf = mock(SessionFactory.class); final Session session = mock(Session.class); final SessionFactoryImplementor sfi = mock(SessionFactoryImplementor.class); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sfi); given(sfi.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true);/*from w w w .j ava 2 s . c o m*/ 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("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).flush(); verify(session).close(); }
From source file:org.springframework.orm.hibernate3.HibernateJtaTransactionTests.java
License:Apache License
@Test @SuppressWarnings("rawtypes") public void testJtaSessionSynchronizationWithSpringTransactionLaterOn() throws Exception { UserTransaction ut = mock(UserTransaction.class); TransactionManager tm = mock(TransactionManager.class); MockJtaTransaction transaction = new MockJtaTransaction(); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.getTransaction()).willReturn(transaction); final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); final Session session = mock(Session.class); given(sf.openSession()).willReturn(session); given(sf.getTransactionManager()).willReturn(tm); given(session.isOpen()).willReturn(true); given(session.getFlushMode()).willReturn(FlushMode.AUTO); assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); final HibernateTemplate ht = new HibernateTemplate(sf); ht.setExposeNativeSession(true);// w ww .ja v a 2 s .c o m for (int i = 0; i < 2; 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; } }); } TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(ut)); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { for (int i = 2; 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("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(session).flush(); verify(session).close(); }