KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc_xa > XAConnectionImpl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XAConnectionImpl.java,v 1.11 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.jdbc_xa;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Vector JavaDoc;
32 import javax.transaction.xa.XAResource JavaDoc;
33 import javax.sql.ConnectionEvent JavaDoc;
34 import javax.sql.ConnectionEventListener JavaDoc;
35 import javax.sql.XAConnection JavaDoc;
36 import org.objectweb.jonas.common.Log;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * This is the implementation of the XAConnection interface. This object
42  * encapsulates 2 objects: the Connection and the XAResource.
43  * Connection is used for std SQL primitive, XAResource is used for
44  * XA primitive (transaction demarcation).
45  *
46  * @author Philippe Durieux
47  * Contributor(s):
48  */

49
50 class XAConnectionImpl implements XAConnection JavaDoc {
51
52     static private Logger logger = null;
53
54     XAResource JavaDoc xaRes = null;
55     Connection JavaDoc implConn = null;
56     Connection JavaDoc actConn = null;
57     Vector JavaDoc eventListeners = new Vector JavaDoc();
58
59     // -----------------------------------------------------------------
60
// Constructors
61
// -----------------------------------------------------------------
62

63     public XAConnectionImpl(Connection JavaDoc conn, XADataSourceImpl ds) {
64
65         this.actConn = conn;
66
67         // An XAConnection holds 2 objects: 1 Connection + 1 XAResource
68
this.implConn = new ConnectionImpl(this, conn);
69         this.xaRes = new XAResourceImpl(this, conn, ds);
70         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
71         if (logger.isLoggable(BasicLevel.DEBUG)) {
72             logger.log(BasicLevel.DEBUG, "constructor");
73         }
74     }
75
76     // -----------------------------------------------------------------
77
// XAConnection implementation
78
// -----------------------------------------------------------------
79

80     /**
81      * Return an XA resource to the caller.
82      *
83      * @return The XAResource
84      *
85      * @exception SQLException - if a database-access error occurs
86      */

87     public XAResource JavaDoc getXAResource() throws SQLException JavaDoc {
88         return xaRes;
89     }
90
91     // -----------------------------------------------------------------
92
// PooledConnection implementation
93
// -----------------------------------------------------------------
94

95     /**
96      * Create an object handle for a database connection.
97      *
98      * @exception SQLException - if a database-access error occurs
99      */

100     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
101
102         // Just return the already created object.
103
return implConn;
104     }
105
106     /**
107      * Close the database connection.
108      *
109      * @exception SQLException - if a database-access error occurs
110      */

111     public void close() throws SQLException JavaDoc {
112
113         if (logger.isLoggable(BasicLevel.DEBUG)) {
114             logger.log(BasicLevel.DEBUG, "");
115         }
116
117         // Close the actual Connection here.
118
if (actConn != null) {
119             actConn.close();
120         } else {
121             logger.log(BasicLevel.ERROR, "Connection already closed");
122         }
123         actConn = null;
124
125         // We can forget Connection and XAResource too.
126
xaRes = null;
127         implConn = null;
128     }
129
130     /**
131      * Add an event listener.
132      *
133      * @param listener event listener
134      */

135     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
136
137         if (logger.isLoggable(BasicLevel.DEBUG)) {
138             logger.log(BasicLevel.DEBUG, "");
139         }
140         eventListeners.addElement(listener);
141     }
142
143     /**
144      * Remove an event listener.
145      *
146      * @param listener event listener
147      */

148     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
149
150         if (logger.isLoggable(BasicLevel.DEBUG)) {
151             logger.log(BasicLevel.DEBUG, "");
152         }
153         eventListeners.removeElement(listener);
154     }
155
156     // -----------------------------------------------------------------
157
// Other methods
158
// -----------------------------------------------------------------
159

160     /*
161      * Notify a Close event on Connection
162      */

163     public void notifyClose() {
164
165         if (logger.isLoggable(BasicLevel.DEBUG)) {
166             logger.log(BasicLevel.DEBUG, "");
167         }
168
169         // Notify event to listeners
170
for (int i = 0; i < eventListeners.size(); i++) {
171             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc) eventListeners.elementAt(i);
172             l.connectionClosed(new ConnectionEvent JavaDoc(this));
173         }
174     }
175
176     /*
177      * Notify an Error event on Connection
178      */

179     public void notifyError(SQLException JavaDoc ex) {
180
181         if (logger.isLoggable(BasicLevel.DEBUG)) {
182             logger.log(BasicLevel.DEBUG, "");
183         }
184
185         // Notify event to listeners
186
for (int i = 0; i < eventListeners.size(); i++) {
187             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc) eventListeners.elementAt(i);
188             l.connectionErrorOccurred(new ConnectionEvent JavaDoc(this, ex));
189         }
190     }
191
192
193 }
194
Popular Tags