1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.connection; 31 32 import com.sun.jdo.api.persistence.support.JDODataStoreException; 33 import com.sun.jdo.api.persistence.support.Transaction; 34 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 35 import com.sun.jdo.spi.persistence.utility.Linkable; 36 import com.sun.jdo.spi.persistence.utility.logging.Logger; 37 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore; 38 39 40 import java.sql.*; 41 42 48 public class ConnectionImpl implements Connection, Linkable { 49 52 private Connection connection; 53 54 57 private String url; 58 59 62 private String userName; 63 64 67 Linkable previous; 68 69 72 Linkable next; 73 74 77 private boolean pooled; 78 79 82 private Transaction transaction; 83 84 88 boolean freePending; 89 90 94 96 99 ConnectionManager connectionManager; 100 101 104 private static Logger logger = LogHelperSQLStore.getLogger(); 105 106 107 113 public ConnectionImpl(Connection conn, String url, String userName, 114 ConnectionManager connMgr) { 115 super(); 116 this.connection = conn; 117 this.url = url; 118 this.userName = userName; 119 this.previous = null; 120 this.next = null; 121 this.pooled = false; 122 this.transaction = null; 123 this.freePending = false; 124 this.connectionManager = connMgr; 126 } 127 128 131 public synchronized Statement createStatement() 138 throws SQLException { 139 StatementImpl fstmt; 140 141 try { 142 this.checkXact(); 143 fstmt = new StatementImpl(this, this.connection.createStatement()); 144 } catch (SQLException se) { 145 throw se; 146 } 147 148 return ((Statement) fstmt); 149 } 150 151 public Statement createStatement(int resultSetType, 152 int resultSetConcurrency) 153 throws SQLException { 154 return (null); 155 } 156 157 public PreparedStatement prepareStatement(String sql, int resultSetType, 158 int resultSetConcurrency) 159 throws SQLException { 160 return (null); 161 } 162 163 public CallableStatement prepareCall(String sql, 164 int resultSetType, 165 int resultSetConcurrency) 166 throws SQLException { 167 return (null); 168 } 169 170 public synchronized PreparedStatement prepareStatement(String sql) 179 throws SQLException { 180 PreparedStatementImpl fpstmt; 181 182 try { 183 this.checkXact(); 184 fpstmt = new PreparedStatementImpl(this, 185 this.connection.prepareStatement(sql)); 186 } catch (SQLException se) { 187 throw se; 188 } 189 190 return ((PreparedStatement) fpstmt); 191 } 192 193 public synchronized CallableStatement prepareCall(String sql) 201 throws SQLException { 202 CallableStatementImpl fcstmt; 203 204 try { 205 this.checkXact(); 206 fcstmt = new CallableStatementImpl(this, 207 this.connection.prepareCall(sql)); 208 return ((CallableStatement) fcstmt); 209 } catch (SQLException se) { 210 throw se; 211 } 212 } 213 214 public synchronized String nativeSQL(String sql) throws SQLException { 215 try { 216 return (this.connection.nativeSQL(sql)); 217 } catch (SQLException se) { 218 throw se; 219 } 220 } 221 222 public synchronized void setAutoCommit(boolean autoCommit) 223 throws SQLException { 224 try { 225 this.connection.setAutoCommit(autoCommit); 226 } catch (SQLException se) { 227 throw se; 228 } 229 } 230 231 public synchronized boolean getAutoCommit() throws SQLException { 232 try { 233 return (this.connection.getAutoCommit()); 234 } catch (SQLException se) { 235 throw se; 236 } 237 } 238 239 public synchronized void commit() throws SQLException { 240 241 try { 242 this.connection.commit(); 243 if (this.freePending) { 244 if (this.connectionManager.shutDownPending) { 245 try { 246 this.connection.close(); 247 logger.finest("sqlstore.connectionimpl.commit"); } catch (SQLException se) { 249 ; 250 } 251 } else { 252 this.freePending = false; 253 this.connectionManager.freeList.insertAtTail(this); 254 } 255 } 256 if (EJBHelper.isManaged()) { 257 closeInternal(); 259 } 260 } catch (SQLException se) { 261 throw se; 262 } 263 } 264 265 public synchronized void rollback() throws SQLException { 266 logger.finest("sqlstore.connectionimpl.rollback"); 268 try { 269 this.connection.rollback(); 270 if (this.freePending) { 271 if (this.connectionManager.shutDownPending) { 272 this.connection.close(); 273 logger.finest("sqlstore.connectionimpl.rollback.close"); } else { 275 this.freePending = false; 276 this.connectionManager.freeList.insertAtTail(this); 277 } 278 } 279 if (EJBHelper.isManaged()) { 280 closeInternal(); 282 } 283 } catch (SQLException se) { 284 throw se; 285 } 286 } 287 288 public synchronized void close() throws SQLException { 289 if (EJBHelper.isManaged()) { 290 logger.finest("sqlstore.connectionimpl.close"); 292 return; 294 } 295 296 closeInternal(); 297 } 298 299 private synchronized void closeInternal() throws SQLException { 300 301 boolean debug = logger.isLoggable(Logger.FINEST); 302 303 ConnectionImpl conn = (ConnectionImpl) this; 304 305 if (debug) { 306 logger.finest("sqlstore.connectionimpl.close_arg",conn); } 308 309 try { 310 conn.connectionManager.busyList.removeFromList((Linkable) conn); 311 if (conn.xactPending() == true) { 312 conn.setFreePending(true); 313 if (debug) { 314 logger.finest("sqlstore.connectionimpl.close.freepending"); } 316 } else if ((conn.getPooled() == true) && (conn.connectionManager.shutDownPending == false)) { 317 conn.connectionManager.freeList.insertAtTail((Linkable) conn); 318 if (debug) { 319 logger.finest("sqlstore.connectionimpl.close.putfreelist"); } 321 } else { 322 if (EJBHelper.isManaged()) { 323 this.connection.close(); 325 if (debug) { 326 logger.finest("sqlstore.connectionimpl.close.exit"); } 328 } else { 329 this.connectionManager.replaceFreeConnection(this); 333 if (debug) { 334 logger.finest("sqlstore.connectionimpl.close.replaced"); } 336 } 337 } 338 339 } catch (SQLException se) { 340 throw se; 341 } 342 } 343 344 348 protected void release() { 349 try { 350 this.connection.close(); 351 } catch (SQLException se) { 352 } 354 logger.finest("sqlstore.connectionimpl.close.connrelease"); } 356 357 public synchronized boolean isClosed() throws SQLException { 358 try { 359 return (this.connection.isClosed()); 360 } catch (SQLException se) { 361 throw se; 362 } 363 } 364 365 public synchronized DatabaseMetaData getMetaData() throws SQLException { 366 try { 367 return (this.connection.getMetaData()); 368 } catch (SQLException se) { 369 throw se; 370 } 371 } 372 373 public synchronized void setReadOnly(boolean readOnly) throws SQLException { 374 try { 375 this.connection.setReadOnly(readOnly); 376 } catch (SQLException se) { 377 throw se; 378 } 379 } 380 381 public synchronized boolean isReadOnly() throws SQLException { 382 try { 383 return (this.connection.isReadOnly()); 384 } catch (SQLException se) { 385 throw se; 386 } 387 } 388 389 public synchronized void setCatalog(String catalog) throws SQLException { 390 try { 391 this.connection.setCatalog(catalog); 392 } catch (SQLException se) { 393 throw se; 394 } 395 } 396 397 public synchronized String getCatalog() throws SQLException { 398 try { 399 return (this.connection.getCatalog()); 400 } catch (SQLException se) { 401 throw se; 402 } 403 } 404 405 public synchronized void setTransactionIsolation(int level) 406 throws SQLException { 407 try { 408 this.connection.setTransactionIsolation(level); 409 } catch (SQLException se) { 410 throw se; 411 } 412 } 413 414 public synchronized int getTransactionIsolation() throws SQLException { 415 try { 416 return (this.connection.getTransactionIsolation()); 417 } catch (SQLException se) { 418 throw se; 419 } 420 } 421 422 public synchronized SQLWarning getWarnings() throws SQLException { 423 try { 424 return (this.connection.getWarnings()); 425 } catch (SQLException se) { 426 throw se; 427 } 428 } 429 430 public synchronized void clearWarnings() throws SQLException { 431 try { 432 this.connection.clearWarnings(); 433 } catch (SQLException se) { 434 throw se; 435 } 436 } 437 438 public synchronized java.util.Map getTypeMap() 439 throws SQLException { 440 try { 441 return (this.connection.getTypeMap()); 442 } catch (SQLException se) { 443 throw se; 444 } 445 } 446 447 public synchronized void setTypeMap(java.util.Map map) 448 throws SQLException { 449 try { 450 this.connection.setTypeMap(map); 451 } catch (SQLException se) { 452 throw se; 453 } 454 } 455 456 458 public synchronized void setHoldability(int holdability) 459 throws SQLException { 460 461 throw new UnsupportedOperationException (); 462 } 463 464 public synchronized int getHoldability() 465 throws SQLException { 466 467 throw new UnsupportedOperationException (); 468 } 469 470 public synchronized Savepoint setSavepoint() 471 throws SQLException { 472 473 throw new UnsupportedOperationException (); 474 } 475 476 public synchronized Savepoint setSavepoint(String name) 477 throws SQLException { 478 479 throw new UnsupportedOperationException (); 480 } 481 482 public synchronized void rollback(Savepoint savepoint) 483 throws SQLException { 484 485 throw new UnsupportedOperationException (); 486 487 } 488 489 public synchronized void releaseSavepoint(Savepoint savepoint) 490 throws SQLException { 491 492 throw new UnsupportedOperationException (); 493 } 494 495 public synchronized Statement createStatement(int resultSetType, 496 int resultSetConcurrency, 497 int resultSetHoldability) 498 throws SQLException { 499 500 throw new UnsupportedOperationException (); 501 } 502 503 public synchronized PreparedStatement prepareStatement(String sql, 504 int resultSetType, 505 int resultSetConcurrency, 506 int resultSetHoldability) 507 throws SQLException { 508 509 throw new UnsupportedOperationException (); 510 } 511 512 public synchronized CallableStatement prepareCall(String sql, 513 int resultSetType, 514 int resultSetConcurrency, 515 int resultSetHoldability) 516 throws SQLException { 517 518 throw new UnsupportedOperationException (); 519 } 520 521 public synchronized PreparedStatement prepareStatement(String sql, 522 int autoGeneratedKeys) 523 throws SQLException { 524 525 throw new UnsupportedOperationException (); 526 } 527 528 public synchronized PreparedStatement prepareStatement(String sql, 529 int[] columnIndexes) 530 throws SQLException { 531 532 throw new UnsupportedOperationException (); 533 } 534 535 public synchronized PreparedStatement prepareStatement(String sql, 536 String [] columnNames) 537 throws SQLException { 538 throw new UnsupportedOperationException (); 539 } 540 541 543 545 560 synchronized void checkXact() throws SQLException { 561 617 } 618 619 624 synchronized void setFreePending(boolean freePending) { 625 this.freePending = freePending; 626 } 627 628 632 synchronized boolean getFreePending() { 633 return (this.freePending); 634 } 635 636 642 synchronized boolean xactPending() { 643 return ((this.transaction != null) ? true : false); 644 } 645 646 651 public Linkable getPrevious() { 652 return (this.previous); 653 } 654 655 660 public void setPrevious(Linkable conn) { 661 this.previous = conn; 662 } 663 664 669 public Linkable getNext() { 670 return (this.next); 671 } 672 673 678 public void setNext(Linkable conn) { 679 this.next = conn; 680 } 681 682 687 synchronized boolean getPooled() { 688 return (this.pooled); 689 } 690 691 695 synchronized void setPooled(boolean flag) { 696 this.pooled = flag; 697 } 698 699 704 synchronized String getURL() { 705 return this.url; 706 } 707 708 714 synchronized String getUserName() { 715 return this.userName; 716 } 717 718 725 public synchronized void internalCommit() { 726 logger.finest("sqlstore.connectionimpl.internalcommit"); 728 try { 729 this.connection.commit(); 730 } catch (Exception e1) { 731 try { 732 this.connection.rollback(); 733 } catch (Exception e2) { 734 } finally { 736 this.clearXact(); 737 } 738 throw new JDODataStoreException(null, e1); } finally { 740 this.clearXact(); 741 } 742 } 743 744 745 751 public synchronized void internalRollback() { 752 logger.finest("sqlstore.connectionimpl.internalrollback"); try { 754 this.connection.rollback(); 755 } catch (Exception e1) { 756 } finally { 758 this.clearXact(); 759 } 760 } 761 762 768 private void clearXact() { 769 logger.finest("sqlstore.connectionimpl.clearxact"); 771 try { 772 if (this.freePending) { 773 this.freePending = false; 774 if (this.pooled) { 775 this.connectionManager.disassociateXact(this.transaction, this, true); 776 logger.finest("sqlstore.connectionimpl.pendingdisassocxact"); } else { 778 this.connectionManager.disassociateXact(this.transaction, this, false); 779 this.connection.close(); 782 783 logger.finest("sqlstore.connectionimpl.clearxact.close"); } 785 } else { 786 this.connectionManager.disassociateXact(this.transaction, this, false); 787 logger.finest("sqlstore.connectionimpl.clearxact.disassocxact"); } 789 this.connection.setAutoCommit(true); 790 } catch (SQLException ex) { 791 } finally { 793 this.transaction = null; 795 } 796 } 797 798 804 public synchronized String toString() { 805 int xactIsolation = 0; 806 String buffer = "Connect@"; 808 String strTran = (this.transaction == null) ? 809 " NULL" : this.transaction.toString(); int hash = this.hashCode(); 811 812 try { 813 xactIsolation = this.getTransactionIsolation(); 814 } catch (SQLException ex) { 815 xactIsolation = -1; 816 } 817 818 buffer = buffer + hash + "\n" + " pooled = " + this.pooled + "\n" + " freePending = " + this.freePending + "\n" + " xactIsolation = " + xactIsolation + "\n" + " Tran = " + strTran + "\n"; 824 return buffer; 825 } 826 827 protected void finalize() { 828 try { 829 this.connection.close(); 830 logger.finest("sqlstore.connectionimpl.finalize"); } catch (SQLException se) { 832 ; 833 } 834 } 835 } 836 | Popular Tags |