001: /*
002:
003: Derby - Class org.apache.derby.jdbc.EmbeddedXADataSource
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.jdbc;
023:
024: import org.apache.derby.iapi.reference.MessageId;
025: import org.apache.derby.iapi.services.i18n.MessageService;
026:
027: import org.apache.derby.iapi.services.monitor.Monitor;
028: import org.apache.derby.iapi.jdbc.ResourceAdapter;
029: import org.apache.derby.iapi.db.Database;
030:
031: import org.apache.derby.iapi.reference.Property;
032:
033: import org.apache.derby.iapi.error.ExceptionSeverity;
034:
035: import java.sql.SQLException;
036: import java.sql.Connection;
037: import java.sql.Driver;
038:
039: /** -- jdbc 2.0. extension -- */
040: import javax.sql.XADataSource;
041: import javax.sql.XAConnection;
042:
043: /**
044:
045: EmbeddedXADataSource is Derby's XADataSource implementation for JDBC3.0 and JDBC2.0.
046:
047:
048: <P>An XADataSource is a factory for XAConnection objects. It represents a
049: RM in a DTP environment. An object that implements the XADataSource
050: interface is typically registered with a JNDI service provider.
051: <P>
052: EmbeddedXADataSource automatically supports the correct JDBC specification version
053: for the Java Virtual Machine's environment.
054: <UL>
055: <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
056: <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
057: </UL>
058:
059: <P>EmbeddedXADataSource object only works on a local database. There is no
060: client/server support. An EmbeddedXADataSource object must live in the same jvm as
061: the database.
062:
063: <P>EmbeddedXADataSource is serializable and referenceable.
064:
065: <P>See EmbeddedDataSource for DataSource properties.
066:
067: */
068: public class EmbeddedXADataSource extends EmbeddedDataSource implements
069: javax.sql.XADataSource {
070:
071: private static final long serialVersionUID = -5715798975598379738L;
072:
073: // link to the database
074: transient private ResourceAdapter ra;
075:
076: /**
077: no-arg constructor
078: */
079: public EmbeddedXADataSource() {
080: super ();
081: }
082:
083: /*
084: * XADataSource methods
085: */
086:
087: /**
088: * Attempt to establish a database connection.
089: *
090: * @return a Connection to the database
091: * @exception SQLException if a database-access error occurs.
092: */
093: public final XAConnection getXAConnection() throws SQLException {
094: if (ra == null || !ra.isActive())
095: setupResourceAdapter(null, null, false);
096:
097: return createXAConnection(ra, getUser(), getPassword(), false);
098: }
099:
100: /**
101: * Attempt to establish a database connection with the given user
102: * name and password.
103: *
104: * @param user the database user on whose behalf the Connection is being made
105: * @param password the user's password
106: * @return a Connection to the database
107: * @exception SQLException if a database-access error occurs.
108: */
109: public final XAConnection getXAConnection(String user,
110: String password) throws SQLException {
111: if (ra == null || !ra.isActive())
112: setupResourceAdapter(user, password, true);
113: return createXAConnection(ra, user, password, true);
114: }
115:
116: /*
117: * private method
118: */
119:
120: void update() {
121: ra = null;
122: super .update();
123: }
124:
125: private void setupResourceAdapter(String user, String password,
126: boolean requestPassword) throws SQLException {
127: synchronized (this ) {
128: if (ra == null || !ra.isActive()) {
129: // If it is inactive, it is useless.
130: ra = null;
131:
132: String dbName = getDatabaseName();
133: if (dbName != null) {
134:
135: // see if database already booted, if it is, then don't make a
136: // connection.
137: Database database = null;
138:
139: // if monitor is never setup by any ModuleControl, getMonitor
140: // returns null and no cloudscape database has been booted.
141: if (Monitor.getMonitor() != null)
142: database = (Database) Monitor.findService(
143: Property.DATABASE_MODULE, dbName);
144:
145: if (database == null) {
146: // If database is not found, try connecting to it. This
147: // boots and/or creates the database. If database cannot
148: // be found, this throws SQLException.
149: if (requestPassword)
150: getConnection(user, password).close();
151: else
152: getConnection().close();
153:
154: // now try to find it again
155: database = (Database) Monitor.findService(
156: Property.DATABASE_MODULE, dbName);
157: }
158:
159: if (database != null)
160: ra = (ResourceAdapter) database
161: .getResourceAdapter();
162: }
163:
164: if (ra == null)
165: throw new SQLException(
166: MessageService
167: .getTextMessage(MessageId.CORE_DATABASE_NOT_AVAILABLE),
168: "08006",
169: ExceptionSeverity.DATABASE_SEVERITY);
170:
171: // If database is already up, we need to set up driver
172: // seperately.
173: findDriver();
174:
175: if (driver == null)
176: throw new SQLException(
177: MessageService
178: .getTextMessage(MessageId.CORE_DRIVER_NOT_AVAILABLE),
179: "08006",
180: ExceptionSeverity.DATABASE_SEVERITY);
181:
182: }
183: }
184: }
185:
186: /**
187: * Intantiate and returns EmbedXAConnection.
188: * @param user
189: * @param password
190: * @return XAConnection
191: */
192: protected XAConnection createXAConnection(ResourceAdapter ra,
193: String user, String password, boolean requestPassword)
194: throws SQLException {
195: return new EmbedXAConnection(this, ra, user, password,
196: requestPassword);
197: }
198: }
|