001: /*
002:
003: Derby - Class org.apache.derby.impl.drda.XADatabase.java
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: /**
023: * This class contains database state specific to XA,
024: * specifically the XAResource that will be used for XA commands.
025: * @author kmarsden@Sourcery.Org
026: */package org.apache.derby.impl.drda;
027:
028: import java.sql.Connection;
029: import java.sql.SQLException;
030:
031: import javax.transaction.xa.XAResource;
032: import javax.sql.XADataSource;
033: import javax.sql.XAConnection;
034:
035: import java.util.Hashtable;
036: import java.util.Properties;
037: import java.util.Enumeration;
038:
039: import org.apache.derby.jdbc.EmbeddedXADataSource;
040: import org.apache.derby.impl.drda.DRDAXid;
041: import org.apache.derby.iapi.jdbc.BrokeredConnection;
042: import org.apache.derby.iapi.jdbc.EngineConnection;
043:
044: class XADatabase extends Database {
045:
046: // XA Datasource used by all the XA connection requests
047: private EmbeddedXADataSource xaDataSource;
048:
049: private XAResource xaResource;
050: private XAConnection xaConnection;
051:
052: XADatabase(String dbName) {
053: super (dbName);
054: forXA = true;
055: }
056:
057: /**
058: * Make a new connection using the database name and set
059: * the connection in the database
060: **/
061: synchronized void makeConnection(Properties p) throws SQLException {
062: if (xaDataSource == null) {
063: xaDataSource = new EmbeddedXADataSource();
064: }
065:
066: xaDataSource.setDatabaseName(shortDbName);
067: appendAttrString(p);
068: if (attrString != null)
069: xaDataSource.setConnectionAttributes(attrString);
070:
071: EngineConnection conn = getConnection();
072: // If we have no existing connection. this is a brand new XAConnection.
073: if (conn == null) {
074: xaConnection = xaDataSource.getXAConnection(userId,
075: password);
076: setXAResource(xaConnection.getXAResource());
077: } else // this is just a connection reset. Close the logical connection.
078: {
079: conn.close();
080: }
081:
082: // Get a new logical connection.
083: // Contract between network server and embedded engine
084: // is that any connection returned implements EngineConnection.
085: conn = (EngineConnection) xaConnection.getConnection();
086: // Client will always drive the commits so connection should
087: // always be autocommit false on the server. DERBY-898/DERBY-899
088: conn.setAutoCommit(false);
089: setConnection(conn);
090: }
091:
092: /** SetXAResource
093: * @param resource XAResource for this connection
094: */
095: protected void setXAResource(XAResource resource) {
096: this .xaResource = resource;
097: }
098:
099: /**
100: * get XA Resource for this connection
101: */
102: protected XAResource getXAResource() {
103: return this.xaResource;
104: }
105:
106: }
|