KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > EmbeddedXADataSource


1 /*
2
3    Derby - Class org.apache.derby.jdbc.EmbeddedXADataSource
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.derby.jdbc;
23
24 import org.apache.derby.iapi.reference.MessageId;
25 import org.apache.derby.iapi.services.i18n.MessageService;
26
27 import org.apache.derby.iapi.services.monitor.Monitor;
28 import org.apache.derby.iapi.jdbc.ResourceAdapter;
29 import org.apache.derby.iapi.db.Database;
30
31 import org.apache.derby.iapi.reference.Property;
32
33 import org.apache.derby.iapi.error.ExceptionSeverity;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.Driver JavaDoc;
38
39
40 /** -- jdbc 2.0. extension -- */
41 import javax.sql.XADataSource JavaDoc;
42 import javax.sql.XAConnection JavaDoc;
43
44
45 /**
46
47     EmbeddedXADataSource is Derby's XADataSource implementation for JDBC3.0 and JDBC2.0.
48     
49
50     <P>An XADataSource is a factory for XAConnection objects. It represents a
51     RM in a DTP environment. An object that implements the XADataSource
52     interface is typically registered with a JNDI service provider.
53     <P>
54     EmbeddedXADataSource automatically supports the correct JDBC specification version
55     for the Java Virtual Machine's environment.
56     <UL>
57     <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
58     <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
59     </UL>
60
61     <P>EmbeddedXADataSource object only works on a local database. There is no
62     client/server support. An EmbeddedXADataSource object must live in the same jvm as
63     the database.
64
65     <P>EmbeddedXADataSource is serializable and referenceable.
66
67     <P>See EmbeddedDataSource for DataSource properties.
68
69  */

70 public class EmbeddedXADataSource extends EmbeddedDataSource implements
71                 javax.sql.XADataSource JavaDoc
72 {
73
74     private static final long serialVersionUID = -5715798975598379738L;
75
76     // link to the database
77
transient private ResourceAdapter ra;
78   
79     /**
80       no-arg constructor
81     */

82     public EmbeddedXADataSource()
83     {
84         super();
85     }
86
87
88     /*
89      * XADataSource methods
90      */

91
92
93     /**
94      * Attempt to establish a database connection.
95      *
96      * @return a Connection to the database
97      * @exception SQLException if a database-access error occurs.
98      */

99     public final XAConnection JavaDoc getXAConnection() throws SQLException JavaDoc
100     {
101         if (ra == null || !ra.isActive())
102             setupResourceAdapter(null, null, false);
103
104         return createXAConnection (ra, getUser(), getPassword(), false);
105     }
106
107     /**
108      * Attempt to establish a database connection with the given user
109      * name and password.
110      *
111      * @param user the database user on whose behalf the Connection is being made
112      * @param password the user's password
113      * @return a Connection to the database
114      * @exception SQLException if a database-access error occurs.
115      */

116     public final XAConnection JavaDoc getXAConnection(String JavaDoc user, String JavaDoc password)
117          throws SQLException JavaDoc
118     {
119         if (ra == null || !ra.isActive())
120             setupResourceAdapter(user, password, true);
121                 return createXAConnection (ra, user, password, true);
122     }
123     
124     /*
125      * private method
126      */

127
128     void update() {
129         ra = null;
130         super.update();
131     }
132
133     private void setupResourceAdapter(String JavaDoc user, String JavaDoc password, boolean requestPassword) throws SQLException JavaDoc
134     {
135         synchronized(this)
136         {
137             if (ra == null || !ra.isActive())
138             {
139                 // If it is inactive, it is useless.
140
ra = null;
141
142                 String JavaDoc dbName = getDatabaseName();
143                 if (dbName != null) {
144
145                     // see if database already booted, if it is, then don't make a
146
// connection.
147
Database database = null;
148
149                     // if monitor is never setup by any ModuleControl, getMonitor
150
// returns null and no cloudscape database has been booted.
151
if (Monitor.getMonitor() != null)
152                         database = (Database)
153                             Monitor.findService(Property.DATABASE_MODULE, dbName);
154
155                     if (database == null)
156                     {
157                         // If database is not found, try connecting to it. This
158
// boots and/or creates the database. If database cannot
159
// be found, this throws SQLException.
160
if (requestPassword)
161                             getConnection(user, password).close();
162                         else
163                             getConnection().close();
164
165                         // now try to find it again
166
database = (Database)
167                             Monitor.findService(Property.DATABASE_MODULE, dbName);
168                     }
169
170                     if (database != null)
171                         ra = (ResourceAdapter) database.getResourceAdapter();
172                 }
173
174                 if (ra == null)
175                     throw new SQLException JavaDoc(MessageService.getTextMessage(MessageId.CORE_DATABASE_NOT_AVAILABLE),
176                                            "08006",
177                                            ExceptionSeverity.DATABASE_SEVERITY);
178
179
180                 // If database is already up, we need to set up driver
181
// seperately.
182
findDriver();
183
184                 if (driver == null)
185                     throw new SQLException JavaDoc(MessageService.getTextMessage(MessageId.CORE_DRIVER_NOT_AVAILABLE),
186                                            "08006",
187                                            ExceptionSeverity.DATABASE_SEVERITY);
188
189             }
190         }
191     }
192         
193     /**
194      * Intantiate and returns EmbedXAConnection.
195      * @param user
196      * @param password
197      * @return XAConnection
198      */

199         protected XAConnection JavaDoc createXAConnection (ResourceAdapter ra,
200                 String JavaDoc user, String JavaDoc password,
201                 boolean requestPassword) throws SQLException JavaDoc {
202             return new EmbedXAConnection(this, ra, user,
203                     password, requestPassword);
204         }
205 }
206
Popular Tags