KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BasicExample


1 /*
2  * @(#) BasicExample.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was orginally developed by
8  *
9  * - INRIA (www.inria.fr)inside the ObjectWeb Consortium
10  * (http://www.objectweb.org)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by INRIA are
14  * Copyright (c) 2002 INRIA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * $Id: BasicExample.java,v 1.2 2003/12/05 19:03:56 trentshue Exp $
41  * --------------------------------------------------------------------------
42  */

43  
44
45 import java.lang.reflect.Field JavaDoc;
46
47 import javax.naming.Context JavaDoc;
48 import javax.naming.InitialContext JavaDoc;
49 import javax.transaction.RollbackException JavaDoc;
50 import javax.transaction.Status JavaDoc;
51 import javax.transaction.UserTransaction JavaDoc;
52
53 /**
54  *
55  * a basic example of transaction.
56  *
57  * @author Christophe Ney - cney@batisseurs.com
58  */

59 public class BasicExample {
60
61     public static void main(String JavaDoc[] args) {
62         if (args.length!=1) {
63             System.out.println("usage : BasicExample [userTransactionURL]");
64             System.exit(1);
65         }
66
67
68         UserTransaction JavaDoc utc = null;
69
70         try {
71             System.out.println("create initial context");
72             Context JavaDoc ictx = new InitialContext JavaDoc();
73             System.out.println("lookup UserTransaction at : "+args[0]);
74             utc = (UserTransaction JavaDoc)ictx.lookup(args[0]);
75         } catch (Exception JavaDoc e) {
76             System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
77             System.out.println("Exception message :"+e.getMessage());
78             e.printStackTrace();
79             System.exit(1);
80         }
81         
82         System.out.println();
83
84         //a simple transaction
85
try {
86             System.out.println("a simple transaction which is committed:");
87             System.out.println("\t- initial status : "+getStatusName(utc.getStatus()));
88             utc.begin();
89             System.out.println("\t- after begin status : "+getStatusName(utc.getStatus()));
90             utc.commit();
91             System.out.println("\t- after commit status : "+getStatusName(utc.getStatus()));
92         } catch (Exception JavaDoc e) {
93             System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
94             System.out.println("Exception message :"+e.getMessage());
95             e.printStackTrace();
96             System.exit(1);
97         }
98
99         System.out.println();
100         // a transaction with a rollback
101
try {
102             System.out.println("a simple transaction which is rolled back.");
103             System.out.println("we set a transaction timeout to 1 second, begin the transaction, "+
104                                "and wait 5 seconds before committing it:");
105             utc.setTransactionTimeout(1);
106             System.out.println("\t- initial status : "+getStatusName(utc.getStatus()));
107             utc.begin();
108             System.out.println("\t- after begin status : "+getStatusName(utc.getStatus()));
109             System.out.println("\t- wait for 5 seconds");
110             Thread.sleep(5*1000);
111             utc.commit();
112             System.out.println("ERROR: the commit method should have failed due to timeout expiration");
113             System.exit(1);
114         } catch (RollbackException JavaDoc e) {
115             try {
116                 System.out.println("\t- after rollback status : "+ getStatusName(utc.getStatus()));
117             } catch (Exception JavaDoc ex) {
118                 System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
119                 System.out.println("Exception message :"+e.getMessage());
120                 e.printStackTrace();
121                 System.exit(1);
122             }
123         } catch (Exception JavaDoc e) {
124             System.out.println("Exception of type :"+e.getClass().getName()+" has been thrown");
125             System.out.println("Exception message :"+e.getMessage());
126             e.printStackTrace();
127             System.exit(1);
128         }
129
130         System.out.println("\nBasic example is OK.");
131     }
132
133     public static String JavaDoc getStatusName(int status) {
134         String JavaDoc statusName = null;
135         try {
136             Field JavaDoc[] flds = Status JavaDoc.class.getDeclaredFields();
137             for (int i=0; i<flds.length; i++) {
138                 if (flds[i].getInt(null) == status)
139                     statusName = flds[i].getName();
140             }
141         } catch (Exception JavaDoc e) {
142             statusName = "invalid status value!";
143         }
144         return statusName;
145     }
146 }
147
Popular Tags