KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > HibernateJtaTransactionTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.hibernate3;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.Status JavaDoc;
24 import javax.transaction.Synchronization JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import junit.framework.TestCase;
29 import org.easymock.MockControl;
30 import org.hibernate.FlushMode;
31 import org.hibernate.HibernateException;
32 import org.hibernate.Query;
33 import org.hibernate.SessionFactory;
34 import org.hibernate.classic.Session;
35 import org.hibernate.engine.SessionFactoryImplementor;
36 import org.hibernate.engine.SessionImplementor;
37
38 import org.springframework.dao.DataAccessException;
39 import org.springframework.transaction.MockJtaTransaction;
40 import org.springframework.transaction.TransactionDefinition;
41 import org.springframework.transaction.TransactionStatus;
42 import org.springframework.transaction.UnexpectedRollbackException;
43 import org.springframework.transaction.jta.JtaTransactionManager;
44 import org.springframework.transaction.support.TransactionCallback;
45 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
46 import org.springframework.transaction.support.TransactionSynchronizationManager;
47 import org.springframework.transaction.support.TransactionTemplate;
48
49 /**
50  * @author Juergen Hoeller
51  * @since 05.03.2005
52  */

53 public class HibernateJtaTransactionTests extends TestCase {
54
55     public void testParticipatingJtaTransactionWithWithRequiresNew() throws Exception JavaDoc {
56         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
57         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
58         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
59         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
60         MockControl tx1Control = MockControl.createControl(javax.transaction.Transaction JavaDoc.class);
61         javax.transaction.Transaction JavaDoc tx1 = (javax.transaction.Transaction JavaDoc) tx1Control.getMock();
62         MockControl sfControl = MockControl.createControl(SessionFactory.class);
63         final SessionFactory sf = (SessionFactory) sfControl.getMock();
64         MockControl session1Control = MockControl.createControl(Session.class);
65         Session session1 = (Session) session1Control.getMock();
66         MockControl session2Control = MockControl.createControl(Session.class);
67         Session session2 = (Session) session2Control.getMock();
68
69         ut.getStatus();
70         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
71         ut.getStatus();
72         utControl.setReturnValue(Status.STATUS_ACTIVE, 3);
73         ut.begin();
74         utControl.setVoidCallable(2);
75         tm.suspend();
76         tmControl.setReturnValue(tx1, 1);
77         tm.resume(tx1);
78         tmControl.setVoidCallable(1);
79         ut.commit();
80         utControl.setVoidCallable(2);
81
82         sf.openSession();
83         sfControl.setReturnValue(session1, 1);
84         sf.openSession();
85         sfControl.setReturnValue(session2, 1);
86         session1.getSessionFactory();
87         session1Control.setReturnValue(sf, 1);
88         session2.getSessionFactory();
89         session2Control.setReturnValue(sf, 1);
90         session1.getFlushMode();
91         session1Control.setReturnValue(FlushMode.AUTO, 1);
92         session2.getFlushMode();
93         session2Control.setReturnValue(FlushMode.AUTO, 1);
94         session1.flush();
95         session1Control.setVoidCallable(1);
96         session2.flush();
97         session2Control.setVoidCallable(2);
98         session1.close();
99         session1Control.setReturnValue(null, 1);
100         session2.close();
101         session2Control.setReturnValue(null, 1);
102
103         utControl.replay();
104         tmControl.replay();
105         sfControl.replay();
106         session1Control.replay();
107         session2Control.replay();
108
109         JtaTransactionManager ptm = new JtaTransactionManager();
110         ptm.setUserTransaction(ut);
111         ptm.setTransactionManager(tm);
112         final TransactionTemplate tt = new TransactionTemplate(ptm);
113         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
114
115         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
116         tt.execute(new TransactionCallback() {
117             public Object JavaDoc doInTransaction(TransactionStatus status) {
118                 SessionFactoryUtils.getSession(sf, true);
119                 final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
120                 assertTrue("Has thread session", holder != null);
121                 tt.execute(new TransactionCallback() {
122                     public Object JavaDoc doInTransaction(TransactionStatus status) {
123                         HibernateTemplate ht = new HibernateTemplate(sf);
124                         ht.setFlushMode(HibernateTemplate.FLUSH_EAGER);
125                         return ht.executeFind(new HibernateCallback() {
126                             public Object JavaDoc doInHibernate(org.hibernate.Session session) {
127                                 assertTrue("Not enclosing session", session != holder.getSession());
128                                 return null;
129                             }
130                         });
131                     }
132                 });
133                 assertTrue("Same thread session as before",
134                         holder.getSession() == SessionFactoryUtils.getSession(sf, false));
135                 return null;
136             }
137         });
138         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
139
140         utControl.verify();
141         tmControl.verify();
142         sfControl.verify();
143         session1Control.verify();
144         session2Control.verify();
145     }
146
147     public void testJtaTransactionCommit() throws Exception JavaDoc {
148         doTestJtaTransactionCommit(Status.STATUS_NO_TRANSACTION, false);
149     }
150
151     public void testJtaTransactionCommitWithReadOnly() throws Exception JavaDoc {
152         doTestJtaTransactionCommit(Status.STATUS_NO_TRANSACTION, true);
153     }
154
155     public void testJtaTransactionCommitWithExisting() throws Exception JavaDoc {
156         doTestJtaTransactionCommit(Status.STATUS_ACTIVE, false);
157     }
158
159     public void testJtaTransactionCommitWithExistingAndReadOnly() throws Exception JavaDoc {
160         doTestJtaTransactionCommit(Status.STATUS_ACTIVE, true);
161     }
162
163     private void doTestJtaTransactionCommit(int status, final boolean readOnly) throws Exception JavaDoc {
164         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
165         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
166         MockControl sfControl = MockControl.createControl(SessionFactory.class);
167         final SessionFactory sf = (SessionFactory) sfControl.getMock();
168         final MockControl sessionControl = MockControl.createControl(Session.class);
169         final Session session = (Session) sessionControl.getMock();
170         final MockControl queryControl = MockControl.createControl(Query.class);
171         Query query = (Query) queryControl.getMock();
172
173         ut.getStatus();
174         utControl.setReturnValue(status, 1);
175         if (status == Status.STATUS_NO_TRANSACTION) {
176             ut.begin();
177             utControl.setVoidCallable(1);
178             ut.getStatus();
179             utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
180             ut.commit();
181             utControl.setVoidCallable(1);
182         }
183
184         final List JavaDoc list = new ArrayList JavaDoc();
185         list.add("test");
186         sf.openSession();
187         sfControl.setReturnValue(session, 1);
188         session.getSessionFactory();
189         sessionControl.setReturnValue(sf, 1);
190         session.createQuery("some query string");
191         sessionControl.setReturnValue(query, 1);
192         if (readOnly) {
193             session.setFlushMode(FlushMode.NEVER);
194             sessionControl.setVoidCallable(1);
195         }
196         query.list();
197         queryControl.setReturnValue(list, 1);
198
199         utControl.replay();
200         sfControl.replay();
201         sessionControl.replay();
202         queryControl.replay();
203
204         JtaTransactionManager ptm = new JtaTransactionManager(ut);
205         TransactionTemplate tt = new TransactionTemplate(ptm);
206         tt.setReadOnly(readOnly);
207         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
208         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
209
210         Object JavaDoc result = tt.execute(new TransactionCallback() {
211             public Object JavaDoc doInTransaction(TransactionStatus status) {
212                 try {
213                     assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
214                     assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
215                     HibernateTemplate ht = new HibernateTemplate(sf);
216                     ht.setExposeNativeSession(true);
217                     ht.executeFind(new HibernateCallback() {
218                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
219                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
220                             assertEquals(session, sess);
221                             return null;
222                         }
223                     });
224                     ht = new HibernateTemplate(sf);
225                     List JavaDoc htl = ht.executeFind(new HibernateCallback() {
226                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
227                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
228                             return sess.createQuery("some query string").list();
229                         }
230                     });
231                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
232                     sessionControl.verify();
233                     queryControl.verify();
234                     sessionControl.reset();
235                     try {
236                         if (!readOnly) {
237                             session.getFlushMode();
238                             sessionControl.setReturnValue(FlushMode.AUTO, 1);
239                             session.flush();
240                             sessionControl.setVoidCallable(1);
241                         }
242                         session.close();
243                         sessionControl.setReturnValue(null, 1);
244                     }
245                     catch (HibernateException e) {
246                     }
247                     sessionControl.replay();
248                     return htl;
249                 }
250                 catch (Error JavaDoc err) {
251                     err.printStackTrace();
252                     throw err;
253                 }
254             }
255         });
256
257         assertTrue("Correct result list", result == list);
258         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
259         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
260
261         utControl.verify();
262         sfControl.verify();
263         sessionControl.verify();
264     }
265
266     public void testJtaTransactionCommitWithJtaTm() throws Exception JavaDoc {
267         doTestJtaTransactionCommitWithJtaTm(Status.STATUS_NO_TRANSACTION);
268     }
269
270     public void testJtaTransactionCommitWithJtaTmAndExisting() throws Exception JavaDoc {
271         doTestJtaTransactionCommitWithJtaTm(Status.STATUS_ACTIVE);
272     }
273
274     private void doTestJtaTransactionCommitWithJtaTm(int status) throws Exception JavaDoc {
275         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
276         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
277         ut.getStatus();
278         utControl.setReturnValue(status, 1);
279         if (status == Status.STATUS_NO_TRANSACTION) {
280             ut.begin();
281             utControl.setVoidCallable(1);
282             ut.getStatus();
283             utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
284             ut.commit();
285             utControl.setVoidCallable(1);
286         }
287
288         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
289         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
290         MockJtaTransaction transaction = new MockJtaTransaction();
291         tm.getStatus();
292         tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
293         tm.getTransaction();
294         tmControl.setReturnValue(transaction, 6);
295
296         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
297         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
298         final MockControl sessionControl = MockControl.createControl(SessionImplementor.class);
299         final SessionImplementor session = (SessionImplementor) sessionControl.getMock();
300         sf.getConnectionProvider();
301         sfControl.setReturnValue(null, 2);
302         sf.getTransactionManager();
303         sfControl.setReturnValue(tm, 1);
304         sf.openSession();
305         sfControl.setReturnValue(session, 1);
306
307         utControl.replay();
308         tmControl.replay();
309         sfControl.replay();
310         sessionControl.replay();
311
312         JtaTransactionManager ptm = new JtaTransactionManager(ut);
313         TransactionTemplate tt = new TransactionTemplate(ptm);
314         final List JavaDoc l = new ArrayList JavaDoc();
315         l.add("test");
316         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
317         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
318
319         Object JavaDoc result = tt.execute(new TransactionCallback() {
320             public Object JavaDoc doInTransaction(TransactionStatus status) {
321                 try {
322                     assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
323                     assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
324                     HibernateTemplate ht = new HibernateTemplate(sf);
325                     ht.setExposeNativeSession(true);
326                     List JavaDoc htl = ht.executeFind(new HibernateCallback() {
327                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
328                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
329                             assertEquals(session, sess);
330                             return l;
331                         }
332                     });
333                     ht = new HibernateTemplate(sf);
334                     ht.setExposeNativeSession(true);
335                     htl = ht.executeFind(new HibernateCallback() {
336                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
337                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
338                             assertEquals(session, sess);
339                             return l;
340                         }
341                     });
342                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
343                     sessionControl.verify();
344                     sessionControl.reset();
345                     try {
346                         session.getFlushMode();
347                         sessionControl.setReturnValue(FlushMode.AUTO, 1);
348                         session.flush();
349                         sessionControl.setVoidCallable(1);
350                         session.close();
351                         sessionControl.setReturnValue(null, 1);
352                     }
353                     catch (HibernateException e) {
354                     }
355                     sessionControl.replay();
356                     return htl;
357                 }
358                 catch (Error JavaDoc err) {
359                     err.printStackTrace();
360                     throw err;
361                 }
362             }
363         });
364
365         assertTrue("Correct result list", result == l);
366         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
367         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
368
369         utControl.verify();
370         sfControl.verify();
371         sessionControl.verify();
372     }
373
374     public void testJtaTransactionWithFlushFailure() throws Exception JavaDoc {
375         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
376         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
377         ut.getStatus();
378         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
379         ut.begin();
380         utControl.setVoidCallable(1);
381         ut.rollback();
382         utControl.setVoidCallable(1);
383         utControl.replay();
384
385         MockControl sfControl = MockControl.createControl(SessionFactory.class);
386         final SessionFactory sf = (SessionFactory) sfControl.getMock();
387         final MockControl sessionControl = MockControl.createControl(Session.class);
388         final Session session = (Session) sessionControl.getMock();
389         sf.openSession();
390         sfControl.setReturnValue(session, 1);
391         session.getSessionFactory();
392         sessionControl.setReturnValue(sf, 1);
393         sfControl.replay();
394         sessionControl.replay();
395
396         JtaTransactionManager ptm = new JtaTransactionManager(ut);
397         TransactionTemplate tt = new TransactionTemplate(ptm);
398         final List JavaDoc l = new ArrayList JavaDoc();
399         l.add("test");
400         final HibernateException flushEx = new HibernateException("flush failure");
401         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
402         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
403
404         try {
405             tt.execute(new TransactionCallback() {
406                 public Object JavaDoc doInTransaction(TransactionStatus status) {
407                     try {
408                         assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
409                         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
410                         HibernateTemplate ht = new HibernateTemplate(sf);
411                         ht.setExposeNativeSession(true);
412                         List JavaDoc htl = ht.executeFind(new HibernateCallback() {
413                             public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
414                                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
415                                 assertEquals(session, sess);
416                                 return l;
417                             }
418                         });
419                         ht = new HibernateTemplate(sf);
420                         ht.setExposeNativeSession(true);
421                         htl = ht.executeFind(new HibernateCallback() {
422                             public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
423                                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
424                                 assertEquals(session, sess);
425                                 return l;
426                             }
427                         });
428                         assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
429                         sessionControl.verify();
430                         sessionControl.reset();
431                         try {
432                             session.getFlushMode();
433                             sessionControl.setReturnValue(FlushMode.AUTO, 1);
434                             session.flush();
435                             sessionControl.setThrowable(flushEx);
436                             session.close();
437                             sessionControl.setReturnValue(null, 1);
438                         }
439                         catch (HibernateException e) {
440                         }
441                         sessionControl.replay();
442                         return htl;
443                     }
444                     catch (Error JavaDoc err) {
445                         err.printStackTrace();
446                         throw err;
447                     }
448                 }
449             });
450         }
451         catch (DataAccessException ex) {
452             // expected
453
assertTrue(flushEx == ex.getCause());
454         }
455
456         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
457         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
458
459         utControl.verify();
460         sfControl.verify();
461         sessionControl.verify();
462     }
463
464     public void testJtaTransactionRollback() throws Exception JavaDoc {
465         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
466         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
467         ut.getStatus();
468         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
469         ut.begin();
470         utControl.setVoidCallable(1);
471         ut.rollback();
472         utControl.setVoidCallable(1);
473         utControl.replay();
474
475         MockControl sfControl = MockControl.createControl(SessionFactory.class);
476         final SessionFactory sf = (SessionFactory) sfControl.getMock();
477         final MockControl sessionControl = MockControl.createControl(Session.class);
478         final Session session = (Session) sessionControl.getMock();
479         sf.openSession();
480         sfControl.setReturnValue(session, 1);
481         session.getSessionFactory();
482         sessionControl.setReturnValue(sf, 1);
483         sfControl.replay();
484         sessionControl.replay();
485
486         JtaTransactionManager ptm = new JtaTransactionManager(ut);
487         TransactionTemplate tt = new TransactionTemplate(ptm);
488         final List JavaDoc l = new ArrayList JavaDoc();
489         l.add("test");
490         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
491
492         Object JavaDoc result = tt.execute(new TransactionCallback() {
493             public Object JavaDoc doInTransaction(TransactionStatus status) {
494                 try {
495                     assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
496                     HibernateTemplate ht = new HibernateTemplate(sf);
497                     List JavaDoc htl = ht.executeFind(new HibernateCallback() {
498                         public Object JavaDoc doInHibernate(org.hibernate.Session session) {
499                             return l;
500                         }
501                     });
502                     status.setRollbackOnly();
503                     sessionControl.verify();
504                     sessionControl.reset();
505                     try {
506                         session.close();
507                     }
508                     catch (HibernateException ex) {
509                     }
510                     sessionControl.setReturnValue(null, 1);
511                     sessionControl.replay();
512                     return htl;
513                 }
514                 catch (Error JavaDoc err) {
515                     err.printStackTrace();
516                     throw err;
517                 }
518             }
519         });
520         assertTrue("Correct result list", result == l);
521
522         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
523         utControl.verify();
524         sfControl.verify();
525         sessionControl.verify();
526     }
527
528     public void testJtaTransactionCommitWithPreBound() throws Exception JavaDoc {
529         doTestJtaTransactionCommitWithPreBound(false, false, false);
530     }
531
532     public void testJtaTransactionCommitWithPreBoundAndReadOnly() throws Exception JavaDoc {
533         doTestJtaTransactionCommitWithPreBound(false, false, true);
534     }
535
536     public void testJtaTransactionCommitWithPreBoundAndFlushModeNever() throws Exception JavaDoc {
537         doTestJtaTransactionCommitWithPreBound(false, true, false);
538     }
539
540     public void testJtaTransactionCommitWithPreBoundAndFlushModeNeverAndReadOnly() throws Exception JavaDoc {
541         doTestJtaTransactionCommitWithPreBound(false, true, true);
542     }
543
544     public void testJtaTransactionCommitWithJtaTmAndPreBound() throws Exception JavaDoc {
545         doTestJtaTransactionCommitWithPreBound(true, false, false);
546     }
547
548     public void testJtaTransactionCommitWithJtaTmAndPreBoundAndReadOnly() throws Exception JavaDoc {
549         doTestJtaTransactionCommitWithPreBound(true, false, true);
550     }
551
552     public void testJtaTransactionCommitWithJtaTmAndPreBoundAndFlushModeNever() throws Exception JavaDoc {
553         doTestJtaTransactionCommitWithPreBound(true, true, false);
554     }
555
556     public void testJtaTransactionCommitWithJtaTmAndPreBoundAndFlushModeNeverAndReadOnly() throws Exception JavaDoc {
557         doTestJtaTransactionCommitWithPreBound(true, true, true);
558     }
559
560     protected void doTestJtaTransactionCommitWithPreBound(
561             boolean jtaTm, final boolean flushNever, final boolean readOnly) throws Exception JavaDoc {
562
563         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
564         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
565         ut.getStatus();
566         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
567         ut.getStatus();
568         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
569         ut.begin();
570         utControl.setVoidCallable(1);
571         ut.commit();
572         utControl.setVoidCallable(1);
573
574         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
575         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
576         if (jtaTm) {
577             MockJtaTransaction transaction = new MockJtaTransaction();
578             tm.getStatus();
579             tmControl.setReturnValue(Status.STATUS_ACTIVE, 1);
580             tm.getTransaction();
581             tmControl.setReturnValue(transaction, 1);
582         }
583
584         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
585         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
586         final MockControl sessionControl = MockControl.createControl(SessionImplementor.class);
587         final SessionImplementor session = (SessionImplementor) sessionControl.getMock();
588         sf.getConnectionProvider();
589         sfControl.setReturnValue(null, 1);
590         sf.getTransactionManager();
591         sfControl.setReturnValue((jtaTm ? tm : null), 1);
592         session.getFlushMode();
593         if (flushNever) {
594             sessionControl.setReturnValue(FlushMode.NEVER, 1);
595             if (!readOnly) {
596                 session.setFlushMode(FlushMode.AUTO);
597                 sessionControl.setVoidCallable(1);
598             }
599         }
600         else {
601             sessionControl.setReturnValue(FlushMode.AUTO, 1);
602         }
603
604         utControl.replay();
605         tmControl.replay();
606         sfControl.replay();
607         sessionControl.replay();
608
609         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
610         try {
611             JtaTransactionManager ptm = new JtaTransactionManager(ut);
612             TransactionTemplate tt = new TransactionTemplate(ptm);
613             tt.setReadOnly(readOnly);
614             final List JavaDoc l = new ArrayList JavaDoc();
615             l.add("test");
616             assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
617             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
618
619             Object JavaDoc result = tt.execute(new TransactionCallback() {
620                 public Object JavaDoc doInTransaction(TransactionStatus status) {
621                     try {
622                         assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
623                         assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
624                         HibernateTemplate ht = new HibernateTemplate(sf);
625                         ht.setExposeNativeSession(true);
626                         List JavaDoc htl = null;
627                         for (int i = 0; i < 5; i++) {
628                             htl = ht.executeFind(new HibernateCallback() {
629                                 public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
630                                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
631                                     assertEquals(session, sess);
632                                     return l;
633                                 }
634                             });
635                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
636                         }
637                         sessionControl.verify();
638                         sessionControl.reset();
639                         try {
640                             if (!readOnly) {
641                                 session.getFlushMode();
642                                 sessionControl.setReturnValue(FlushMode.AUTO, 1);
643                                 session.flush();
644                                 sessionControl.setVoidCallable(1);
645                                 if (flushNever) {
646                                     session.setFlushMode(FlushMode.NEVER);
647                                     sessionControl.setVoidCallable(1);
648                                 }
649                             }
650                             session.afterTransactionCompletion(true, null);
651                             sessionControl.setVoidCallable(1);
652                         }
653                         catch (HibernateException e) {
654                         }
655                         sessionControl.replay();
656                         return htl;
657                     }
658                     catch (Error JavaDoc err) {
659                         err.printStackTrace();
660                         throw err;
661                     }
662                 }
663             });
664
665             assertTrue("Correct result list", result == l);
666             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
667             assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
668         }
669         finally {
670             TransactionSynchronizationManager.unbindResource(sf);
671         }
672
673         utControl.verify();
674         tmControl.verify();
675         sfControl.verify();
676         sessionControl.verify();
677     }
678
679     public void testJtaTransactionRollbackWithPreBound() throws Exception JavaDoc {
680         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
681         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
682         ut.getStatus();
683         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
684         ut.getStatus();
685         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
686         ut.getStatus();
687         utControl.setReturnValue(Status.STATUS_MARKED_ROLLBACK, 1);
688         ut.begin();
689         utControl.setVoidCallable(1);
690         ut.setRollbackOnly();
691         utControl.setVoidCallable(1);
692         RollbackException JavaDoc rex = new RollbackException JavaDoc();
693         ut.commit();
694         utControl.setThrowable(rex, 1);
695         utControl.replay();
696
697         MockControl sfControl = MockControl.createControl(SessionFactory.class);
698         final SessionFactory sf = (SessionFactory) sfControl.getMock();
699         final MockControl sessionControl = MockControl.createControl(Session.class);
700         final Session session = (Session) sessionControl.getMock();
701         session.getSessionFactory();
702         sessionControl.setReturnValue(sf, 1);
703         session.getFlushMode();
704         sessionControl.setReturnValue(FlushMode.AUTO, 1);
705         sfControl.replay();
706         sessionControl.replay();
707
708         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
709         try {
710             JtaTransactionManager ptm = new JtaTransactionManager(ut);
711             final TransactionTemplate tt = new TransactionTemplate(ptm);
712             assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
713             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
714
715             tt.execute(new TransactionCallbackWithoutResult() {
716                 public void doInTransactionWithoutResult(TransactionStatus status) {
717                     tt.execute(new TransactionCallbackWithoutResult() {
718                         public void doInTransactionWithoutResult(TransactionStatus status) {
719                             status.setRollbackOnly();
720                             try {
721                                 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
722                                 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
723                                 HibernateTemplate ht = new HibernateTemplate(sf);
724                                 ht.setExposeNativeSession(true);
725                                 for (int i = 0; i < 5; i++) {
726                                     ht.execute(new HibernateCallback() {
727                                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
728                                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
729                                             assertEquals(session, sess);
730                                             return null;
731                                         }
732                                     });
733                                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
734                                 }
735                                 sessionControl.verify();
736                                 sessionControl.reset();
737                                 session.getFlushMode();
738                                 sessionControl.setReturnValue(FlushMode.AUTO, 1);
739                                 session.flush();
740                                 sessionControl.setVoidCallable(1);
741                                 session.clear();
742                                 sessionControl.setVoidCallable(1);
743                                 sessionControl.replay();
744                             }
745                             catch (Error JavaDoc err) {
746                                 err.printStackTrace();
747                                 throw err;
748                             }
749                         }
750                     });
751                 }
752             });
753             fail("Should have thrown UnexpectedRollbackException");
754         }
755         catch (UnexpectedRollbackException ex) {
756             // expected
757
assertEquals(rex, ex.getCause());
758         }
759         finally {
760             TransactionSynchronizationManager.unbindResource(sf);
761         }
762
763         utControl.verify();
764         sfControl.verify();
765         sessionControl.verify();
766     }
767
768     public void testJtaSessionSynchronization() throws Exception JavaDoc {
769         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
770         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
771         MockJtaTransaction transaction = new MockJtaTransaction();
772         tm.getStatus();
773         tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
774         tm.getTransaction();
775         tmControl.setReturnValue(transaction, 6);
776
777         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
778         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
779         final MockControl sessionControl = MockControl.createControl(Session.class);
780         final Session session = (Session) sessionControl.getMock();
781         sf.getConnectionProvider();
782         sfControl.setReturnValue(null, 1);
783         sf.openSession();
784         sfControl.setReturnValue(session, 1);
785         sf.getTransactionManager();
786         sfControl.setReturnValue(tm, 6);
787         session.getFlushMode();
788         sessionControl.setReturnValue(FlushMode.AUTO, 1);
789         session.flush();
790         sessionControl.setVoidCallable(1);
791         session.close();
792         sessionControl.setReturnValue(null, 1);
793
794         tmControl.replay();
795         sfControl.replay();
796         sessionControl.replay();
797
798         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
799         HibernateTemplate ht = new HibernateTemplate(sf);
800         ht.setExposeNativeSession(true);
801         for (int i = 0; i < 5; i++) {
802             ht.executeFind(new HibernateCallback() {
803                 public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
804                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
805                     assertEquals(session, sess);
806                     return null;
807                 }
808             });
809         }
810
811         Synchronization JavaDoc synchronization = transaction.getSynchronization();
812         assertTrue("JTA synchronization registered", synchronization != null);
813         synchronization.beforeCompletion();
814         synchronization.afterCompletion(Status.STATUS_COMMITTED);
815
816         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
817         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
818
819         tmControl.verify();
820         sfControl.verify();
821         sessionControl.verify();
822     }
823
824     public void testJtaSessionSynchronizationWithRollback() throws Exception JavaDoc {
825         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
826         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
827         MockJtaTransaction transaction = new MockJtaTransaction();
828         tm.getStatus();
829         tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
830         tm.getTransaction();
831         tmControl.setReturnValue(transaction, 6);
832
833         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
834         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
835         final MockControl sessionControl = MockControl.createControl(Session.class);
836         final Session session = (Session) sessionControl.getMock();
837         sf.getConnectionProvider();
838         sfControl.setReturnValue(null, 1);
839         sf.openSession();
840         sfControl.setReturnValue(session, 1);
841         sf.getTransactionManager();
842         sfControl.setReturnValue(tm, 6);
843         session.close();
844         sessionControl.setReturnValue(null, 1);
845
846         tmControl.replay();
847         sfControl.replay();
848         sessionControl.replay();
849
850         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
851         HibernateTemplate ht = new HibernateTemplate(sf);
852         ht.setExposeNativeSession(true);
853         for (int i = 0; i < 5; i++) {
854             ht.executeFind(new HibernateCallback() {
855                 public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
856                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
857                     assertEquals(session, sess);
858                     return null;
859                 }
860             });
861         }
862
863         Synchronization JavaDoc synchronization = transaction.getSynchronization();
864         assertTrue("JTA synchronization registered", synchronization != null);
865         synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);
866
867         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
868         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
869
870         tmControl.verify();
871         sfControl.verify();
872         sessionControl.verify();
873     }
874
875     public void testJtaSessionSynchronizationWithRollbackByOtherThread() throws Exception JavaDoc {
876         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
877         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
878         MockJtaTransaction transaction = new MockJtaTransaction();
879         tm.getStatus();
880         tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
881         tm.getStatus();
882         tmControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 2);
883         tm.getTransaction();
884         tmControl.setReturnValue(transaction, 6);
885
886         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
887         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
888         final MockControl sessionControl = MockControl.createControl(Session.class);
889         final Session session = (Session) sessionControl.getMock();
890         sf.getConnectionProvider();
891         sfControl.setReturnValue(null, 1);
892         sf.openSession();
893         sfControl.setReturnValue(session, 2);
894         sf.getTransactionManager();
895         sfControl.setReturnValue(tm, 7);
896         session.setFlushMode(FlushMode.NEVER);
897         sessionControl.setVoidCallable(1);
898         session.close();
899         sessionControl.setReturnValue(null, 2);
900
901         tmControl.replay();
902         sfControl.replay();
903         sessionControl.replay();
904
905         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
906         final HibernateTemplate ht = new HibernateTemplate(sf);
907         ht.setExposeNativeSession(true);
908         for (int i = 0; i < 5; i++) {
909             ht.executeFind(new HibernateCallback() {
910                 public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
911                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
912                     assertEquals(session, sess);
913                     return null;
914                 }
915             });
916         }
917
918         final Synchronization JavaDoc synchronization = transaction.getSynchronization();
919         assertTrue("JTA synchronization registered", synchronization != null);
920         Thread JavaDoc thread = new Thread JavaDoc() {
921             public void run() {
922                 synchronization.afterCompletion(Status.STATUS_ROLLEDBACK);
923             }
924         };
925         thread.start();
926         thread.join();
927
928         assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
929         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
930
931         TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(tm));
932         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
933         tt.setReadOnly(true);
934         tt.execute(new TransactionCallbackWithoutResult() {
935             protected void doInTransactionWithoutResult(TransactionStatus status) {
936                 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
937                 for (int i = 0; i < 5; i++) {
938                     ht.executeFind(new HibernateCallback() {
939                         public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
940                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
941                             assertEquals(session, sess);
942                             return null;
943                         }
944                     });
945                 }
946             }
947         });
948
949         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
950         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
951
952         tmControl.verify();
953         sfControl.verify();
954         sessionControl.verify();
955     }
956
957     public void testJtaSessionSynchronizationWithFlushFailure() throws Exception JavaDoc {
958         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
959         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
960         MockJtaTransaction transaction = new MockJtaTransaction();
961         tm.getStatus();
962         tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
963         tm.getTransaction();
964         tmControl.setReturnValue(transaction, 6);
965         tm.setRollbackOnly();
966         tmControl.setVoidCallable(1);
967
968         final HibernateException flushEx = new HibernateException("flush failure");
969         MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
970         final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
971         final MockControl sessionControl = MockControl.createControl(Session.class);
972         final Session session = (Session) sessionControl.getMock();
973         sf.getConnectionProvider();
974         sfControl.setReturnValue(null, 1);
975         sf.openSession();
976         sfControl.setReturnValue(session, 1);
977         sf.getTransactionManager();
978         sfControl.setReturnValue(tm, 6);
979         session.getFlushMode();
980         sessionControl.setReturnValue(FlushMode.AUTO, 1);
981         session.flush();
982         sessionControl.setThrowable(flushEx, 1);
983         session.close();
984         sessionControl.setReturnValue(null, 1);
985
986         tmControl.replay();
987         sfControl.replay();
988         sessionControl.replay();
989
990         assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
991         HibernateTemplate ht = new HibernateTemplate(sf);
992         ht.setExposeNativeSession(true);
993         for (int i = 0; i < 5; i++) {
994             ht.executeFind(new HibernateCallback() {
995                 public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
996                     assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
997                     assertEquals(session, sess);
998                     return null;
999                 }
1000            });
1001        }
1002
1003        Synchronization JavaDoc synchronization = transaction.getSynchronization();
1004        assertTrue("JTA synchronization registered", synchronization != null);
1005        synchronization.beforeCompletion();
1006        synchronization.afterCompletion(Status.STATUS_COMMITTED);
1007
1008        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1009        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1010
1011        tmControl.verify();
1012        sfControl.verify();
1013        sessionControl.verify();
1014    }
1015
1016    public void testJtaSessionSynchronizationWithSuspendedTransaction() throws Exception JavaDoc {
1017        MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
1018        TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
1019        MockJtaTransaction transaction1 = new MockJtaTransaction();
1020        MockJtaTransaction transaction2 = new MockJtaTransaction();
1021        tm.getStatus();
1022        tmControl.setReturnValue(Status.STATUS_ACTIVE, 5);
1023        tm.getTransaction();
1024        tmControl.setReturnValue(transaction1, 2);
1025        tm.getTransaction();
1026        tmControl.setReturnValue(transaction2, 3);
1027
1028        MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
1029        final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
1030        final MockControl session1Control = MockControl.createControl(Session.class);
1031        final Session session1 = (Session) session1Control.getMock();
1032        final MockControl session2Control = MockControl.createControl(Session.class);
1033        final Session session2 = (Session) session2Control.getMock();
1034        sf.getConnectionProvider();
1035        sfControl.setReturnValue(null, 1);
1036        sf.openSession();
1037        sfControl.setReturnValue(session1, 1);
1038        sf.openSession();
1039        sfControl.setReturnValue(session2, 1);
1040        sf.getTransactionManager();
1041        sfControl.setReturnValue(tm, 5);
1042        session1.getFlushMode();
1043        session1Control.setReturnValue(FlushMode.AUTO, 1);
1044        session2.getFlushMode();
1045        session2Control.setReturnValue(FlushMode.AUTO, 1);
1046        session1.flush();
1047        session1Control.setVoidCallable(1);
1048        session2.flush();
1049        session2Control.setVoidCallable(1);
1050        session1.close();
1051        session1Control.setReturnValue(null, 1);
1052        session2.close();
1053        session2Control.setReturnValue(null, 1);
1054
1055        tmControl.replay();
1056        sfControl.replay();
1057        session1Control.replay();
1058        session2Control.replay();
1059
1060        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1061        HibernateTemplate ht = new HibernateTemplate(sf);
1062        ht.setExposeNativeSession(true);
1063        ht.executeFind(new HibernateCallback() {
1064            public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1065                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1066                assertEquals(session1, sess);
1067                return null;
1068            }
1069        });
1070        ht.executeFind(new HibernateCallback() {
1071            public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1072                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1073                assertEquals(session2, sess);
1074                return null;
1075            }
1076        });
1077
1078        Synchronization JavaDoc synchronization2 = transaction2.getSynchronization();
1079        assertTrue("JTA synchronization registered", synchronization2 != null);
1080        synchronization2.beforeCompletion();
1081        synchronization2.afterCompletion(Status.STATUS_COMMITTED);
1082
1083        Synchronization JavaDoc synchronization1 = transaction1.getSynchronization();
1084        assertTrue("JTA synchronization registered", synchronization1 != null);
1085        synchronization1.beforeCompletion();
1086        synchronization1.afterCompletion(Status.STATUS_COMMITTED);
1087
1088        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1089        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1090
1091        tmControl.verify();
1092        sfControl.verify();
1093        session1Control.verify();
1094        session2Control.verify();
1095    }
1096
1097    public void testJtaSessionSynchronizationWithNonSessionFactoryImplementor() throws Exception JavaDoc {
1098        MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
1099        TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
1100        MockJtaTransaction transaction = new MockJtaTransaction();
1101        tm.getStatus();
1102        tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
1103        tm.getTransaction();
1104        tmControl.setReturnValue(transaction, 6);
1105
1106        MockControl sfControl = MockControl.createControl(SessionFactory.class);
1107        final SessionFactory sf = (SessionFactory) sfControl.getMock();
1108        final MockControl sessionControl = MockControl.createControl(Session.class);
1109        final Session session = (Session) sessionControl.getMock();
1110        MockControl sfiControl = MockControl.createControl(SessionFactoryImplementor.class);
1111        final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sfiControl.getMock();
1112        sf.openSession();
1113        sfControl.setReturnValue(session, 1);
1114        session.getSessionFactory();
1115        sessionControl.setReturnValue(sfi, 6);
1116        sfi.getTransactionManager();
1117        sfiControl.setReturnValue(tm, 6);
1118        session.getFlushMode();
1119        sessionControl.setReturnValue(FlushMode.AUTO, 1);
1120        session.flush();
1121        sessionControl.setVoidCallable(1);
1122        session.close();
1123        sessionControl.setReturnValue(null, 1);
1124
1125        tmControl.replay();
1126        sfControl.replay();
1127        sessionControl.replay();
1128        sfiControl.replay();
1129
1130        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1131        HibernateTemplate ht = new HibernateTemplate(sf);
1132        ht.setExposeNativeSession(true);
1133        for (int i = 0; i < 5; i++) {
1134            ht.executeFind(new HibernateCallback() {
1135                public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1136                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1137                    assertEquals(session, sess);
1138                    return null;
1139                }
1140            });
1141        }
1142
1143        Synchronization JavaDoc synchronization = transaction.getSynchronization();
1144        assertTrue("JTA Synchronization registered", synchronization != null);
1145        synchronization.beforeCompletion();
1146        synchronization.afterCompletion(Status.STATUS_COMMITTED);
1147
1148        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1149        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1150
1151        tmControl.verify();
1152        sfControl.verify();
1153        sessionControl.verify();
1154        sfiControl.verify();
1155    }
1156
1157    public void testJtaSessionSynchronizationWithSpringTransactionLaterOn() throws Exception JavaDoc {
1158        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1159        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1160        MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
1161        TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
1162        MockJtaTransaction transaction = new MockJtaTransaction();
1163        ut.getStatus();
1164        utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
1165        tm.getStatus();
1166        tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
1167        tm.getTransaction();
1168        tmControl.setReturnValue(transaction, 6);
1169
1170        MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
1171        final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
1172        final MockControl sessionControl = MockControl.createControl(Session.class);
1173        final Session session = (Session) sessionControl.getMock();
1174        sf.getConnectionProvider();
1175        sfControl.setReturnValue(null, 1);
1176        sf.openSession();
1177        sfControl.setReturnValue(session, 1);
1178        sf.getTransactionManager();
1179        sfControl.setReturnValue(tm, 6);
1180        session.getFlushMode();
1181        sessionControl.setReturnValue(FlushMode.AUTO, 1);
1182        session.flush();
1183        sessionControl.setVoidCallable(1);
1184        session.close();
1185        sessionControl.setReturnValue(null, 1);
1186
1187        utControl.replay();
1188        tmControl.replay();
1189        sfControl.replay();
1190        sessionControl.replay();
1191
1192        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1193        final HibernateTemplate ht = new HibernateTemplate(sf);
1194        ht.setExposeNativeSession(true);
1195        for (int i = 0; i < 2; i++) {
1196            ht.executeFind(new HibernateCallback() {
1197                public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1198                    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1199                    assertEquals(session, sess);
1200                    return null;
1201                }
1202            });
1203        }
1204
1205        TransactionTemplate tt = new TransactionTemplate(new JtaTransactionManager(ut));
1206        tt.execute(new TransactionCallbackWithoutResult() {
1207            protected void doInTransactionWithoutResult(TransactionStatus status) {
1208                for (int i = 2; i < 5; i++) {
1209                    ht.executeFind(new HibernateCallback() {
1210                        public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1211                            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1212                            assertEquals(session, sess);
1213                            return null;
1214                        }
1215                    });
1216                }
1217            }
1218        });
1219
1220        Synchronization JavaDoc synchronization = transaction.getSynchronization();
1221        assertTrue("JTA synchronization registered", synchronization != null);
1222        synchronization.beforeCompletion();
1223        synchronization.afterCompletion(Status.STATUS_COMMITTED);
1224
1225        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1226        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1227
1228        utControl.verify();
1229        tmControl.verify();
1230        sfControl.verify();
1231        sessionControl.verify();
1232    }
1233
1234    public void testJtaSessionSynchronizationWithPreBound() throws Exception JavaDoc {
1235        doTestJtaSessionSynchronizationWithPreBound(false);
1236    }
1237
1238    public void testJtaJtaSessionSynchronizationWithPreBoundAndFlushNever() throws Exception JavaDoc {
1239        doTestJtaSessionSynchronizationWithPreBound(true);
1240    }
1241
1242    private void doTestJtaSessionSynchronizationWithPreBound(boolean flushNever) throws Exception JavaDoc {
1243        MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
1244        TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
1245        MockJtaTransaction transaction = new MockJtaTransaction();
1246        tm.getStatus();
1247        tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
1248        tm.getTransaction();
1249        tmControl.setReturnValue(transaction, 6);
1250
1251        MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
1252        final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
1253        final MockControl sessionControl = MockControl.createControl(Session.class);
1254        final Session session = (Session) sessionControl.getMock();
1255        sf.getConnectionProvider();
1256        sfControl.setReturnValue(null, 1);
1257        sf.getTransactionManager();
1258        sfControl.setReturnValue(tm, 6);
1259        session.getFlushMode();
1260        if (flushNever) {
1261            sessionControl.setReturnValue(FlushMode.NEVER, 1);
1262            session.setFlushMode(FlushMode.AUTO);
1263            sessionControl.setVoidCallable(1);
1264        }
1265        else {
1266            sessionControl.setReturnValue(FlushMode.AUTO, 1);
1267        }
1268
1269        tmControl.replay();
1270        sfControl.replay();
1271        sessionControl.replay();
1272
1273        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1274        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
1275        try {
1276            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1277            HibernateTemplate ht = new HibernateTemplate(sf);
1278            ht.setExposeNativeSession(true);
1279            for (int i = 0; i < 5; i++) {
1280                ht.executeFind(new HibernateCallback() {
1281                    public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1282                        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1283                        assertEquals(session, sess);
1284                        return null;
1285                    }
1286                });
1287            }
1288
1289            sessionControl.verify();
1290            sessionControl.reset();
1291            session.getFlushMode();
1292            sessionControl.setReturnValue(FlushMode.AUTO, 1);
1293            session.flush();
1294            sessionControl.setVoidCallable(1);
1295            if (flushNever) {
1296                session.setFlushMode(FlushMode.NEVER);
1297                sessionControl.setVoidCallable(1);
1298            }
1299            sessionControl.replay();
1300
1301            Synchronization JavaDoc synchronization = transaction.getSynchronization();
1302            assertTrue("JTA synchronization registered", synchronization != null);
1303            synchronization.beforeCompletion();
1304            synchronization.afterCompletion(Status.STATUS_COMMITTED);
1305            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1306        }
1307        finally {
1308            TransactionSynchronizationManager.unbindResource(sf);
1309        }
1310        assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1311
1312        tmControl.verify();
1313        sfControl.verify();
1314        sessionControl.verify();
1315    }
1316
1317    public void testJtaSessionSynchronizationWithRemoteTransaction() throws Exception JavaDoc {
1318        MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
1319        TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
1320        MockJtaTransaction transaction = new MockJtaTransaction();
1321
1322        MockControl sfControl = MockControl.createControl(SessionFactoryImplementor.class);
1323        final SessionFactoryImplementor sf = (SessionFactoryImplementor) sfControl.getMock();
1324        final MockControl sessionControl = MockControl.createControl(Session.class);
1325        final Session session = (Session) sessionControl.getMock();
1326
1327        for (int j = 0; j < 2; j++) {
1328            tmControl.reset();
1329            sfControl.reset();
1330            sessionControl.reset();
1331
1332            tm.getStatus();
1333            tmControl.setReturnValue(Status.STATUS_ACTIVE, 6);
1334            tm.getTransaction();
1335            tmControl.setReturnValue(transaction, 6);
1336
1337            sf.getConnectionProvider();
1338            sfControl.setReturnValue(null, 1);
1339            sf.openSession();
1340            sfControl.setReturnValue(session, 1);
1341            sf.getTransactionManager();
1342            sfControl.setReturnValue(tm, 6);
1343            session.getFlushMode();
1344            sessionControl.setReturnValue(FlushMode.AUTO, 1);
1345            session.flush();
1346            sessionControl.setVoidCallable(1);
1347            session.close();
1348            sessionControl.setReturnValue(null, 1);
1349
1350            tmControl.replay();
1351            sfControl.replay();
1352            sessionControl.replay();
1353
1354            if (j == 0) {
1355                assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
1356            }
1357            else {
1358                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1359            }
1360
1361            HibernateTemplate ht = new HibernateTemplate(sf);
1362            ht.setExposeNativeSession(true);
1363            for (int i = 0; i < 5; i++) {
1364                ht.executeFind(new HibernateCallback() {
1365                    public Object JavaDoc doInHibernate(org.hibernate.Session sess) {
1366                        assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1367                        assertEquals(session, sess);
1368                        return null;
1369                    }
1370                });
1371            }
1372
1373            final Synchronization JavaDoc synchronization = transaction.getSynchronization();
1374            assertTrue("JTA synchronization registered", synchronization != null);
1375
1376            // Call synchronization in a new thread, to simulate a synchronization
1377
// triggered by a new remote call from a remote transaction coordinator.
1378
Thread JavaDoc synch = new Thread JavaDoc() {
1379                public void run() {
1380                    synchronization.beforeCompletion();
1381                    synchronization.afterCompletion(Status.STATUS_COMMITTED);
1382                }
1383            };
1384            synch.start();
1385            synch.join();
1386
1387            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
1388            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
1389            assertTrue("Thread session holder empty", sessionHolder.isEmpty());
1390            assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1391
1392            tmControl.verify();
1393            sfControl.verify();
1394            sessionControl.verify();
1395        }
1396
1397        TransactionSynchronizationManager.unbindResource(sf);
1398    }
1399
1400    protected void tearDown() {
1401        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
1402        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
1403        assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
1404        assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
1405    }
1406
1407}
1408
Popular Tags