KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > ClientXAConnection40


1 /*
2
3    Derby - Class org.apache.derby.client.ClientXAConnection40
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.client;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28 import javax.sql.StatementEvent JavaDoc;
29 import javax.sql.StatementEventListener JavaDoc;
30 import org.apache.derby.client.am.SqlException;
31 import org.apache.derby.client.net.NetLogWriter;
32 import org.apache.derby.client.net.NetXAConnection;
33 import org.apache.derby.jdbc.ClientDataSource;
34 import org.apache.derby.jdbc.ClientXADataSource;
35
36 /**
37  * jdbc4.0 implementation of XAConnection
38  */

39 public class ClientXAConnection40 extends ClientXAConnection {
40     
41     //using generics to avoid casting problems
42
protected final Vector JavaDoc<StatementEventListener JavaDoc> statementEventListeners =
43              new Vector JavaDoc<StatementEventListener JavaDoc>();
44
45     
46     /**
47      * Constructor for ClientXAConnection40.
48      * @param ds
49      * @param logWtr
50      * @param userId
51      * @param password
52      */

53     public ClientXAConnection40 (ClientXADataSource ds,
54                               org.apache.derby.client.net.NetLogWriter logWtr,
55                               String JavaDoc userId,
56                               String JavaDoc password) throws SQLException JavaDoc {
57         super(ds, logWtr, userId, password);
58     }
59     
60     
61     /**
62      * Removes the specified <code>StatementEventListener</code> from the list of
63      * components that will be notified when the driver detects that a
64      * <code>PreparedStatement</code> has been closed or is invalid.
65      * <p>
66      *
67      * @param listener the component which implements the
68      * <code>StatementEventListener</code> interface that was previously
69      * registered with this <code>PooledConnection</code> object
70      * <p>
71      */

72     public void removeStatementEventListener(StatementEventListener JavaDoc listener) {
73         if (logWriter_ != null) {
74             logWriter_.traceEntry(this, "removeConnectionEventListener", listener);
75         }
76         statementEventListeners.removeElement(listener);
77     }
78     
79     /**
80      * Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. Components that
81      * wish to be notified when <code>PreparedStatement</code>s created by the
82      * connection are closed or are detected to be invalid may use this method
83      * to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object.
84      * <p>
85      *
86      * @param listener an component which implements the
87      * <code>StatementEventListener</code> interface that is to be
88      * registered with this <code>PooledConnection</code> object
89      * <p>
90      */

91     public void addStatementEventListener(StatementEventListener JavaDoc listener) {
92         if (logWriter_ != null) {
93             logWriter_.traceEntry(this, "addStatementEventListener", listener);
94         }
95         statementEventListeners.addElement(listener);
96     }
97     
98     /**
99      * Raise the statementClosed event for all the listeners when the
100      * corresponding events occurs
101      * @param statement The PreparedStatement that was closed
102      */

103     public void onStatementClose(PreparedStatement JavaDoc statement) {
104         if (!statementEventListeners.isEmpty()) {
105             StatementEvent JavaDoc event = new StatementEvent JavaDoc(this,statement);
106             //synchronized block on statementEventListeners to make it thread
107
//safe
108
synchronized(statementEventListeners) {
109                 for (StatementEventListener JavaDoc l : statementEventListeners) {
110                     l.statementClosed(event);
111                 }
112             }
113         }
114     }
115     
116     /**
117      *
118      * Raise the statementErrorOccurred event for all the listeners when the
119      * corresponding events occurs.
120      *
121      * @param statement The PreparedStatement on which error occurred
122      * @param sqle The SQLException associated with the error that
123      * caused the invalidation of the PreparedStatements
124      *
125      */

126     public void onStatementErrorOccurred(PreparedStatement JavaDoc statement,
127                     SQLException JavaDoc sqle) {
128         if (!statementEventListeners.isEmpty()) {
129             StatementEvent JavaDoc event = new StatementEvent JavaDoc(this,statement,sqle);
130             //synchronized block on statementEventListeners to make it thread
131
//safe
132
synchronized(statementEventListeners) {
133                 for (StatementEventListener JavaDoc l : statementEventListeners) {
134                     l.statementErrorOccurred(event);
135                 }
136             }
137         }
138     }
139 }
140
Popular Tags