KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.jdbc.EmbedPooledConnection40
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.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import javax.sql.StatementEvent JavaDoc;
30 import javax.sql.StatementEventListener JavaDoc;
31
32 /**
33     A PooledConnection object is a connection object that provides hooks for
34     connection pool management.
35
36     <P>This is Derby's implementation of a PooledConnection for use in
37     the following environments:
38     <UL>
39     <LI> JDBC 4.0 - J2SE 6.0
40     </UL>
41
42  */

43 class EmbedPooledConnection40 extends EmbedPooledConnection {
44     
45     //using generics to avoid casting problems
46
protected final Vector JavaDoc<StatementEventListener JavaDoc> statementEventListeners =
47             new Vector JavaDoc<StatementEventListener JavaDoc>();
48     
49
50     EmbedPooledConnection40 (ReferenceableDataSource ds, String JavaDoc user,
51                  String JavaDoc password, boolean requestPassword) throws SQLException JavaDoc {
52         super (ds, user, password, requestPassword);
53     }
54     /**
55      * Removes the specified <code>StatementEventListener</code> from the list of
56      * components that will be notified when the driver detects that a
57      * <code>PreparedStatement</code> has been closed or is invalid.
58      * <p>
59      *
60      * @param listener the component which implements the
61      * <code>StatementEventListener</code> interface that was previously
62      * registered with this <code>PooledConnection</code> object
63      * <p>
64      * @since 1.6
65      */

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

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

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

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