KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > store > T_XA


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_XA
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.store;
23
24 import org.apache.derbyTesting.unitTests.harness.T_Generic;
25 import org.apache.derbyTesting.unitTests.harness.T_Fail;
26
27 import org.apache.derby.iapi.store.access.xa.*;
28 import org.apache.derby.iapi.store.access.*;
29
30 import org.apache.derby.iapi.services.io.FormatableBitSet;
31
32 import org.apache.derby.iapi.reference.Property;
33
34 import org.apache.derby.iapi.services.context.ContextService;
35 import org.apache.derby.iapi.services.context.ContextManager;
36
37 import org.apache.derby.iapi.services.monitor.Monitor;
38
39 import org.apache.derby.iapi.services.sanity.SanityManager;
40
41 import org.apache.derby.iapi.services.io.FormatIdUtil;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import java.util.Properties JavaDoc;
46
47 import javax.transaction.xa.XAResource JavaDoc;
48 import javax.transaction.xa.Xid JavaDoc;
49
50
51 public class T_XA extends T_Generic
52 {
53     private static final String JavaDoc testService = "XaTest";
54
55     byte[] global_id =
56         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
57          10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
58          20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
59          30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
60          40, 41, 42, 44, 44, 45, 46, 47, 48, 49,
61          50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
62          60, 61, 62, 63};
63
64     byte[] branch_id =
65         { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
66          10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
67          20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
68          30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
69          40, 41, 42, 44, 44, 45, 46, 47, 48, 49,
70          50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
71          60, 61, 62, 63};
72
73     AccessFactory store = null;
74
75     public T_XA()
76     {
77         super();
78     }
79
80     /*
81     ** Methods of UnitTest.
82     */

83
84     /*
85     ** Methods required by T_Generic
86     */

87
88     public String JavaDoc getModuleToTestProtocolName()
89     {
90         return AccessFactory.MODULE;
91     }
92
93     /**
94         @exception T_Fail Unexpected behaviour from the API
95      */

96
97     protected void runTests() throws T_Fail
98     {
99         // Create a AccessFactory to test.
100

101         // don't automatic boot this service if it gets left around
102
if (startParams == null)
103         {
104             startParams = new Properties JavaDoc();
105         }
106         startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
107         // remove the service directory to ensure a clean run
108
startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString());
109
110         // see if we are testing encryption
111
startParams = T_Util.setEncryptionParam(startParams);
112
113         try {
114             store = (AccessFactory) Monitor.createPersistentService(
115                 getModuleToTestProtocolName(), testService, startParams);
116         } catch (StandardException mse) {
117             throw T_Fail.exceptionFail(mse);
118         }
119
120
121         if (store == null)
122         {
123             throw T_Fail.testFailMsg(
124                 getModuleToTestProtocolName() + " service not started.");
125         }
126
127         REPORT("(unitTestMain) Testing " + testService);
128
129         try {
130
131             XATest_1(new commit_method(store, true));
132             XATest_2(new commit_method(store, true));
133             XATest_3(new commit_method(store, true));
134             XATest_4(new commit_method(store, true));
135             XATest_5(new commit_method(store, true));
136             XATest_6(new commit_method(store, true));
137
138             XATest_1(new commit_method(store, false));
139             XATest_2(new commit_method(store, false));
140             XATest_3(new commit_method(store, false));
141             XATest_4(new commit_method(store, false));
142             XATest_5(new commit_method(store, false));
143             XATest_6(new commit_method(store, false));
144         }
145         catch (StandardException e)
146         {
147             String JavaDoc msg = e.getMessage();
148             if (msg == null)
149                 msg = e.getClass().getName();
150             REPORT(msg);
151             e.printStackTrace();
152             throw T_Fail.exceptionFail(e);
153         }
154         catch (Throwable JavaDoc t)
155         {
156             t.printStackTrace();
157         }
158     }
159
160     /**************************************************************************
161      * Utility methods.
162      **************************************************************************
163      */

164
165     /**************************************************************************
166      * Test Cases.
167      **************************************************************************
168      */

169
170     /**
171      * one phase commit xa transaction.
172      * <p>
173      * @exception StandardException Standard exception policy.
174      **/

