001: /*
002:
003: Derby - Class org.apache.derby.client.ClientXAConnection40
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.client;
023:
024: import java.sql.PreparedStatement;
025: import java.sql.SQLException;
026: import java.util.Enumeration;
027: import java.util.Vector;
028: import javax.sql.StatementEvent;
029: import javax.sql.StatementEventListener;
030: import org.apache.derby.client.am.SqlException;
031: import org.apache.derby.client.net.NetLogWriter;
032: import org.apache.derby.client.net.NetXAConnection;
033: import org.apache.derby.jdbc.ClientDataSource;
034: import org.apache.derby.jdbc.ClientXADataSource;
035:
036: /**
037: * jdbc4.0 implementation of XAConnection
038: */
039: public class ClientXAConnection40 extends ClientXAConnection {
040:
041: //using generics to avoid casting problems
042: protected final Vector<StatementEventListener> statementEventListeners = new Vector<StatementEventListener>();
043:
044: /**
045: * Constructor for ClientXAConnection40.
046: * @param ds
047: * @param logWtr
048: * @param userId
049: * @param password
050: */
051: public ClientXAConnection40(ClientXADataSource ds,
052: org.apache.derby.client.net.NetLogWriter logWtr,
053: String userId, String password) throws SQLException {
054: super (ds, logWtr, userId, password);
055: }
056:
057: /**
058: * Removes the specified <code>StatementEventListener</code> from the list of
059: * components that will be notified when the driver detects that a
060: * <code>PreparedStatement</code> has been closed or is invalid.
061: * <p>
062: *
063: * @param listener the component which implements the
064: * <code>StatementEventListener</code> interface that was previously
065: * registered with this <code>PooledConnection</code> object
066: * <p>
067: */
068: public void removeStatementEventListener(
069: StatementEventListener listener) {
070: if (logWriter_ != null) {
071: logWriter_.traceEntry(this ,
072: "removeConnectionEventListener", listener);
073: }
074: statementEventListeners.removeElement(listener);
075: }
076:
077: /**
078: * Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. Components that
079: * wish to be notified when <code>PreparedStatement</code>s created by the
080: * connection are closed or are detected to be invalid may use this method
081: * to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object.
082: * <p>
083: *
084: * @param listener an component which implements the
085: * <code>StatementEventListener</code> interface that is to be
086: * registered with this <code>PooledConnection</code> object
087: * <p>
088: */
089: public void addStatementEventListener(
090: StatementEventListener listener) {
091: if (logWriter_ != null) {
092: logWriter_.traceEntry(this , "addStatementEventListener",
093: listener);
094: }
095: statementEventListeners.addElement(listener);
096: }
097:
098: /**
099: * 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 statement) {
104: if (!statementEventListeners.isEmpty()) {
105: StatementEvent event = new StatementEvent(this , statement);
106: //synchronized block on statementEventListeners to make it thread
107: //safe
108: synchronized (statementEventListeners) {
109: for (StatementEventListener 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 statement,
127: SQLException sqle) {
128: if (!statementEventListeners.isEmpty()) {
129: StatementEvent event = new StatementEvent(this , statement,
130: sqle);
131: //synchronized block on statementEventListeners to make it thread
132: //safe
133: synchronized (statementEventListeners) {
134: for (StatementEventListener l : statementEventListeners) {
135: l.statementErrorOccurred(event);
136: }
137: }
138: }
139: }
140: }
|