模块:Mongoid::Clients::Sessions::ClassMethods
- 包含在:
- Mongoid
- 定义于:
- lib/mongoid/clients/sessions.rb
常量摘要折叠
- CALLBACK_ACTIONS =
此常量是私有 API 的一部分。 应尽可能避免使用此常量,因为它将来可能会被删除或更改。
可用于trigger事务回调的操作。
[:create, :destroy, :update]
实例方法摘要折叠
-
# after_commit (*args, &block) ⇒ 对象
设置在提交ACID 事务后调用的回调。
-
# after_create_commit (*args, &block) ⇒ 对象
after_commit :hook, on: :create的快捷方式。
-
# after_destroy_commit (*args, &block) ⇒ 对象
after_commit :hook, on: :destroy的快捷方式。
-
# after_rollback (*args, &block) ⇒ 对象
在创建、更新或销毁回滚后调用此回调。
-
# after_save_commit (*args, &block) ⇒ 对象
after_commit :hook, on: [ :create, : 更新 ]的快捷方式。
-
# after_update_commit (*args, &block) ⇒ 对象
after_commit :hook, on: : 更新的快捷方式。
-
# ACID 事务 (options = {}, session_options: {}) { ... } ⇒ 对象
在ACID 事务上下文中执行区块。
-
# with_session (options = {}) {|The| ... } ⇒ 对象
在会话上下文中执行区块。
实例方法详细信息
# after_commit (*args, &block) ⇒对象
设置在提交ACID 事务后调用的回调。 仅当在ACID 事务中创建、更新或销毁文档时才会回调。
有关方法参数和可能选项的更多信息,请参阅ActiveSupport::Callbacks::ClassMethods::set_callback
。
119 120 121 122 |
# File 'lib/mongoid/clients/sessions.rb', line 119 def after_commit(*args, 和块) (args) set_callback(:提交, :after, *args, 和块) end |
# after_create_commit (*args, &block) ⇒对象
after_commit :hook, on: :create的快捷方式。
131 132 133 134 |
# File 'lib/mongoid/clients/sessions.rb', line 131 def after_create_commit(*args, 和块) (args, 于: :create) set_callback(:提交, :after, *args, 和块) end |
# after_destroy_commit (*args, &block) ⇒对象
after_commit :hook, on: :destroy的快捷方式。
143 144 145 146 |
# File 'lib/mongoid/clients/sessions.rb', line 143 def after_destroy_commit(*args, 和块) (args, 于: :destroy) set_callback(:提交, :after, *args, 和块) end |
# after_rollback (*args, &block) ⇒对象
在创建、更新或销毁回滚后调用此回调。
请查看after_commit
的文档以了解选项。
151 152 153 154 |
# File 'lib/mongoid/clients/sessions.rb', line 151 def after_rollback(*args, 和块) (args) set_callback(:rollback, :after, *args, 和块) end |
# after_save_commit (*args, &block) ⇒对象
after_commit :hook, on: [ :create, : 更新 ]的快捷方式
125 126 127 128 |
# File 'lib/mongoid/clients/sessions.rb', line 125 def after_save_commit(*args, 和块) (args, 于: [ :create, :update ]) set_callback(:提交, :after, *args, 和块) end |
# after_update_commit (*args, &block) ⇒对象
after_commit :hook, on: : 更新的快捷方式。
137 138 139 140 |
# File 'lib/mongoid/clients/sessions.rb', line 137 def after_update_commit(*args, 和块) (args, 于: :update) set_callback(:提交, :after, *args, 和块) end |
# ACID 事务 (options = {}, session_options: {}) { ... } ⇒对象
在ACID 事务上下文中执行区块。
如果区块没有引发错误,则提交ACID 事务。 如果出现错误,则ACID 事务将中止。 除 'Mongoid::Errors::Rollback' 之外的错误都会传递。 此错误不会传递,因此如果要故意回滚ACID 事务,可以引发 is 。
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mongoid/clients/sessions.rb', line 90 def 事务( = {}, session_options: {}) with_session() do |会话| 开始 会话.with_transaction() do 产量 end.点击 { run_commit_callbacks(会话) } 救援 *transactions_not_supported_Exceptions 提高 Mongoid::Errors::TransactionsNotSupported 救援 Mongoid::Errors::Rollback run_abort_callbacks(会话) 救援 Mongoid::Errors::InvalidSessionNesting # 会话应在此处结束。 提高 Mongoid::Errors::InvalidTransactionNesting.new 救援 mongo::错误::无效会话, mongo::错误::InvalidTransactionOperation => e run_abort_callbacks(会话) 提高 Mongoid::Errors::TransactionError.new(e) 救援 StandardError => e run_abort_callbacks(会话) 提高 e end end end |
# with_session (options = {}) {|The| ... } ⇒对象
在会话上下文中执行区块。
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mongoid/clients/sessions.rb', line 42 def with_session( = {}) if 线程化.get_session(客户端: persistence_context.客户端) 提高 Mongoid::Errors::InvalidSessionNesting.new end 会话 = persistence_context.客户端.start_session() 线程化.set_session(会话, 客户端: persistence_context.客户端) 产量(会话) 救援 mongo::错误::无效会话 => ex if mongo::错误::SessionsNotSupported === ex 提高 Mongoid::Errors::SessionsNotSupported.new else 提高 ex end 救援 mongo::错误::OperationFailure => ex if (ex.代码 == 40415 && ex. =~ /startTransaction/) || (ex.代码 == 20 && ex. =~ /事务/) 提高 Mongoid::Errors::TransactionsNotSupported.new else 提高 ex end 救援 *transactions_not_supported_Exceptions 提高 Mongoid::Errors::TransactionsNotSupported 确保 线程化.clear_modified_documents(会话) 线程化.clear_session(客户端: persistence_context.客户端) end |