175     void XATest_1(
176     commit_method commit_method)
177         throws StandardException, T_Fail
178     {
179         REPORT("(XATest_1) starting");
180
181         ContextManager cm =
182                 ContextService.getFactory().getCurrentContextManager();
183
184         // COMMIT AN IDLE TRANSACTION.
185

186         // Start a global transaction
187
XATransactionController xa_tc = (XATransactionController)
188             store.startXATransaction(
189                 cm,
190                 42, // fake format id
191
global_id,
192                 branch_id);
193
194         // commit an idle transaction - using onePhase optimization.
195
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
196
197         // done with this xact.
198
xa_tc.destroy();
199
200         // COMMIT AN UPDATE ONLY TRANSACTION.
201

202         // Start a global transaction
203
xa_tc = (XATransactionController)
204             store.startXATransaction(
205                 cm,
206                 42, // fake format id
207
global_id,
208                 branch_id);
209
210         // Create a heap conglomerate.
211
T_AccessRow template_row = new T_AccessRow(1);
212         long conglomid =
213             xa_tc.createConglomerate(
214                 "heap", // create a heap conglomerate
215
template_row.getRowArray(), // 1 column template.
216
null, //column sort order - not required for heap
217
null, // default properties
218
TransactionController.IS_DEFAULT); // not temporary
219

220
221         // commit an idle transaction - using onePhase optimization.
222
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
223
224         // done with this xact.
225
xa_tc.destroy();
226
227         // COMMIT A READ ONLY TRANSACTION.
228

229         // Start a global transaction
230
xa_tc = (XATransactionController)
231             store.startXATransaction(
232                 cm,
233                 42, // fake format id
234
global_id,
235                 branch_id);
236
237         // Open a scan on the conglomerate.
238
ScanController scan1 = xa_tc.openScan(
239             conglomid,
240             false, // don't hold
241
0, // not for update
242
TransactionController.MODE_RECORD,
243             TransactionController.ISOLATION_SERIALIZABLE,
244             (FormatableBitSet) null, // all columns, all as objects
245
null, // start position - first row in conglomerate
246
0, // unused if start position is null.
247
null, // qualifier - accept all rows
248
null, // stop position - last row in conglomerate
249
0); // unused if stop position is null.
250

251         scan1.next();
252         scan1.close();
253
254         // commit an idle transaction - using onePhase optimization.
255
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
256
257         // done with this xact.
258
xa_tc.destroy();
259
260         REPORT("(XATest_1) finishing");
261     }
262
263     /**
264      * simple two phase commit xa transaction.
265      * <p>
266      * @exception StandardException Standard exception policy.
267      **/

268     void XATest_2(
269     commit_method commit_method)
270         throws StandardException, T_Fail
271     {
272         REPORT("(XATest_2) starting");
273         ContextManager cm =
274                 ContextService.getFactory().getCurrentContextManager();
275
276         // COMMIT AN IDLE TRANSACTION.
277

278         // Start a global transaction
279
XATransactionController xa_tc = (XATransactionController)
280             store.startXATransaction(
281                 cm,
282                 42, // fake format id
283
global_id,
284                 branch_id);
285
286         if (!xa_tc.isGlobal())
287         {
288             throw T_Fail.testFailMsg("should be a global transaction.");
289         }
290
291         // This prepare will commit the idle transaction.
292
if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY)
293         {
294             throw T_Fail.testFailMsg(
295                 "prepare of idle xact did not return XA_RDONLY.");
296         }
297
298         // commit an idle transaction - using onePhase optimization.
299
try
300         {
301             // this should fail as the xact has been committed, so committing
302
// it in 2 phase mode should fail. This test can't be run in
303
// offline mode, no transaction will be found. Pass null as
304
// global_id to make that test not run.
305

306             commit_method.commit(false, 42, null, null, xa_tc);
307
308             throw T_Fail.testFailMsg(
309                 "A XA_RDONLY prepare-committed xact cant be 2P xa_committed.");
310         }
311         catch (StandardException se)
312         {
313             // expected exception - drop through.
314
}
315
316         // should not be able to find this global xact, it has been committed
317
if (((XAResourceManager) store.getXAResourceManager()).find(
318                 new XAXactId(42, global_id, branch_id)) != null)
319         {
320             throw T_Fail.testFailMsg(
321                 "A XA_RDONLY prepare-committed xact should not be findable.");
322         }
323
324         // done with this xact.
325
xa_tc.destroy();
326
327         // COMMIT AN UPDATE ONLY TRANSACTION.
328

329         // Start a global transaction
330
xa_tc = (XATransactionController)
331             store.startXATransaction(
332                 cm,
333                 42, // fake format id
334
global_id,
335                 branch_id);
336
337         // Create a heap conglomerate.
338
T_AccessRow template_row = new T_AccessRow(1);
339         long conglomid =
340             xa_tc.createConglomerate(
341                 "heap", // create a heap conglomerate
342
template_row.getRowArray(), // 1 column template.
343
null, //column sort order - not required for heap
344
null, // default properties
345
TransactionController.IS_DEFAULT); // not temporary
346

347         // prepare the update xact.
348
if (xa_tc.xa_prepare() != XATransactionController.XA_OK)
349         {
350             throw T_Fail.testFailMsg(
351                 "prepare of update xact did not return XA_OK.");
352         }
353
354         // commit an idle transaction - using onePhase optimization.
355
commit_method.commit(false, 42, global_id, branch_id, xa_tc);
356
357         // done with this xact.
358
xa_tc.destroy();
359
360         // COMMIT A READ ONLY TRANSACTION.
361

