KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > XADatabase


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.XADatabase.java
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 /**
23  * This class contains database state specific to XA,
24  * specifically the XAResource that will be used for XA commands.
25  * @author kmarsden@Sourcery.Org
26  */

27
28 package org.apache.derby.impl.drda;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 import javax.transaction.xa.XAResource JavaDoc;
34 import javax.sql.XADataSource JavaDoc;
35 import javax.sql.XAConnection JavaDoc;
36
37 import java.util.Hashtable JavaDoc;
38 import java.util.Properties JavaDoc;
39 import java.util.Enumeration JavaDoc;
40
41
42 import org.apache.derby.jdbc.EmbeddedXADataSource;
43 import org.apache.derby.impl.drda.DRDAXid;
44 import org.apache.derby.iapi.jdbc.BrokeredConnection;
45 import org.apache.derby.iapi.jdbc.EngineConnection;
46
47 class XADatabase extends Database {
48
49
50     // XA Datasource used by all the XA connection requests
51
private EmbeddedXADataSource xaDataSource;
52
53     private XAResource JavaDoc xaResource;
54     private XAConnection JavaDoc xaConnection;
55
56     
57     XADatabase (String JavaDoc dbName)
58     {
59         super(dbName);
60         forXA = true;
61     }
62
63     /**
64      * Make a new connection using the database name and set
65      * the connection in the database
66      **/

67     synchronized void makeConnection(Properties JavaDoc p) throws
68  SQLException JavaDoc
69     {
70         if (xaDataSource == null)
71         {
72             xaDataSource = new EmbeddedXADataSource();
73         }
74
75         xaDataSource.setDatabaseName(shortDbName);
76         appendAttrString(p);
77         if (attrString != null)
78             xaDataSource.setConnectionAttributes(attrString);
79         
80         EngineConnection conn = getConnection();
81         // If we have no existing connection. this is a brand new XAConnection.
82
if (conn == null)
83         {
84             xaConnection = xaDataSource.getXAConnection(userId,password);
85             setXAResource(xaConnection.getXAResource());
86         }
87         else // this is just a connection reset. Close the logical connection.
88
{
89             conn.close();
90         }
91         
92         // Get a new logical connection.
93
// Contract between network server and embedded engine
94
// is that any connection returned implements EngineConnection.
95
conn = (EngineConnection) xaConnection.getConnection();
96         // Client will always drive the commits so connection should
97
// always be autocommit false on the server. DERBY-898/DERBY-899
98
conn.setAutoCommit(false);
99         setConnection(conn);
100     }
101
102     /** SetXAResource
103      * @param resource XAResource for this connection
104      */

105     protected void setXAResource (XAResource JavaDoc resource)
106     {
107         this.xaResource = resource;
108     }
109
110     /**
111      * get XA Resource for this connection
112      */

113     protected XAResource JavaDoc getXAResource ()
114     {
115         return this.xaResource;
116     }
117
118
119 }
120
121
122
123
124
125
126
127
128
129
Popular Tags