1 56 package org.objectstyle.cayenne.conn; 57 58 import java.sql.CallableStatement ; 59 import java.sql.Connection ; 60 import java.sql.DatabaseMetaData ; 61 import java.sql.PreparedStatement ; 62 import java.sql.SQLException ; 63 import java.sql.SQLWarning ; 64 import java.sql.Savepoint ; 65 import java.sql.Statement ; 66 import java.util.Map ; 67 68 76 public class ConnectionWrapper implements Connection { 77 private Connection connectionObj; 78 private PooledConnectionImpl pooledConnection; 79 private long lastReconnected; 80 private int reconnectCount; 81 82 91 static void sybaseAutoCommitPatch( 92 Connection c, 93 SQLException e, 94 boolean autoCommit) 95 throws SQLException { 96 97 String s = e.getMessage().toLowerCase(); 98 if (s.indexOf("set chained command not allowed") >= 0) { 99 c.commit(); 100 c.setAutoCommit(autoCommit); } else { 102 throw e; 103 } 104 } 105 106 107 public ConnectionWrapper( 108 Connection connectionObj, 109 PooledConnectionImpl pooledConnection) { 110 this.connectionObj = connectionObj; 111 this.pooledConnection = pooledConnection; 112 } 113 114 protected void reconnect(SQLException exception) throws SQLException { 115 116 if (reconnectCount > 0 119 && System.currentTimeMillis() - lastReconnected < 60000) { 120 121 retire(exception); 122 throw exception; 123 } 124 125 pooledConnection.reconnect(); 126 127 Connection connection = pooledConnection.getConnection(); 131 if (connection instanceof ConnectionWrapper) { 132 this.connectionObj = ((ConnectionWrapper) connection).connectionObj; 133 } 134 else { 135 this.connectionObj = connection; 136 } 137 138 lastReconnected = System.currentTimeMillis(); 139 reconnectCount++; 140 } 141 142 protected void retire(SQLException exception) { 143 pooledConnection.connectionErrorNotification(exception); 145 } 146 147 public void clearWarnings() throws SQLException { 148 try { 149 connectionObj.clearWarnings(); 150 } catch (SQLException sqlEx) { 151 retire(sqlEx); 152 throw sqlEx; 153 } 154 } 155 156 public void close() throws SQLException { 157 pooledConnection.returnConnectionToThePool(); 158 connectionObj = null; 159 pooledConnection = null; 160 } 161 162 public void commit() throws SQLException { 163 try { 164 connectionObj.commit(); 165 } catch (SQLException sqlEx) { 166 retire(sqlEx); 167 throw sqlEx; 168 } 169 } 170 171 public Statement createStatement() throws SQLException { 172 try { 173 return connectionObj.createStatement(); 174 } catch (SQLException sqlEx) { 175 176 reconnect(sqlEx); 178 return createStatement(); 179 } 180 } 181 182 public Statement createStatement( 183 int resultSetType, 184 int resultSetConcurrency) 185 throws SQLException { 186 try { 187 return connectionObj.createStatement( 188 resultSetType, 189 resultSetConcurrency); 190 } catch (SQLException sqlEx) { 191 192 reconnect(sqlEx); 194 return createStatement(resultSetType, resultSetConcurrency); 195 } 196 } 197 198 public boolean getAutoCommit() throws SQLException { 199 try { 200 return connectionObj.getAutoCommit(); 201 } catch (SQLException sqlEx) { 202 retire(sqlEx); 203 throw sqlEx; 204 } 205 } 206 207 public String getCatalog() throws SQLException { 208 try { 209 return connectionObj.getCatalog(); 210 } catch (SQLException sqlEx) { 211 retire(sqlEx); 212 throw sqlEx; 213 } 214 } 215 216 public DatabaseMetaData getMetaData() throws SQLException { 217 try { 218 return connectionObj.getMetaData(); 219 } catch (SQLException sqlEx) { 220 retire(sqlEx); 221 throw sqlEx; 222 } 223 } 224 225 public int getTransactionIsolation() throws SQLException { 226 try { 227 return connectionObj.getTransactionIsolation(); 228 } catch (SQLException sqlEx) { 229 retire(sqlEx); 230 throw sqlEx; 231 } 232 } 233 234 public SQLWarning getWarnings() throws SQLException { 235 try { 236 return connectionObj.getWarnings(); 237 } catch (SQLException sqlEx) { 238 retire(sqlEx); 239 throw sqlEx; 240 } 241 } 242 243 public boolean isClosed() throws SQLException { 244 if (connectionObj != null) { 245 try { 246 return connectionObj.isClosed(); 247 } catch (SQLException sqlEx) { 248 retire(sqlEx); 249 throw sqlEx; 250 } 251 } else 252 return true; 253 } 254 255 public boolean isReadOnly() throws SQLException { 256 try { 257 return connectionObj.isReadOnly(); 258 } catch (SQLException sqlEx) { 259 retire(sqlEx); 260 throw sqlEx; 261 } 262 } 263 264 public String nativeSQL(String sql) throws SQLException { 265 try { 266 return connectionObj.nativeSQL(sql); 267 } catch (SQLException sqlEx) { 268 retire(sqlEx); 269 throw sqlEx; 270 } 271 } 272 273 public CallableStatement prepareCall(String sql) throws SQLException { 274 try { 275 return connectionObj.prepareCall(sql); 276 } catch (SQLException sqlEx) { 277 278 reconnect(sqlEx); 280 return prepareCall(sql); 281 } 282 } 283 284 public CallableStatement prepareCall( 285 String sql, 286 int resultSetType, 287 int resultSetConcurrency) 288 throws SQLException { 289 try { 290 return connectionObj.prepareCall( 291 sql, 292 resultSetType, 293 resultSetConcurrency); 294 } catch (SQLException sqlEx) { 295 296 reconnect(sqlEx); 298 return prepareCall(sql, resultSetType, resultSetConcurrency); 299 } 300 } 301 302 public PreparedStatement prepareStatement(String sql) throws SQLException { 303 try { 304 return connectionObj.prepareStatement(sql); 305 } catch (SQLException sqlEx) { 306 307 reconnect(sqlEx); 309 return prepareStatement(sql); 310 } 311 } 312 313 public PreparedStatement prepareStatement( 314 String sql, 315 int resultSetType, 316 int resultSetConcurrency) 317 throws SQLException { 318 try { 319 return connectionObj.prepareStatement( 320 sql, 321 resultSetType, 322 resultSetConcurrency); 323 } catch (SQLException sqlEx) { 324 325 reconnect(sqlEx); 327 return prepareStatement(sql, resultSetType, resultSetConcurrency); 328 } 329 } 330 331 public void rollback() throws SQLException { 332 try { 333 connectionObj.rollback(); 334 } catch (SQLException sqlEx) { 335 retire(sqlEx); 336 throw sqlEx; 337 } 338 } 339 340 public void setAutoCommit(boolean autoCommit) throws SQLException { 341 try { 342 connectionObj.setAutoCommit(autoCommit); 343 } catch (SQLException sqlEx) { 344 345 try { 346 sybaseAutoCommitPatch(connectionObj, sqlEx, autoCommit); 348 } catch (SQLException patchEx) { 349 retire(sqlEx); 350 throw sqlEx; 351 } 352 } 353 } 354 355 public void setCatalog(String catalog) throws SQLException { 356 try { 357 connectionObj.setCatalog(catalog); 358 } catch (SQLException sqlEx) { 359 retire(sqlEx); 360 throw sqlEx; 361 } 362 } 363 364 public void setReadOnly(boolean readOnly) throws SQLException { 365 try { 366 connectionObj.setReadOnly(readOnly); 367 } catch (SQLException sqlEx) { 368 retire(sqlEx); 369 throw sqlEx; 370 } 371 } 372 373 public void setTransactionIsolation(int level) throws SQLException { 374 try { 375 connectionObj.setTransactionIsolation(level); 376 } catch (SQLException sqlEx) { 377 retire(sqlEx); 378 throw sqlEx; 379 } 380 } 381 382 public Map getTypeMap() throws SQLException { 383 try { 384 return connectionObj.getTypeMap(); 385 } catch (SQLException sqlEx) { 386 retire(sqlEx); 387 throw sqlEx; 388 } 389 } 390 391 public void setTypeMap(Map map) throws SQLException { 392 try { 393 connectionObj.setTypeMap(map); 394 } catch (SQLException sqlEx) { 395 retire(sqlEx); 396 throw sqlEx; 397 } 398 } 399 400 public void setHoldability(int holdability) throws SQLException { 401 throw new java.lang.UnsupportedOperationException ( 402 "Method setHoldability() not yet implemented."); 403 } 404 405 public int getHoldability() throws SQLException { 406 throw new java.lang.UnsupportedOperationException ( 407 "Method getHoldability() not yet implemented."); 408 } 409 410 public Savepoint setSavepoint() throws SQLException { 411 throw new java.lang.UnsupportedOperationException ( 412 "Method setSavepoint() not yet implemented."); 413 } 414 415 public Savepoint setSavepoint(String name) throws SQLException { 416 throw new java.lang.UnsupportedOperationException ( 417 "Method setSavepoint() not yet implemented."); 418 } 419 420 public void rollback(Savepoint savepoint) throws SQLException { 421 throw new java.lang.UnsupportedOperationException ( 422 "Method rollback() not yet implemented."); 423 } 424 425 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 426 throw new java.lang.UnsupportedOperationException ( 427 "Method releaseSavepoint() not yet implemented."); 428 } 429 430 public Statement createStatement( 431 int resultSetType, 432 int resultSetConcurrency, 433 int resultSetHoldability) 434 throws SQLException { 435 throw new java.lang.UnsupportedOperationException ( 436 "Method createStatement() not yet implemented."); 437 } 438 439 public PreparedStatement prepareStatement( 440 String sql, 441 int resultSetType, 442 int resultSetConcurrency, 443 int resultSetHoldability) 444 throws SQLException { 445 throw new java.lang.UnsupportedOperationException ( 446 "Method prepareStatement() not yet implemented."); 447 } 448 449 public CallableStatement prepareCall( 450 String sql, 451 int resultSetType, 452 int resultSetConcurrency, 453 int resultSetHoldability) throws SQLException { 454 try { 455 return connectionObj.prepareCall( 456 sql, 457 resultSetType, 458 resultSetConcurrency, 459 resultSetHoldability); 460 } 461 catch (SQLException sqlEx) { 462 463 reconnect(sqlEx); 465 return prepareCall( 466 sql, 467 resultSetType, 468 resultSetConcurrency, 469 resultSetHoldability); 470 } 471 } 472 473 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 474 throws SQLException { 475 476 try { 477 return connectionObj.prepareStatement(sql, autoGeneratedKeys); 478 } 479 catch (SQLException sqlEx) { 480 481 reconnect(sqlEx); 483 return prepareStatement(sql, autoGeneratedKeys); 484 } 485 } 486 487 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) 488 throws SQLException { 489 try { 490 return connectionObj.prepareStatement(sql, columnIndexes); 491 } 492 catch (SQLException sqlEx) { 493 494 reconnect(sqlEx); 496 return prepareStatement(sql, columnIndexes); 497 } 498 } 499 500 public PreparedStatement prepareStatement(String sql, String [] columnNames) 501 throws SQLException { 502 try { 503 return connectionObj.prepareStatement(sql, columnNames); 504 } 505 catch (SQLException sqlEx) { 506 507 reconnect(sqlEx); 509 return prepareStatement(sql, columnNames); 510 } 511 } 512 513 } | Popular Tags |