362         // Start a global transaction
363
xa_tc = (XATransactionController)
364             store.startXATransaction(
365                 cm,
366                 42, // fake format id
367
global_id,
368                 branch_id);
369
370         // Open a scan on the conglomerate.
371
ScanController scan1 = xa_tc.openScan(
372             conglomid,
373             false, // don't hold
374
0, // not for update
375
TransactionController.MODE_RECORD,
376             TransactionController.ISOLATION_SERIALIZABLE,
377             (FormatableBitSet) null, // all columns, all as objects
378
null, // start position - first row in conglomerate
379
0, // unused if start position is null.
380
null, // qualifier - accept all rows
381
null, // stop position - last row in conglomerate
382
0); // unused if stop position is null.
383

384         scan1.next();
385         scan1.close();
386
387         // This prepare will commit the idle transaction.
388
if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY)
389         {
390             throw T_Fail.testFailMsg(
391                 "prepare of idle xact did not return XA_RDONLY.");
392         }
393
394         // commit an idle transaction - using onePhase optimization.
395
try
396         {
397             // this should fail as the xact has been committed, so committing
398
// it in 2 phase mode should fail. This test can't be run in
399
// offline mode, no transaction will be found. Pass null as
400
// global_id to make that test not run.
401

402             commit_method.commit(false, 42, null, null, xa_tc);
403
404             throw T_Fail.testFailMsg(
405                 "A XA_RDONLY prepare-committed xact cant be 2P xa_committed.");
406         }
407         catch (StandardException se)
408         {
409             // expected exception - drop through.
410
}
411
412         // should not be able to find this global xact, it has been committed
413
if (((XAResourceManager) store.getXAResourceManager()).find(
414                 new XAXactId(42, global_id, branch_id)) != null)
415         {
416             throw T_Fail.testFailMsg(
417                 "A XA_RDONLY prepare-committed xact should not be findable.");
418         }
419
420         // done with this xact.
421
xa_tc.destroy();
422
423         REPORT("(XATest_2) finishing");
424     }
425
426     /**
427      * Test aborts of unprepared xa transaction.
428      * <p>
429      * @exception StandardException Standard exception policy.
430      **/

431     void XATest_3(
432     commit_method commit_method)
433         throws StandardException, T_Fail
434     {
435         REPORT("(XATest_3) starting");
436
437         ContextManager cm =
438                 ContextService.getFactory().getCurrentContextManager();
439
440         // ABORT AN IDLE TRANSACTION.
441

442         // Start a global transaction
443
XATransactionController xa_tc = (XATransactionController)
444             store.startXATransaction(
445                 cm,
446                 42, // fake format id
447
global_id,
448                 branch_id);
449
450         // commit an idle transaction - using onePhase optimization.
451
commit_method.rollback(42, global_id, branch_id, xa_tc);
452
453         // done with this xact.
454
xa_tc.destroy();
455
456         // ABORT AN UPDATE ONLY TRANSACTION.
457

458         // Start a global transaction
459
xa_tc = (XATransactionController)
460             store.startXATransaction(
461                 cm,
462                 42, // fake format id
463
global_id,
464                 branch_id);
465
466         // Create a heap conglomerate.
467
T_AccessRow template_row = new T_AccessRow(1);
468         long conglomid =
469             xa_tc.createConglomerate(
470                 "heap", // create a heap conglomerate
471
template_row.getRowArray(), // 1 column template.
472
null, //column sort order - not required for heap
473
null, // default properties
474
TransactionController.IS_DEFAULT); // not temporary
475

476
477         // commit an idle transaction - using onePhase optimization.
478
commit_method.rollback(42, global_id, branch_id, xa_tc);
479
480         // done with this xact.
481
xa_tc.destroy();
482
483         // ABORT A READ ONLY TRANSACTION.
484

485         // Start a global transaction
486
xa_tc = (XATransactionController)
487             store.startXATransaction(
488                 cm,
489                 42, // fake format id
490
global_id,
491                 branch_id);
492
493         // Create a heap conglomerate.
494
template_row = new T_AccessRow(1);
495         conglomid =
496             xa_tc.createConglomerate(
497                 "heap", // create a heap conglomerate
498
template_row.getRowArray(), // 1 column template.
499
null, //column sort order - not required for heap
500
null, // default properties
501
TransactionController.IS_DEFAULT); // not temporary
502

503
504         // commit an idle transaction - using onePhase optimization.
505
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
506
507         // done with this xact.
508
xa_tc.destroy();
509
510         // Start a global transaction
511
xa_tc = (XATransactionController)
512             store.startXATransaction(
513                 cm,
514                 42, // fake format id
515
global_id,
516                 branch_id);
517
518         // Open a scan on the conglomerate.
519
ScanController scan1 = xa_tc.openScan(
520             conglomid,
521             false, // don't hold
522
0, // not for update
523
TransactionController.MODE_RECORD,
524             TransactionController.ISOLATION_SERIALIZABLE,
525             (FormatableBitSet) null, // all columns, all as objects
526
null, // start position - first row in conglomerate
527
0, // unused if start position is null.
528
null, // qualifier - accept all rows
529
null, // stop position - last row in conglomerate
530
0); // unused if stop position is null.
531

532         scan1.next();
533         scan1.close();
534
535         // commit an idle transaction - using onePhase optimization.
536
commit_method.rollback(42, global_id, branch_id, xa_tc);
537
538         // done with this xact.
539
xa_tc.destroy();
540
541         REPORT("(XATest_3) finishing");
542     }
543
544     /**
545      * Test aborts of prepared two phase commit xa transaction.
546      * <p>
547      * @exception StandardException Standard exception policy.
548      **/

