1 21 22 package org.opensubsystems.core.persist.db.transaction; 23 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.PreparedStatement ; 28 import java.sql.SQLException ; 29 import java.sql.SQLWarning ; 30 import java.sql.Savepoint ; 31 import java.sql.Statement ; 32 import java.util.Map ; 33 34 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 35 import org.opensubsystems.core.util.GlobalConstants; 36 37 47 public class TransactionalConnection implements Connection 48 { 49 51 54 private Connection m_privateConnection; 55 56 59 private String m_strDataSourceName; 60 61 64 private String m_strUser; 65 66 69 private String m_strPassword; 70 71 76 private boolean m_bUsed; 77 78 84 private int m_iActiveCount; 85 86 91 private boolean m_bInTransaction; 92 93 96 private DatabaseConnectionFactoryImpl m_connectionFactory; 97 98 100 111 protected TransactionalConnection( 112 Connection privateConnection, 113 String strDataSourceName, 114 String strUser, 115 String strPassword, 116 boolean inTransaction, 117 DatabaseConnectionFactoryImpl connectionFactory 118 ) 119 { 120 super(); 121 122 m_privateConnection = privateConnection; 123 m_strDataSourceName = strDataSourceName; 124 m_strUser = strUser; 125 m_strPassword = strPassword; 126 m_bInTransaction = inTransaction; 127 m_connectionFactory = connectionFactory; 128 m_bUsed = false; m_iActiveCount = 1; } 131 132 134 140 boolean isActive() 141 { 142 return (m_iActiveCount > 0); 143 } 144 145 151 void setActive( 152 boolean active 153 ) 154 { 155 if (active) 156 { 157 m_iActiveCount++; 158 } 159 else 160 { 161 m_iActiveCount--; 162 if (GlobalConstants.ERROR_CHECKING) 163 { 164 assert m_iActiveCount >= 0 : "Connection was returned too many times."; 165 } 166 } 167 } 168 169 175 boolean isInTransaction() 176 { 177 return m_bInTransaction; 178 } 179 180 188 void setInTransaction( 189 boolean inTransaction 190 ) throws SQLException 191 { 192 if ((inTransaction) && (m_privateConnection.getAutoCommit())) 193 { 194 m_privateConnection.setAutoCommit(false); 197 } 198 m_bInTransaction = inTransaction; 199 } 200 201 207 public boolean isUsed() 208 { 209 return m_bUsed; 210 } 211 212 218 void setUsed( 219 boolean used 220 ) throws SQLException 221 { 222 if ((m_iActiveCount == 0) && (used)) 223 { 224 throw new SQLException ("This connection was already returned to the pool " + 225 " and shouldn't be used anymore."); 226 } 227 m_bUsed = used; 228 } 229 230 233 public String getDataSourceName() 234 { 235 return m_strDataSourceName; 236 } 237 238 241 public String getPassword() 242 { 243 return m_strPassword; 244 } 245 246 249 public String getUser() 250 { 251 return m_strUser; 252 } 253 254 260 public boolean verifyConnection( 261 Connection verifiableConnection 262 ) 263 { 264 return verifiableConnection == m_privateConnection; 266 } 267 268 271 public DatabaseConnectionFactoryImpl getConnectionFactory() 272 { 273 return m_connectionFactory; 274 } 275 276 278 281 public void clearWarnings( 282 ) throws SQLException 283 { 284 setUsed(true); 285 m_privateConnection.clearWarnings(); 286 } 287 288 291 public void close( 292 ) throws SQLException 293 { 294 throw new SQLException ("Connection is not supposed to be closed." + 295 " It should be returned to database connection factory."); 296 } 299 300 303 public void commit( 304 ) throws SQLException 305 { 306 if (m_bInTransaction) 307 { 308 throw new SQLException ("Connection in transaction cannot be commited."); 309 } 310 setUsed(false); 313 m_privateConnection.commit(); 314 } 315 316 319 public Statement createStatement( 320 ) throws SQLException 321 { 322 setUsed(true); 323 return m_privateConnection.createStatement(); 324 } 325 326 329 public Statement createStatement( 330 int resultSetType, 331 int resultSetConcurrency 332 ) throws SQLException 333 { 334 setUsed(true); 335 return m_privateConnection.createStatement(resultSetType, resultSetConcurrency); 336 } 337 338 341 public Statement createStatement( 342 int resultSetType, 343 int resultSetConcurrency, 344 int resultSetHoldability 345 ) throws SQLException 346 { 347 setUsed(true); 348 return m_privateConnection.createStatement(resultSetType, resultSetConcurrency, 349 resultSetHoldability); 350 } 351 352 355 public boolean equals( 356 Object obj 357 ) 358 { 359 return m_privateConnection.equals(obj); 362 } 363 364 367 public boolean getAutoCommit( 368 ) throws SQLException 369 { 370 setUsed(true); 371 return m_privateConnection.getAutoCommit(); 372 } 373 374 377 public String getCatalog( 378 ) throws SQLException 379 { 380 setUsed(true); 381 return m_privateConnection.getCatalog(); 382 } 383 384 387 public int getHoldability( 388 ) throws SQLException 389 { 390 setUsed(true); 391 return m_privateConnection.getHoldability(); 392 } 393 394 397 public DatabaseMetaData getMetaData( 398 ) throws SQLException 399 { 400 setUsed(true); 401 return m_privateConnection.getMetaData(); 402 } 403 404 407 public int getTransactionIsolation( 408 ) throws SQLException 409 { 410 setUsed(true); 411 return m_privateConnection.getTransactionIsolation(); 412 } 413 414 417 public Map getTypeMap( 418 ) throws SQLException 419 { 420 setUsed(true); 421 return m_privateConnection.getTypeMap(); 422 } 423 424 427 public SQLWarning getWarnings( 428 ) throws SQLException 429 { 430 setUsed(true); 431 return m_privateConnection.getWarnings(); 432 } 433 434 437 public int hashCode() 438 { 439 return m_privateConnection.hashCode(); 442 } 443 444 447 public boolean isClosed( 448 ) throws SQLException 449 { 450 setUsed(true); 451 return m_privateConnection.isClosed(); 452 } 453 454 457 public boolean isReadOnly( 458 ) throws SQLException 459 { 460 setUsed(true); 461 return m_privateConnection.isReadOnly(); 462 } 463 464 467 public String nativeSQL( 468 String sql 469 ) throws SQLException 470 { 471 setUsed(true); 472 return m_privateConnection.nativeSQL(sql); 473 } 474 475 478 public CallableStatement prepareCall( 479 String sql 480 ) throws SQLException 481 { 482 setUsed(true); 483 return m_privateConnection.prepareCall(sql); 484 } 485 486 489 public CallableStatement prepareCall( 490 String sql, 491 int resultSetType, 492 int resultSetConcurrency 493 ) throws SQLException 494 { 495 setUsed(true); 496 return m_privateConnection.prepareCall(sql, resultSetType, resultSetConcurrency); 497 } 498 499 502 public CallableStatement prepareCall( 503 String sql, 504 int resultSetType, 505 int resultSetConcurrency, 506 int resultSetHoldability 507 ) throws SQLException 508 { 509 setUsed(true); 510 return m_privateConnection.prepareCall(sql, resultSetType, resultSetConcurrency, 511 resultSetHoldability); 512 } 513 514 517 public PreparedStatement prepareStatement( 518 String sql 519 ) throws SQLException 520 { 521 setUsed(true); 522 return m_privateConnection.prepareStatement(sql); 523 } 524 525 528 public PreparedStatement prepareStatement( 529 String sql, 530 int autoGeneratedKeys 531 ) throws SQLException 532 { 533 setUsed(true); 534 return m_privateConnection.prepareStatement(sql, autoGeneratedKeys); 535 } 536 537 540 public PreparedStatement prepareStatement( 541 String sql, 542 int resultSetType, 543 int resultSetConcurrency 544 ) throws SQLException 545 { 546 setUsed(true); 547 return m_privateConnection.prepareStatement(sql, resultSetType, resultSetConcurrency); 548 } 549 550 553 public PreparedStatement prepareStatement( 554 String sql, 555 int resultSetType, 556 int resultSetConcurrency, 557 int resultSetHoldability 558 ) throws SQLException 559 { 560 setUsed(true); 561 return m_privateConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, 562 resultSetHoldability); 563 } 564 565 568 public PreparedStatement prepareStatement( 569 String sql, 570 int[] columnIndexes 571 ) throws SQLException 572 { 573 setUsed(true); 574 return m_privateConnection.prepareStatement(sql, columnIndexes); 575 } 576 577 580 public PreparedStatement prepareStatement( 581 String sql, String [] columnNames 582 ) throws SQLException 583 { 584 setUsed(true); 585 return m_privateConnection.prepareStatement(sql, columnNames); 586 } 587 588 591 public void releaseSavepoint( 592 Savepoint savepoint 593 ) throws SQLException 594 { 595 setUsed(true); 596 m_privateConnection.releaseSavepoint(savepoint); 597 } 598 599 602 public void rollback( 603 ) throws SQLException 604 { 605 if (m_bInTransaction) 606 { 607 throw new SQLException ("Connection in transaction cannot be rollbacked."); 608 } 609 setUsed(false); 612 m_privateConnection.rollback(); 613 } 614 615 618 public void rollback( 619 Savepoint savepoint 620 ) throws SQLException 621 { 622 setUsed(true); 623 m_privateConnection.rollback(savepoint); 624 } 625 626 629 public void setAutoCommit( 630 boolean autoCommit 631 ) throws SQLException 632 { 633 if ((m_bInTransaction) && (autoCommit)) 634 { 635 throw new SQLException ("Connection in transaction cannot be set to autocommit."); 636 } 637 setUsed(true); 638 m_privateConnection.setAutoCommit(autoCommit); 639 } 640 641 644 public void setCatalog( 645 String catalog 646 ) throws SQLException 647 { 648 setUsed(true); 649 m_privateConnection.setCatalog(catalog); 650 } 651 652 655 public void setHoldability( 656 int holdability 657 ) throws SQLException 658 { 659 setUsed(true); 660 m_privateConnection.setHoldability(holdability); 661 } 662 663 666 public void setReadOnly( 667 boolean readOnly 668 ) throws SQLException 669 { 670 setUsed(true); 671 m_privateConnection.setReadOnly(readOnly); 672 } 673 674 677 public Savepoint setSavepoint( 678 ) throws SQLException 679 { 680 setUsed(true); 681 return m_privateConnection.setSavepoint(); 682 } 683 684 687 public Savepoint setSavepoint( 688 String name 689 ) throws SQLException 690 { 691 setUsed(true); 692 return m_privateConnection.setSavepoint(name); 693 } 694 695 698 public void setTransactionIsolation( 699 int level 700 ) throws SQLException 701 { 702 setUsed(true); 703 m_privateConnection.setTransactionIsolation(level); 704 } 705 706 709 public void setTypeMap( 710 Map map 711 ) throws SQLException 712 { 713 setUsed(true); 714 m_privateConnection.setTypeMap(map); 715 } 716 717 720 public String toString() 721 { 722 return m_privateConnection.toString(); 725 } 726 727 729 } 889 | Popular Tags |