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