549     void XATest_4(
550     commit_method commit_method)
551         throws StandardException, T_Fail
552     {
553         REPORT("(XATest_4) starting");
554
555         ContextManager cm =
556                 ContextService.getFactory().getCurrentContextManager();
557
558         // ABORT AN IDLE TRANSACTION.
559

560         // Start a global transaction
561
XATransactionController xa_tc = (XATransactionController)
562             store.startXATransaction(
563                 cm,
564                 42, // fake format id
565
global_id,
566                 branch_id);
567
568         // This prepare will commit the idle transaction.
569
if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY)
570         {
571             throw T_Fail.testFailMsg(
572                 "prepare of idle xact did not return XA_RDONLY.");
573         }
574
575         // nothing to do, will just abort the next current idle xact.
576

577        // after prepare/readonly we cna continue to use transaction
578
commit_method.commit(true, 42, null, null, xa_tc);
579
580
581
582         // should not be able to find this global xact, it has been committed
583
if (((XAResourceManager) store.getXAResourceManager()).find(
584                 new XAXactId(42, global_id, branch_id)) != null)
585         {
586             throw T_Fail.testFailMsg(
587                 "A XA_RDONLY prepare-committed xact should not be findable.");
588         }
589
590         // done with this xact.
591
xa_tc.destroy();
592
593         // ABORT AN UPDATE ONLY TRANSACTION.
594

595         // Start a global transaction
596
xa_tc = (XATransactionController)
597             store.startXATransaction(
598                 cm,
599                 42, // fake format id
600
global_id,
601                 branch_id);
602
603         // Create a heap conglomerate.
604
T_AccessRow template_row = new T_AccessRow(1);
605         long conglomid =
606             xa_tc.createConglomerate(
607                 "heap", // create a heap conglomerate
608
template_row.getRowArray(), // 1 column template.
609
null, //column sort order - not required for heap
610
null, // default properties
611
TransactionController.IS_DEFAULT); // not temporary
612

613
614
615         // Open a scan on the conglomerate, to verify the create happened,
616
// and to show that the same openScan done after abort fails.
617
ScanController scan1 = xa_tc.openScan(
618             conglomid,
619             false, // don't hold
620
0, // not for update
621
TransactionController.MODE_RECORD,
622             TransactionController.ISOLATION_SERIALIZABLE,
623             (FormatableBitSet) null, // all columns, all as objects
624
null, // start position - first row in conglomerate
625
0, // unused if start position is null.
626
null, // qualifier - accept all rows
627
null, // stop position - last row in conglomerate
628
0); // unused if stop position is null.
629

630         scan1.next();
631         scan1.close();
632
633         // prepare the update xact.
634
if (xa_tc.xa_prepare() != XATransactionController.XA_OK)
635         {
636             throw T_Fail.testFailMsg(
637                 "prepare of update xact did not return XA_OK.");
638         }
639
640         try
641         {
642             // Open a scan on the conglomerate.
643
scan1 = xa_tc.openScan(
644                 conglomid,
645                 false, // don't hold
646
0, // not for update
647
TransactionController.MODE_RECORD,
648                 TransactionController.ISOLATION_SERIALIZABLE,
649                 (FormatableBitSet) null, // all columns, all as objects
650
null, // start position - first row in conglomerate
651
0, // unused if start position is null.
652
null, // qualifier - accept all rows
653
null, // stop position - last row in conglomerate
654
0); // unused if stop position is null.
655

656             scan1.next();
657             scan1.close();
658
659             throw T_Fail.testFailMsg(
660                 "Should not be able to do anything on xact after prepare.");
661         }
662         catch (StandardException se)
663         {
664             // expected exception, fall through.
665
}
666
667
668         // commit an idle transaction - using onePhase optimization.
669
commit_method.rollback(42, global_id, branch_id, xa_tc);
670
671         commit_method.commit(true, 42, null, null, xa_tc);
672
673         // should not be able to find this global xact, it has been committed
674
if (((XAResourceManager) store.getXAResourceManager()).find(
675                 new XAXactId(42, global_id, branch_id)) != null)
676         {
677             throw T_Fail.testFailMsg(
678                 "A xa_rollbacked xact should not be findable.");
679         }
680
681
682         // done with this xact.
683
xa_tc.destroy();
684
685         // Start a global transaction
686
xa_tc = (XATransactionController)
687             store.startXATransaction(
688                 cm,
689                 42, // fake format id
690
global_id,
691                 branch_id);
692
693         try
694         {
695             // Open a scan on the conglomerate.
696
scan1 = xa_tc.openScan(
697                 conglomid,
698                 false, // don't hold
699
0, // not for update
700
TransactionController.MODE_RECORD,
701                 TransactionController.ISOLATION_SERIALIZABLE,
702                 (FormatableBitSet) null, // all columns, all as objects
703
null, // start position - first row in conglomerate
704
0, // unused if start position is null.
705
null, // qualifier - accept all rows
706
null, // stop position - last row in conglomerate
707
0); // unused if stop position is null.
708

709             scan1.next();
710             scan1.close();
711
712             throw T_Fail.testFailMsg(
713                 "Should not be able to open conglom, the create was aborted.");
714         }
715         catch (StandardException se)
716         {
717             // expected exception, fall through.
718
}
719
720         xa_tc.destroy();
721
722
723         // ABORT A READ ONLY TRANSACTION.
724

