1 | |
package org.apache.ojb.broker.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.sql.CallableStatement; |
19 | |
import java.sql.Connection; |
20 | |
import java.sql.DatabaseMetaData; |
21 | |
import java.sql.PreparedStatement; |
22 | |
import java.sql.SQLException; |
23 | |
import java.sql.SQLWarning; |
24 | |
import java.sql.Statement; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
28 | |
import org.apache.commons.lang.builder.ToStringStyle; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class WrappedConnection implements Connection |
35 | |
{ |
36 | |
private Connection _conn = null; |
37 | |
private boolean _isClosed = false; |
38 | |
|
39 | |
public WrappedConnection(Connection c) |
40 | |
{ |
41 | |
_conn = c; |
42 | |
} |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public Connection getDelegate() |
49 | |
{ |
50 | |
return _conn; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public Connection getInnermostDelegate() |
69 | |
{ |
70 | |
Connection c = _conn; |
71 | |
while (c != null && c instanceof WrappedConnection) |
72 | |
{ |
73 | |
c = ((WrappedConnection) c).getDelegate(); |
74 | |
if (this == c) |
75 | |
{ |
76 | |
return null; |
77 | |
} |
78 | |
} |
79 | |
return c; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
public void setDelegate(Connection c) |
84 | |
{ |
85 | |
_conn = c; |
86 | |
} |
87 | |
|
88 | |
protected void checkOpen() throws SQLException |
89 | |
{ |
90 | |
if (_isClosed) |
91 | |
{ |
92 | |
throw new SQLException("Connection is closed. " + this); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void activateConnection() |
100 | |
{ |
101 | |
_isClosed = false; |
102 | |
if (_conn instanceof WrappedConnection) |
103 | |
{ |
104 | |
((WrappedConnection) _conn).activateConnection(); |
105 | |
} |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void passivateConnection() throws SQLException |
112 | |
{ |
113 | |
_isClosed = true; |
114 | |
if (_conn instanceof WrappedConnection) |
115 | |
{ |
116 | |
((WrappedConnection) _conn).passivateConnection(); |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
public String toString() |
121 | |
{ |
122 | |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) |
123 | |
.append("wrapped connection", (_conn != null ? _conn.toString() : null)) |
124 | |
.toString(); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void close() throws SQLException |
132 | |
{ |
133 | |
passivateConnection(); |
134 | |
_conn.close(); |
135 | |
} |
136 | |
|
137 | |
public boolean isClosed() throws SQLException |
138 | |
{ |
139 | |
if (_isClosed || _conn.isClosed()) |
140 | |
{ |
141 | |
return true; |
142 | |
} |
143 | |
return false; |
144 | |
} |
145 | |
|
146 | |
public Statement createStatement() throws SQLException |
147 | |
{ |
148 | |
checkOpen(); |
149 | |
return _conn.createStatement(); |
150 | |
} |
151 | |
|
152 | |
public Statement createStatement(int resultSetType, |
153 | |
int resultSetConcurrency) |
154 | |
throws SQLException |
155 | |
{ |
156 | |
checkOpen(); |
157 | |
return _conn.createStatement(resultSetType, resultSetConcurrency); |
158 | |
} |
159 | |
|
160 | |
public PreparedStatement prepareStatement(String sql) throws SQLException |
161 | |
{ |
162 | |
checkOpen(); |
163 | |
return _conn.prepareStatement(sql); |
164 | |
} |
165 | |
|
166 | |
public PreparedStatement prepareStatement(String sql, |
167 | |
int resultSetType, |
168 | |
int resultSetConcurrency) |
169 | |
throws SQLException |
170 | |
{ |
171 | |
checkOpen(); |
172 | |
return _conn.prepareStatement(sql, resultSetType, resultSetConcurrency); |
173 | |
} |
174 | |
|
175 | |
public CallableStatement prepareCall(String sql) throws SQLException |
176 | |
{ |
177 | |
checkOpen(); |
178 | |
return _conn.prepareCall(sql); |
179 | |
} |
180 | |
|
181 | |
public CallableStatement prepareCall(String sql, |
182 | |
int resultSetType, |
183 | |
int resultSetConcurrency) |
184 | |
throws SQLException |
185 | |
{ |
186 | |
checkOpen(); |
187 | |
return _conn.prepareCall(sql, resultSetType, resultSetConcurrency); |
188 | |
} |
189 | |
|
190 | |
public void clearWarnings() throws SQLException |
191 | |
{ |
192 | |
checkOpen(); |
193 | |
_conn.clearWarnings(); |
194 | |
} |
195 | |
|
196 | |
public void commit() throws SQLException |
197 | |
{ |
198 | |
checkOpen(); |
199 | |
_conn.commit(); |
200 | |
} |
201 | |
|
202 | |
public boolean getAutoCommit() throws SQLException |
203 | |
{ |
204 | |
checkOpen(); |
205 | |
return _conn.getAutoCommit(); |
206 | |
} |
207 | |
|
208 | |
public String getCatalog() throws SQLException |
209 | |
{ |
210 | |
checkOpen(); |
211 | |
return _conn.getCatalog(); |
212 | |
} |
213 | |
|
214 | |
public DatabaseMetaData getMetaData() throws SQLException |
215 | |
{ |
216 | |
checkOpen(); |
217 | |
return _conn.getMetaData(); |
218 | |
} |
219 | |
|
220 | |
public int getTransactionIsolation() throws SQLException |
221 | |
{ |
222 | |
checkOpen(); |
223 | |
return _conn.getTransactionIsolation(); |
224 | |
} |
225 | |
|
226 | |
public Map getTypeMap() throws SQLException |
227 | |
{ |
228 | |
checkOpen(); |
229 | |
return _conn.getTypeMap(); |
230 | |
} |
231 | |
|
232 | |
public SQLWarning getWarnings() throws SQLException |
233 | |
{ |
234 | |
checkOpen(); |
235 | |
return _conn.getWarnings(); |
236 | |
} |
237 | |
|
238 | |
public boolean isReadOnly() throws SQLException |
239 | |
{ |
240 | |
checkOpen(); |
241 | |
return _conn.isReadOnly(); |
242 | |
} |
243 | |
|
244 | |
public String nativeSQL(String sql) throws SQLException |
245 | |
{ |
246 | |
checkOpen(); |
247 | |
return _conn.nativeSQL(sql); |
248 | |
} |
249 | |
|
250 | |
public void rollback() throws SQLException |
251 | |
{ |
252 | |
checkOpen(); |
253 | |
_conn.rollback(); |
254 | |
} |
255 | |
|
256 | |
public void setAutoCommit(boolean autoCommit) throws SQLException |
257 | |
{ |
258 | |
checkOpen(); |
259 | |
_conn.setAutoCommit(autoCommit); |
260 | |
} |
261 | |
|
262 | |
public void setCatalog(String catalog) throws SQLException |
263 | |
{ |
264 | |
checkOpen(); |
265 | |
_conn.setCatalog(catalog); |
266 | |
} |
267 | |
|
268 | |
public void setReadOnly(boolean readOnly) throws SQLException |
269 | |
{ |
270 | |
checkOpen(); |
271 | |
_conn.setReadOnly(readOnly); |
272 | |
} |
273 | |
|
274 | |
public void setTransactionIsolation(int level) throws SQLException |
275 | |
{ |
276 | |
checkOpen(); |
277 | |
_conn.setTransactionIsolation(level); |
278 | |
} |
279 | |
|
280 | |
public void setTypeMap(Map map) throws SQLException |
281 | |
{ |
282 | |
checkOpen(); |
283 | |
_conn.setTypeMap(map); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public int getHoldability() throws SQLException { |
294 | |
checkOpen(); |
295 | |
return _conn.getHoldability(); |
296 | |
} |
297 | |
|
298 | |
public void setHoldability(int holdability) throws SQLException { |
299 | |
checkOpen(); |
300 | |
_conn.setHoldability(holdability); |
301 | |
} |
302 | |
|
303 | |
public java.sql.Savepoint setSavepoint() throws SQLException { |
304 | |
checkOpen(); |
305 | |
return _conn.setSavepoint(); |
306 | |
} |
307 | |
|
308 | |
public java.sql.Savepoint setSavepoint(String name) throws SQLException { |
309 | |
checkOpen(); |
310 | |
return _conn.setSavepoint(name); |
311 | |
} |
312 | |
|
313 | |
public void rollback(java.sql.Savepoint savepoint) throws SQLException { |
314 | |
checkOpen(); |
315 | |
_conn.rollback(savepoint); |
316 | |
} |
317 | |
|
318 | |
public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException { |
319 | |
checkOpen(); |
320 | |
_conn.releaseSavepoint(savepoint); |
321 | |
} |
322 | |
|
323 | |
public Statement createStatement(int resultSetType, |
324 | |
int resultSetConcurrency, |
325 | |
int resultSetHoldability) |
326 | |
throws SQLException { |
327 | |
checkOpen(); |
328 | |
return _conn.createStatement(resultSetType, resultSetConcurrency, |
329 | |
resultSetHoldability); |
330 | |
} |
331 | |
|
332 | |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
333 | |
int resultSetConcurrency, |
334 | |
int resultSetHoldability) |
335 | |
throws SQLException { |
336 | |
checkOpen(); |
337 | |
return _conn.prepareStatement(sql, resultSetType, |
338 | |
resultSetConcurrency, |
339 | |
resultSetHoldability); |
340 | |
} |
341 | |
|
342 | |
public CallableStatement prepareCall(String sql, int resultSetType, |
343 | |
int resultSetConcurrency, |
344 | |
int resultSetHoldability) |
345 | |
throws SQLException { |
346 | |
checkOpen(); |
347 | |
return _conn.prepareCall(sql, resultSetType, |
348 | |
resultSetConcurrency, |
349 | |
resultSetHoldability); |
350 | |
} |
351 | |
|
352 | |
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) |
353 | |
throws SQLException { |
354 | |
checkOpen(); |
355 | |
return _conn.prepareStatement(sql, autoGeneratedKeys); |
356 | |
} |
357 | |
|
358 | |
public PreparedStatement prepareStatement(String sql, int columnIndexes[]) |
359 | |
throws SQLException { |
360 | |
checkOpen(); |
361 | |
return _conn.prepareStatement(sql, columnIndexes); |
362 | |
} |
363 | |
|
364 | |
public PreparedStatement prepareStatement(String sql, String columnNames[]) |
365 | |
throws SQLException { |
366 | |
checkOpen(); |
367 | |
return _conn.prepareStatement(sql, columnNames); |
368 | |
} |
369 | |
|
370 | |
|
371 | |
} |