KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DistDatabaseHelper


1 /*
2  * @(#) DistDatabaseHelper.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: DistDatabaseHelper.java,v 1.1 2004/01/30 23:11:09 tonyortiz Exp $
41  * --------------------------------------------------------------------------
42  */

43
44 import java.sql.Connection JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 import javax.naming.InitialContext JavaDoc;
49 import javax.sql.XAConnection JavaDoc;
50 import javax.sql.XADataSource JavaDoc;
51 import org.objectweb.jotm.Jotm;
52 import org.objectweb.transaction.jta.TMService;
53
54 import org.enhydra.jdbc.standard.StandardXADataSource;
55
56
57 /**
58  * @author jmesnil
59  * updated tortiz
60  *
61  */

62 public class DistDatabaseHelper {
63
64     private String JavaDoc password1;
65     private String JavaDoc password2;
66     private TMService jotm;
67     private XADataSource JavaDoc xads1;
68     private XADataSource JavaDoc xads2;
69     private String JavaDoc login1;
70     private String JavaDoc login2;
71     private static final String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
72
73     /**
74      * Constructor for DistDatabaseHelper.
75      */

76     public DistDatabaseHelper(String JavaDoc database1, String JavaDoc database2) {
77         Properties JavaDoc props1 = new Properties JavaDoc();
78         Properties JavaDoc props2 = new Properties JavaDoc();
79
80         try {
81             props1.load(ClassLoader.getSystemResourceAsStream(database1 + ".properties"));
82         } catch (Exception JavaDoc e) {
83             System.out.println("no properties file found found for " + database1);
84             System.exit(1);
85         }
86
87         System.out.println("\n" + database1 + " configuration:");
88         props1.list(System.out);
89         System.out.println("------------------------\n");
90
91         login1 = props1.getProperty("login");
92         password1 = props1.getProperty("password");
93
94         try {
95             props2.load(ClassLoader.getSystemResourceAsStream(database2 + ".properties"));
96         } catch (Exception JavaDoc e) {
97             System.out.println("no properties file found found for " + database2);
98             System.exit(1);
99         }
100
101         System.out.println("\n" + database2 + " configuration:");
102         props2.list(System.out);
103         System.out.println("------------------------\n");
104
105         login2 = props2.getProperty("login");
106         password2 = props2.getProperty("password");
107
108         // Get a transction manager
109
try {
110             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
111
jotm = new Jotm(true, false);
112             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
113             ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
114         } catch (Exception JavaDoc e) {
115             e.printStackTrace();
116             System.exit(1);
117         }
118
119         xads1 = new StandardXADataSource();
120         try {
121             ((StandardXADataSource) xads1).setDriverName(props1.getProperty("driver"));
122             ((StandardXADataSource) xads1).setUrl(props1.getProperty("url"));
123             ((StandardXADataSource) xads1).setTransactionManager(jotm.getTransactionManager());
124         } catch (Exception JavaDoc e) {
125             e.printStackTrace();
126             System.exit(1);
127         }
128
129         xads2 = new StandardXADataSource();
130         try {
131             ((StandardXADataSource) xads2).setDriverName(props2.getProperty("driver"));
132             ((StandardXADataSource) xads2).setUrl(props2.getProperty("url"));
133             ((StandardXADataSource) xads2).setTransactionManager(jotm.getTransactionManager());
134         } catch (Exception JavaDoc e) {
135             e.printStackTrace();
136             System.exit(1);
137         }
138     }
139
140     public Connection JavaDoc getConnection1() throws SQLException JavaDoc {
141         XAConnection JavaDoc xaconn = xads1.getXAConnection(login1, password1);
142         return xaconn.getConnection();
143     }
144
145     public Connection JavaDoc getConnection2() throws SQLException JavaDoc {
146         XAConnection JavaDoc xaconn = xads2.getXAConnection(login2, password2);
147         return xaconn.getConnection();
148     }
149
150     /**
151      * Method stop.
152      */

153     public void stop() {
154         xads1 = null;
155         xads2 = null;
156
157         try {
158            InitialContext JavaDoc ictx = new InitialContext JavaDoc();
159            ictx.unbind(USER_TRANSACTION_JNDI_NAME);
160         } catch (Exception JavaDoc e) {
161             e.printStackTrace();
162         }
163
164         jotm.stop();
165         jotm = null;
166     }
167
168 }
169
170
Popular Tags