725         // Start a global transaction
726
xa_tc = (XATransactionController)
727             store.startXATransaction(
728                 cm,
729                 42, // fake format id
730
global_id,
731                 branch_id);
732
733         // Create a heap conglomerate.
734
template_row = new T_AccessRow(1);
735         conglomid =
736             xa_tc.createConglomerate(
737                 "heap", // create a heap conglomerate
738
template_row.getRowArray(), // 1 column template.
739
null, //column sort order - not required for heap
740
null, // default properties
741
TransactionController.IS_DEFAULT); // not temporary
742

743         commit_method.commit(true, 42, global_id, branch_id, xa_tc);
744
745         xa_tc.destroy();
746
747
748         // Start a global transaction
749
xa_tc = (XATransactionController)
750             store.startXATransaction(
751                 cm,
752                 42, // fake format id
753
global_id,
754                 branch_id);
755
756         // Open a scan on the conglomerate.
757
scan1 = xa_tc.openScan(
758             conglomid,
759             false, // don't hold
760
0, // not for update
761
TransactionController.MODE_RECORD,
762             TransactionController.ISOLATION_SERIALIZABLE,
763             (FormatableBitSet) null, // all columns, all as objects
764
null, // start position - first row in conglomerate
765
0, // unused if start position is null.
766
null, // qualifier - accept all rows
767
null, // stop position - last row in conglomerate
768
0); // unused if stop position is null.
769

770         scan1.next();
771         scan1.close();
772
773         // This prepare will commit the idle transaction.
774
if (xa_tc.xa_prepare() != XATransactionController.XA_RDONLY)
775         {
776             throw T_Fail.testFailMsg(
777                 "prepare of idle xact did not return XA_RDONLY.");
778         }
779
780         // commit an idle transaction - using onePhase optimization.
781
commit_method.commit(true, 42, null, null, xa_tc);
782
783         // should not be able to find this global xact, it has been committed
784
if (((XAResourceManager) store.getXAResourceManager()).find(
785                 new XAXactId(42, global_id, branch_id)) != null)
786         {
787             throw T_Fail.testFailMsg(
788                 "A XA_RDONLY prepare-committed xact should not be findable.");
789         }
790
791         // done with this xact.
792
xa_tc.destroy();
793
794         REPORT("(XATest_5) finishing");
795     }
796
797     /**
798      * Very simple testing of the recover() call.
799      * <p>
800      * @exception StandardException Standard exception policy.
801      **/

