1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbcp2.datasources;
19
20 import java.sql.Connection;
21 import java.sql.SQLException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.EventListener;
26 import java.util.List;
27
28 import javax.sql.ConnectionEvent;
29 import javax.sql.ConnectionEventListener;
30 import javax.sql.PooledConnection;
31 import javax.sql.StatementEventListener;
32
33
34
35
36
37
38 public class PooledConnectionProxy implements PooledConnection,
39 ConnectionEventListener {
40
41 protected PooledConnection delegate;
42
43
44
45
46 private final List<EventListener> eventListeners = Collections.synchronizedList(new ArrayList<>());
47
48
49
50
51
52
53 private boolean notifyOnClose;
54
55 public PooledConnectionProxy(final PooledConnection pooledConnection) {
56 this.delegate = pooledConnection;
57 pooledConnection.addConnectionEventListener(this);
58 }
59
60
61
62
63 @Override
64 public void addConnectionEventListener(final ConnectionEventListener listener) {
65 if (!eventListeners.contains(listener)) {
66 eventListeners.add(listener);
67 }
68 }
69
70 @Override
71 public void addStatementEventListener(final StatementEventListener listener) {
72 if (!eventListeners.contains(listener)) {
73 eventListeners.add(listener);
74 }
75 }
76
77
78
79
80 @Override
81 public void close() throws SQLException {
82 delegate.close();
83 if (isNotifyOnClose()) {
84 notifyListeners();
85 }
86 }
87
88
89
90
91 @Override
92 public void connectionClosed(final ConnectionEvent event) {
93 notifyListeners();
94 }
95
96
97
98
99 @Override
100 public void connectionErrorOccurred(final ConnectionEvent event) {
101 final Object[] listeners = eventListeners.toArray();
102 for (final Object listener : listeners) {
103 ((ConnectionEventListener) listener).connectionErrorOccurred(event);
104 }
105 }
106
107 @Override
108 public Connection getConnection() throws SQLException {
109 return delegate.getConnection();
110 }
111
112
113
114
115 public Collection<EventListener> getListeners() {
116 return eventListeners;
117 }
118
119 public boolean isNotifyOnClose() {
120 return notifyOnClose;
121 }
122
123
124
125
126 void notifyListeners() {
127 final ConnectionEvent event = new ConnectionEvent(this);
128 final Object[] listeners = eventListeners.toArray();
129 for (final Object listener : listeners) {
130 ((ConnectionEventListener) listener).connectionClosed(event);
131 }
132 }
133
134
135
136
137 @Override
138 public void removeConnectionEventListener(final ConnectionEventListener listener) {
139 eventListeners.remove(listener);
140 }
141
142 @Override
143 public void removeStatementEventListener(final StatementEventListener listener) {
144 eventListeners.remove(listener);
145 }
146
147 public void setNotifyOnClose(final boolean notifyOnClose) {
148 this.notifyOnClose = notifyOnClose;
149 }
150
151
152
153
154 public void throwConnectionError() {
155 final ConnectionEvent event = new ConnectionEvent(this);
156 connectionErrorOccurred(event);
157 }
158
159 }