KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > EmbedXAConnection40


1 /*
2  
3    Derby - class org.apache.derby.jdbc.EmbedXAConnection40
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.jdbc;
23
24 import java.util.Vector JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import javax.sql.StatementEvent JavaDoc;
28 import javax.sql.StatementEventListener JavaDoc;
29 import javax.sql.XAConnection JavaDoc;
30 import org.apache.derby.iapi.jdbc.ResourceAdapter;
31
32 /**
33  * This class implements jdbc4.0 methods of XAConnection
34  */

35 final class EmbedXAConnection40 extends EmbedXAConnection
36         implements XAConnection JavaDoc {
37     
38     //using generics to avoid casting problems
39
protected final Vector JavaDoc<StatementEventListener JavaDoc> statementEventListeners =
40             new Vector JavaDoc<StatementEventListener JavaDoc>();
41     
42     /**
43      * Creates EmbedXAConnection40.
44      * @param ds
45      * @param ra
46      * @param user
47      * @param password
48      * @param requestPassword
49      */

50         EmbedXAConnection40 (EmbeddedDataSource ds, ResourceAdapter ra,
51                 String JavaDoc user, String JavaDoc password,
52                 boolean requestPassword) throws SQLException JavaDoc {
53         super(ds, ra, user, password, requestPassword);
54     }
55     /**
56      * Removes the specified <code>StatementEventListener</code> from the list of
57      * components that will be notified when the driver detects that a
58      * <code>PreparedStatement</code> has been closed or is invalid.
59      * <p>
60      *
61      * @param listener the component which implements the
62      * <code>StatementEventListener</code> interface that was previously
63      * registered with this <code>PooledConnection</code> object
64      * <p>
65      * @since 1.6
66      */

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

88     public void addStatementEventListener(StatementEventListener JavaDoc listener) {
89          if (!isActive)
90             return;
91         if (listener == null)
92             return;
93         statementEventListeners.addElement(listener);
94     }
95     
96     /**
97      * Raise the statementClosed event for all the listeners when the
98      * corresponding events occurs
99      * @param statement PreparedStatement
100      */

101     public void onStatementClose(PreparedStatement JavaDoc statement) {
102         if (!statementEventListeners.isEmpty()){
103             StatementEvent JavaDoc event = new StatementEvent JavaDoc(this,statement);
104             //synchronized block on statementEventListeners to make it thread
105
//safe
106
synchronized(statementEventListeners) {
107                 for (StatementEventListener JavaDoc l : statementEventListeners) {
108                     l.statementClosed(event);
109                 }
110             }
111         }
112     }
113     
114     /**
115      * Raise the statementErrorOccurred event for all the listeners when the
116      * corresponding events occurs
117      * @param statement PreparedStatement
118      * @param sqle SQLException
119      */

120     public void onStatementErrorOccurred(PreparedStatement JavaDoc statement,SQLException JavaDoc sqle) {
121         if (!statementEventListeners.isEmpty()){
122             StatementEvent JavaDoc event = new StatementEvent JavaDoc(this,statement,sqle);
123             //synchronized block on statementEventListeners to make it thread
124
//safe
125
synchronized(statementEventListeners) {
126                 for (StatementEventListener JavaDoc l : statementEventListeners){
127                     l.statementErrorOccurred(event);
128                 }
129             }
130         }
131     }
132    
133 }
Popular Tags