802     void XATest_5(
803     commit_method commit_method)
804         throws StandardException, T_Fail
805     {
806         REPORT("(XATest_5) starting");
807
808         // Should be no prepared transactions when we first start.
809
if (((XAResourceManager) store.getXAResourceManager()).recover(
810                 XAResource.TMSTARTRSCAN).length != 0)
811         {
812             throw T_Fail.testFailMsg(
813                 "recover incorrectly returned prepared xacts.");
814         }
815
816         // Should be no prepared transactions when we first start.
817
if (((XAResourceManager) store.getXAResourceManager()).recover(
818                 XAResource.TMNOFLAGS).length != 0)
819         {
820             throw T_Fail.testFailMsg("NOFLAGS should always return 0.");
821         }
822
823         ContextManager cm =
824                 ContextService.getFactory().getCurrentContextManager();
825
826         // COMMIT AN IDLE TRANSACTION.
827

828         // Start a global transaction
829
XATransactionController xa_tc = (XATransactionController)
830             store.startXATransaction(
831                 cm,
832                 42, // fake format id
833
global_id,
834                 branch_id);
835
836         // Should be no prepared transactions, there is one idle global xact.
837
if (((XAResourceManager) store.getXAResourceManager()).recover(
838                 XAResource.TMSTARTRSCAN).length != 0)
839         {
840             throw T_Fail.testFailMsg(
841                 "recover incorrectly returned prepared xacts.");
842         }
843
844         // commit an idle transaction - using onePhase optimization.
845
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
846
847         // done with this xact.
848
xa_tc.destroy();
849
850         // COMMIT AN UPDATE ONLY TRANSACTION.
851

852         // Start a global transaction
853
xa_tc = (XATransactionController)
854             store.startXATransaction(
855                 cm,
856                 42, // fake format id
857
global_id,
858                 branch_id);
859
860         // Create a heap conglomerate.
861
T_AccessRow template_row = new T_AccessRow(1);
862         long conglomid =
863             xa_tc.createConglomerate(
864                 "heap", // create a heap conglomerate
865
template_row.getRowArray(), // 1 column template.
866
null, //column sort order - not required for heap
867
null, // default properties
868
TransactionController.IS_DEFAULT); // not temporary
869

870         // Should be no prepared transactions, there is one update global xact.
871
if (((XAResourceManager) store.getXAResourceManager()).recover(
872                 XAResource.TMSTARTRSCAN).length != 0)
873         {
874             throw T_Fail.testFailMsg(
875                 "recover incorrectly returned prepared xacts.");
876         }
877
878         // commit an idle transaction - using onePhase optimization.
879
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
880
881         // done with this xact.
882
xa_tc.destroy();
883
884         // COMMIT A READ ONLY TRANSACTION.
885

886         // Start a global transaction
887
xa_tc = (XATransactionController)
888             store.startXATransaction(
889                 cm,
890                 42, // fake format id
891
global_id,
892                 branch_id);
893
894         // Open a scan on the conglomerate.
895
ScanController scan1 = xa_tc.openScan(
896             conglomid,
897             false, // don't hold
898
0, // not for update
899
TransactionController.MODE_RECORD,
900             TransactionController.ISOLATION_SERIALIZABLE,
901             (FormatableBitSet) null, // all columns, all as objects
902
null, // start position - first row in conglomerate
903
0, // unused if start position is null.
904
null, // qualifier - accept all rows
905
null, // stop position - last row in conglomerate
906
0); // unused if stop position is null.
907

908         scan1.next();
909         scan1.close();
910
911         // Should be no prepared transactions, there is one update global xact.
912
if (((XAResourceManager) store.getXAResourceManager()).recover(
913                 XAResource.TMSTARTRSCAN).length != 0)
914         {
915             throw T_Fail.testFailMsg(
916                 "recover incorrectly returned prepared xacts.");
917         }
918
919
920         // commit an idle transaction - using onePhase optimization.
921
commit_method.commit(true, 42, global_id, branch_id, xa_tc);
922
923         // done with this xact.
924
xa_tc.destroy();
925
926         // PREPARE AN UPDATE TRANSACTION.
927

928         // Start a global transaction
929
xa_tc = (XATransactionController)
930             store.startXATransaction(
931                 cm,
932                 42, // fake format id
933
global_id,
934                 branch_id);
935
936         // Create a heap conglomerate.
937
template_row = new T_AccessRow(1);
938         conglomid =
939             xa_tc.createConglomerate(
940                 "heap", // create a heap conglomerate
941
template_row.getRowArray(), // 1 column template.
942
null, //column sort order - not required for heap
943
null, // default properties
944
TransactionController.IS_DEFAULT); // not temporary
945

946         // Should be no prepared transactions, there is one update global xact.
947
if (((XAResourceManager) store.getXAResourceManager()).recover(
948                 XAResource.TMSTARTRSCAN).length != 0)
949         {
950             throw T_Fail.testFailMsg(
951                 "recover incorrectly returned prepared xacts.");
952         }
953
954         // prepare the update xact.
955
if (xa_tc.xa_prepare() != XATransactionController.XA_OK)
956         {
957             throw T_Fail.testFailMsg(
958                 "prepare of update xact did not return XA_OK.");
959         }
960
961         try
962         {
963             // Open a scan on the conglomerate.
964
scan1 = xa_tc.openScan(
965                 conglomid,
966                 false, // don't hold
967
0, // not for update
968
TransactionController.MODE_RECORD,
969                 TransactionController.ISOLATION_SERIALIZABLE,
970                 (FormatableBitSet) null, // all columns, all as objects
971
null, // start position - first row in conglomerate
972
0, // unused if start position is null.
973
null, // qualifier - accept all rows
974
null, // stop position - last row in conglomerate
975
0); // unused if stop position is null.
976

977             scan1.next();
978             scan1.close();
979
980             throw T_Fail.testFailMsg(
981                 "Should not be able to do anything on xact after prepare.");
982         }
983         catch (StandardException se)
984         {
985             // expected exception, fall through.
986
}
987
988         // Should be no prepared transactions, there is one update global xact.
989
Xid JavaDoc[] prepared_xacts =
990             ((XAResourceManager) store.getXAResourceManager()).recover(
991                 XAResource.TMSTARTRSCAN);
992
993         if (prepared_xacts.length != 1)
994         {
995             throw T_Fail.testFailMsg(
996                 "recover incorrectly returned wrong prepared xacts.");
997         }
998
999         if (prepared_xacts[0].getFormatId() != 42)
1000            throw T_Fail.testFailMsg(
1001                "bad format id = " + prepared_xacts[0].getFormatId());
1002
1003        byte[] gid = prepared_xacts[0].getGlobalTransactionId();
1004
1005        if (!java.util.Arrays.equals(gid, global_id))
1006        {
1007            throw T_Fail.testFailMsg(
1008                "bad global id = " + org.apache.derbyTesting.unitTests.util.BitUtil.hexDump(gid));
1009        }
1010
1011        byte[] bid = prepared_xacts[0].getBranchQualifier();
1012
1013        if (!java.util.Arrays.equals(bid, branch_id))
1014        {
1015            throw T_Fail.testFailMsg(
1016                "bad branch id = " + org.apache.derbyTesting.unitTests.util.BitUtil.hexDump(bid));
1017        }
1018
1019        if (((XAResourceManager) store.getXAResourceManager()).recover(
1020                XAResource.TMNOFLAGS).length != 0)
1021        {
1022            throw T_Fail.testFailMsg("NOFLAGS should always return 0.");
1023        }
1024
1025        // commit a prepared transaction - using two phase.
1026
commit_method.commit(false, 42, global_id, branch_id, xa_tc);
1027
1028        // Should be no prepared transactions, there is one update global xact.
1029
if (((XAResourceManager) store.getXAResourceManager()).recover(
1030                XAResource.TMSTARTRSCAN).length != 0)
1031        {
1032            throw T_Fail.testFailMsg(
1033                "recover incorrectly returned prepared xacts.");
1034        }
1035
1036        // done with this xact.
1037
xa_tc.destroy();
1038
1039        // Should be no prepared transactions, there is one update global xact.
1040
if (((XAResourceManager) store.getXAResourceManager()).recover(
1041                XAResource.TMSTARTRSCAN).length != 0)
1042        {
1043            throw T_Fail.testFailMsg(
1044                "recover incorrectly returned prepared xacts.");
1045        }
1046
1047        REPORT("(XATest_5) finishing");
1048
1049    }
1050
1051    /**
1052     * Very simple testing of changing a local transaction to a global.
1053     * <p>
1054     * @exception StandardException Standard exception policy.
1055     **/

