Module: Mongoid::Clients::Sessions::ClassMethods
- Included in:
- Mongoid
- Defined in:
- lib/mongoid/clients/sessions.rb
Constant Summary collapse
- CALLBACK_ACTIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Actions that can be used to trigger transactional callbacks.
%i[create destroy update]
Instance Method Summary collapse
-
#after_commit(*args, &block) ⇒ Object
Sets up a callback is called after a commit of a transaction.
-
#after_create_commit(*args, &block) ⇒ Object
Shortcut for
after_commit :hook, on: :create. -
#after_destroy_commit(*args, &block) ⇒ Object
Shortcut for
after_commit :hook, on: :destroy. -
#after_rollback(*args, &block) ⇒ Object
This callback is called after a create, update, or destroy are rolled back.
-
#after_save_commit(*args, &block) ⇒ Object
Shortcut for
after_commit :hook, on: [ :create, :update ]. -
#after_update_commit(*args, &block) ⇒ Object
Shortcut for
after_commit :hook, on: :update. -
#transaction(options = {}, session_options: {}) { ... } ⇒ Object
Executes a block within the context of a transaction.
-
#with_session(options = {}) {|The| ... } ⇒ Object
Execute a block within the context of a session.
Instance Method Details
#after_commit(*args, &block) ⇒ Object
Sets up a callback is called after a commit of a transaction. The callback is called only if the document is created, updated, or destroyed in the transaction.
See ActiveSupport::Callbacks::ClassMethods::set_callback for more
information about method parameters and possible options.
108 109 110 111 |
# File 'lib/mongoid/clients/sessions.rb', line 108 def after_commit(*args, &block) (args) set_callback(:commit, :after, *args, &block) end |
#after_create_commit(*args, &block) ⇒ Object
Shortcut for after_commit :hook, on: :create.
120 121 122 123 |
# File 'lib/mongoid/clients/sessions.rb', line 120 def after_create_commit(*args, &block) (args, on: :create) set_callback(:commit, :after, *args, &block) end |
#after_destroy_commit(*args, &block) ⇒ Object
Shortcut for after_commit :hook, on: :destroy.
132 133 134 135 |
# File 'lib/mongoid/clients/sessions.rb', line 132 def after_destroy_commit(*args, &block) (args, on: :destroy) set_callback(:commit, :after, *args, &block) end |
#after_rollback(*args, &block) ⇒ Object
This callback is called after a create, update, or destroy are rolled back.
Please check the documentation of after_commit for options.
140 141 142 143 |
# File 'lib/mongoid/clients/sessions.rb', line 140 def after_rollback(*args, &block) (args) set_callback(:rollback, :after, *args, &block) end |
#after_save_commit(*args, &block) ⇒ Object
Shortcut for after_commit :hook, on: [ :create, :update ]
114 115 116 117 |
# File 'lib/mongoid/clients/sessions.rb', line 114 def after_save_commit(*args, &block) (args, on: %i[create update]) set_callback(:commit, :after, *args, &block) end |
#after_update_commit(*args, &block) ⇒ Object
Shortcut for after_commit :hook, on: :update.
126 127 128 129 |
# File 'lib/mongoid/clients/sessions.rb', line 126 def after_update_commit(*args, &block) (args, on: :update) set_callback(:commit, :after, *args, &block) end |
#transaction(options = {}, session_options: {}) { ... } ⇒ Object
Executes a block within the context of a transaction.
If the block does not raise an error, the transaction is committed.
If an error is raised, the transaction is aborted. The error is passed on
except for the Mongoid::Errors::Rollback. This error is not passed on,
so you can raise is if you want to deliberately rollback the transaction.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mongoid/clients/sessions.rb', line 83 def transaction( = {}, session_options: {}, &block) with_session() do |session| session.with_transaction(, &block).tap { run_commit_callbacks(session) } rescue *transactions_not_supported_exceptions raise Mongoid::Errors::TransactionsNotSupported rescue Mongoid::Errors::Rollback run_abort_callbacks(session) rescue Mongoid::Errors::InvalidSessionNesting # Session should be ended here. raise Mongoid::Errors::InvalidTransactionNesting.new rescue Mongo::Error::InvalidSession, Mongo::Error::InvalidTransactionOperation => e run_abort_callbacks(session) raise Mongoid::Errors::TransactionError.new(e) rescue StandardError => e run_abort_callbacks(session) raise e end end |
#with_session(options = {}) {|The| ... } ⇒ Object
Execute a block within the context of a session.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongoid/clients/sessions.rb', line 38 def with_session( = {}) raise Mongoid::Errors::InvalidSessionNesting.new if Threaded.get_session(client: persistence_context.client) session = persistence_context.client.start_session() Threaded.set_session(session, client: persistence_context.client) yield(session) rescue Mongo::Error::InvalidSession => e raise Mongoid::Errors::SessionsNotSupported.new if e.is_a?(Mongo::Error::SessionsNotSupported) raise e rescue Mongo::Error::OperationFailure => e if (e.code == 40_415 && e. =~ /startTransaction/) || (e.code == 20 && e. =~ /Transaction/) raise Mongoid::Errors::TransactionsNotSupported.new else raise e end rescue *transactions_not_supported_exceptions raise Mongoid::Errors::TransactionsNotSupported ensure Threaded.clear_modified_documents(session) Threaded.clear_session(client: persistence_context.client) end |