KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > TopLinkJtaTransactionTests


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.toplink;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.transaction.Status JavaDoc;
23 import javax.transaction.TransactionManager JavaDoc;
24 import javax.transaction.UserTransaction JavaDoc;
25
26 import junit.framework.TestCase;
27 import oracle.toplink.exceptions.TopLinkException;
28 import oracle.toplink.sessions.Session;
29 import org.easymock.MockControl;
30
31 import org.springframework.transaction.MockJtaTransaction;
32 import org.springframework.transaction.TransactionDefinition;
33 import org.springframework.transaction.TransactionStatus;
34 import org.springframework.transaction.jta.JtaTransactionManager;
35 import org.springframework.transaction.support.TransactionCallback;
36 import org.springframework.transaction.support.TransactionSynchronizationManager;
37 import org.springframework.transaction.support.TransactionTemplate;
38
39 /**
40  * @author Juergen Hoeller
41  * @author <a HREF="mailto:james.x.clark@oracle.com">James Clark</a>
42  * @since 28.04.2005
43  */

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

1225    protected void tearDown() {
1226        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
1227        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
1228    }
1229
1230}
1231
Popular Tags