1056    void XATest_6(
1057    commit_method commit_method)
1058        throws StandardException, T_Fail
1059    {
1060        REPORT("(XATest_5) starting");
1061
1062        ContextManager cm =
1063                ContextService.getFactory().getCurrentContextManager();
1064
1065        TransactionController tc = store.getTransaction(cm);
1066
1067        // Create a heap conglomerate.
1068
T_AccessRow template_row = new T_AccessRow(1);
1069        long conglomid =
1070            tc.createConglomerate(
1071                "heap", // create a heap conglomerate
1072
template_row.getRowArray(), // 1 column template.
1073
null, //column sort order - not required for heap
1074
null, // default properties
1075
TransactionController.IS_DEFAULT); // not temporary
1076

1077        tc.commit();
1078
1079        // COMMIT AN IDLE TRANSACTION.
1080

1081        // Start a global transaction
1082
XATransactionController xa_tc = (XATransactionController)
1083            tc.createXATransactionFromLocalTransaction(
1084                42, // fake format id
1085
global_id,
1086                branch_id);
1087
1088        if (!xa_tc.isGlobal())
1089        {
1090            throw T_Fail.testFailMsg("should be a global transaction.");
1091        }
1092
1093
1094        // Open a scan on the conglomerate.
1095
ScanController scan1 = xa_tc.openScan(
1096            conglomid,
1097            false, // don't hold
1098
0, // not for update
1099
TransactionController.MODE_RECORD,
1100            TransactionController.ISOLATION_SERIALIZABLE,
1101            (FormatableBitSet) null, // all columns, all as objects
1102
null, // start position - first row in conglomerate
1103
0, // unused if start position is null.
1104
null, // qualifier - accept all rows
1105
null, // stop position - last row in conglomerate
1106
0); // unused if stop position is null.
1107

1108
1109        scan1.next();
1110        scan1.close();
1111
1112
1113        // Create a heap conglomerate.
1114
template_row = new T_AccessRow(1);
1115        conglomid =
1116            xa_tc.createConglomerate(
1117                "heap", // create a heap conglomerate
1118
template_row.getRowArray(), // 1 column template.
1119
null, //column sort order - not required for heap
1120
null, // default properties
1121
TransactionController.IS_DEFAULT); // not temporary
1122

1123        // Should be no prepared transactions, there is one update global xact.
1124
if (((XAResourceManager) store.getXAResourceManager()).recover(
1125                XAResource.TMSTARTRSCAN).length != 0)
1126        {
1127            throw T_Fail.testFailMsg(
1128                "recover incorrectly returned prepared xacts.");
1129        }
1130
1131        // prepare the update xact.
1132
if (xa_tc.xa_prepare() != XATransactionController.XA_OK)
1133        {
1134            throw T_Fail.testFailMsg(
1135                "prepare of update xact did not return XA_OK.");
1136        }
1137
1138        try
1139        {
1140            // Open a scan on the conglomerate.
1141
scan1 = xa_tc.openScan(
1142                conglomid,
1143                false, // don't hold
1144
0, // not for update
1145
TransactionController.MODE_RECORD,
1146                TransactionController.ISOLATION_SERIALIZABLE,
1147                (FormatableBitSet) null, // all columns, all as objects
1148
null, // start position - first row in conglomerate
1149
0, // unused if start position is null.
1150
null, // qualifier - accept all rows
1151
null, // stop position - last row in conglomerate
1152
0); // unused if stop position is null.
1153

1154            scan1.next();
1155            scan1.close();
1156
1157            throw T_Fail.testFailMsg(
1158                "Should not be able to do anything on xact after prepare.");
1159        }
1160        catch (StandardException se)
1161        {
1162            // expected exception, fall through.
1163
}
1164
1165        // commit a prepared transaction - using two phase.
1166
commit_method.commit(false, 42, global_id, branch_id, xa_tc);
1167
1168        xa_tc.destroy();
1169
1170        REPORT("(XATest_6) finishing");
1171    }
1172
1173}
1174
1175class commit_method
1176{
1177    private boolean online_xact;
1178    private AccessFactory store;
1179
1180    public commit_method(
1181    AccessFactory store,
1182    boolean online_xact)
1183    {
1184        this.store = store;
1185        this.online_xact = online_xact;
1186    }
1187
1188    public void commit(
1189    boolean one_phase,
1190    int format_id,
1191    byte[] global_id,
1192    byte[] branch_id,
1193    XATransactionController xa_tc)
1194        throws StandardException
1195    {
1196        if (SanityManager.DEBUG)
1197            SanityManager.ASSERT((global_id != null) || (xa_tc != null));
1198
1199        boolean local_online_xact = online_xact;
1200
1201        if (global_id == null)
1202            local_online_xact = true;
1203        if (xa_tc == null)
1204            local_online_xact = false;
1205            
1206        if (local_online_xact)
1207        {
1208            xa_tc.xa_commit(one_phase);
1209        }
1210        else
1211        {
1212            Xid JavaDoc xid = new XAXactId(format_id, global_id, branch_id);
1213
1214            ContextManager cm =
1215                ((XAResourceManager) store.getXAResourceManager()).find(xid);
1216
1217            if (SanityManager.DEBUG)
1218            {
1219                SanityManager.ASSERT(cm != null, "could not find xid = " + xid);
1220
1221                SanityManager.ASSERT(
1222                    cm ==
1223                    ContextService.getFactory().getCurrentContextManager(),
1224                    "cm = " + cm +
1225                    "current = " +
1226                        ContextService.getFactory().getCurrentContextManager());
1227            }
1228
1229            ((XAResourceManager) store.getXAResourceManager()).commit(
1230                cm, xid, one_phase);
1231        }
1232    }
1233
1234    public void rollback(
1235    int format_id,
1236    byte[] global_id,
1237    byte[] branch_id,
1238    XATransactionController xa_tc)
1239        throws StandardException
1240    {
1241        if (SanityManager.DEBUG)
1242            SanityManager.ASSERT((global_id != null) || (xa_tc != null));
1243
1244        boolean local_online_xact = online_xact;
1245
1246        if (global_id == null)
1247            local_online_xact = true;
1248        if (xa_tc == null)
1249            local_online_xact = false;
1250            
1251        if (local_online_xact)
1252        {
1253            xa_tc.xa_rollback();
1254        }
1255        else
1256        {
1257            Xid JavaDoc xid = new XAXactId(format_id, global_id, branch_id);
1258
1259            ContextManager cm =
1260                ((XAResourceManager) store.getXAResourceManager()).find(xid);
1261
1262            if (SanityManager.DEBUG)
1263            {
1264                SanityManager.ASSERT(cm != null, "could not find xid = " + xid);
1265
1266                SanityManager.ASSERT(
1267                    cm ==
1268                    ContextService.getFactory().getCurrentContextManager(),
1269                    "cm = " + cm +
1270                    "current = " +
1271                        ContextService.getFactory().getCurrentContextManager());
1272            }
1273
1274            ((XAResourceManager) store.getXAResourceManager()).rollback(
1275                cm, xid);
1276        }
1277    }
1278}
1279
Popular Tags