|
23 | 23 | import java.util.ArrayList;
|
24 | 24 | import java.util.List;
|
25 | 25 | import java.util.Properties;
|
26 |
| - |
27 | 26 | import javax.sql.DataSource;
|
28 | 27 |
|
29 | 28 | import org.hibernate.FlushMode;
|
|
36 | 35 | import org.hibernate.cfg.Configuration;
|
37 | 36 | import org.hibernate.classic.Session;
|
38 | 37 | import org.hibernate.dialect.HSQLDialect;
|
| 38 | +import org.hibernate.engine.SessionFactoryImplementor; |
39 | 39 | import org.hibernate.exception.ConstraintViolationException;
|
40 | 40 | import org.hibernate.exception.GenericJDBCException;
|
41 |
| - |
42 | 41 | import org.junit.After;
|
43 | 42 | import org.junit.Test;
|
44 |
| - |
45 | 43 | import org.mockito.InOrder;
|
46 | 44 |
|
47 | 45 | import org.springframework.beans.factory.BeanFactory;
|
@@ -615,6 +613,49 @@ public Object doInHibernate(org.hibernate.Session session) {
|
615 | 613 | ordered.verify(session).close();
|
616 | 614 | }
|
617 | 615 |
|
| 616 | + @Test |
| 617 | + public void testTransactionWithPropagationSupportsAndCurrentSession() throws Exception { |
| 618 | + final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class); |
| 619 | + final Session session = mock(Session.class); |
| 620 | + |
| 621 | + given(sf.openSession()).willReturn(session); |
| 622 | + given(session.getSessionFactory()).willReturn(sf); |
| 623 | + given(session.getFlushMode()).willReturn(FlushMode.MANUAL); |
| 624 | + |
| 625 | + LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() { |
| 626 | + @Override |
| 627 | + protected SessionFactory newSessionFactory(Configuration config) throws HibernateException { |
| 628 | + return sf; |
| 629 | + } |
| 630 | + }; |
| 631 | + lsfb.afterPropertiesSet(); |
| 632 | + final SessionFactory sfProxy = lsfb.getObject(); |
| 633 | + |
| 634 | + PlatformTransactionManager tm = new HibernateTransactionManager(sfProxy); |
| 635 | + TransactionTemplate tt = new TransactionTemplate(tm); |
| 636 | + tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); |
| 637 | + assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy)); |
| 638 | + |
| 639 | + tt.execute(new TransactionCallback() { |
| 640 | + @Override |
| 641 | + public Object doInTransaction(TransactionStatus status) { |
| 642 | + assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy)); |
| 643 | + assertTrue("Is not new transaction", !status.isNewTransaction()); |
| 644 | + assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); |
| 645 | + assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); |
| 646 | + Session session = new SpringSessionContext(sf).currentSession(); |
| 647 | + assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sfProxy)); |
| 648 | + session.flush(); |
| 649 | + return null; |
| 650 | + } |
| 651 | + }); |
| 652 | + |
| 653 | + assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy)); |
| 654 | + InOrder ordered = inOrder(session); |
| 655 | + ordered.verify(session).flush(); |
| 656 | + ordered.verify(session).close(); |
| 657 | + } |
| 658 | + |
618 | 659 | @Test
|
619 | 660 | public void testTransactionWithPropagationSupportsAndInnerTransaction() throws Exception {
|
620 | 661 | final SessionFactory sf = mock(SessionFactory.class);
|
@@ -900,7 +941,7 @@ public Object doInHibernate(org.hibernate.Session session) throws HibernateExcep
|
900 | 941 | catch (DataIntegrityViolationException ex) {
|
901 | 942 | // expected
|
902 | 943 | assertEquals(rootCause, ex.getCause());
|
903 |
| - assertTrue(ex.getMessage().indexOf("mymsg") != -1); |
| 944 | + assertTrue(ex.getMessage().contains("mymsg")); |
904 | 945 | }
|
905 | 946 |
|
906 | 947 | assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
|
|
0 commit comments