Class: ActiveRecord::ConnectionAdapters::TransactionManager
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::TransactionManager
- Defined in:
- lib/active_record/connection_adapters/abstract/transaction.rb
Overview
:nodoc:
Instance Method Summary collapse
- #begin_transaction(isolation: nil, joinable: true, _lazy: true) ⇒ Object
- #commit_transaction ⇒ Object
- #current_transaction ⇒ Object
- #dirty_current_transaction ⇒ Object
- #disable_lazy_transactions! ⇒ Object
- #enable_lazy_transactions! ⇒ Object
-
#initialize(connection) ⇒ TransactionManager
constructor
A new instance of TransactionManager.
- #lazy_transactions_enabled? ⇒ Boolean
- #materialize_transactions ⇒ Object
- #open_transactions ⇒ Object
- #restorable? ⇒ Boolean
- #restore_transactions ⇒ Object
- #rollback_transaction(transaction = nil) ⇒ Object
- #within_new_transaction(isolation: nil, joinable: true) ⇒ Object
Constructor Details
#initialize(connection) ⇒ TransactionManager
Returns a new instance of TransactionManager.
488 489 490 491 492 493 494 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 488 def initialize(connection) @stack = [] @connection = connection @has_unmaterialized_transactions = false @materializing_transactions = false @lazy_transactions_enabled = true end |
Instance Method Details
#begin_transaction(isolation: nil, joinable: true, _lazy: true) ⇒ Object
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 496 def begin_transaction(isolation: nil, joinable: true, _lazy: true) @connection.lock.synchronize do run_commit_callbacks = !current_transaction.joinable? transaction = if @stack.empty? RealTransaction.new( @connection, isolation: isolation, joinable: joinable, run_commit_callbacks: run_commit_callbacks ) elsif current_transaction.restartable? RestartParentTransaction.new( @connection, current_transaction, isolation: isolation, joinable: joinable, run_commit_callbacks: run_commit_callbacks ) else SavepointTransaction.new( @connection, "active_record_#{@stack.size}", current_transaction, isolation: isolation, joinable: joinable, run_commit_callbacks: run_commit_callbacks ) end unless transaction.materialized? if @connection.supports_lazy_transactions? && lazy_transactions_enabled? && _lazy @has_unmaterialized_transactions = true else transaction.materialize! end end @stack.push(transaction) transaction end end |
#commit_transaction ⇒ Object
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 583 def commit_transaction @connection.lock.synchronize do transaction = @stack.last begin transaction.before_commit_records ensure @stack.pop end dirty_current_transaction if transaction.dirty? transaction.commit transaction.commit_records end end |
#current_transaction ⇒ Object
651 652 653 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 651 def current_transaction @stack.last || NULL_TRANSACTION end |
#dirty_current_transaction ⇒ Object
551 552 553 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 551 def dirty_current_transaction current_transaction.dirty! end |
#disable_lazy_transactions! ⇒ Object
538 539 540 541 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 538 def disable_lazy_transactions! materialize_transactions @lazy_transactions_enabled = false end |
#enable_lazy_transactions! ⇒ Object
543 544 545 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 543 def enable_lazy_transactions! @lazy_transactions_enabled = true end |
#lazy_transactions_enabled? ⇒ Boolean
547 548 549 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 547 def lazy_transactions_enabled? @lazy_transactions_enabled end |
#materialize_transactions ⇒ Object
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 567 def materialize_transactions return if @materializing_transactions if @has_unmaterialized_transactions @connection.lock.synchronize do begin @materializing_transactions = true @stack.each { |t| t.materialize! unless t.materialized? } ensure @materializing_transactions = false end @has_unmaterialized_transactions = false end end end |
#open_transactions ⇒ Object
647 648 649 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 647 def open_transactions @stack.size end |
#restorable? ⇒ Boolean
563 564 565 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 563 def restorable? @stack.none?(&:dirty?) end |
#restore_transactions ⇒ Object
555 556 557 558 559 560 561 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 555 def restore_transactions return false unless restorable? @stack.each(&:restore!) true end |
#rollback_transaction(transaction = nil) ⇒ Object
600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 600 def rollback_transaction(transaction = nil) @connection.lock.synchronize do transaction ||= @stack.last begin transaction.rollback ensure @stack.pop if @stack.last == transaction end transaction.rollback_records end end |
#within_new_transaction(isolation: nil, joinable: true) ⇒ Object
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/active_record/connection_adapters/abstract/transaction.rb', line 612 def within_new_transaction(isolation: nil, joinable: true) @connection.lock.synchronize do transaction = begin_transaction(isolation: isolation, joinable: joinable) begin yield transaction.user_transaction rescue Exception => error rollback_transaction after_failure_actions(transaction, error) raise ensure unless error if Thread.current.status == "aborting" rollback_transaction else begin commit_transaction rescue ActiveRecord::ConnectionFailed transaction.invalidate! unless transaction.state.completed? raise rescue Exception rollback_transaction(transaction) unless transaction.state.completed? raise end end end end ensure unless transaction&.state&.completed? @connection.throw_away! transaction&.incomplete! end end end |