Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
72d959d9
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
72d959d9
编写于
6月 07, 2008
作者:
N
Nick Sieger
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Split connection handler into single- and multiple-thread versions.
上级
ff97e9d0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
57 addition
and
31 deletion
+57
-31
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
...ve_record/connection_adapters/abstract/connection_pool.rb
+40
-26
activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
.../connection_adapters/abstract/connection_specification.rb
+17
-5
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
浏览文件 @
72d959d9
...
...
@@ -134,18 +134,17 @@ def clear_entries!(cache, keys, &block)
end
end
class
ConnectionHandler
attr_reader
:connection_pools_lock
module
ConnectionHandlerMethods
def
initialize
(
pools
=
{})
@connection_pools
=
pools
end
def
initialize
@connection_pools
=
{}
@connection_pools_lock
=
Monitor
.
new
def
connection_pools
@connection_pools
||=
{}
end
def
establish_connection
(
name
,
spec
)
connection_pools_lock
.
synchronize
do
@connection_pools
[
name
]
=
ConnectionAdapters
::
ConnectionPool
.
new
(
spec
)
end
@connection_pools
[
name
]
=
ConnectionAdapters
::
ConnectionPool
.
new
(
spec
)
end
# for internal use only and for testing
...
...
@@ -168,7 +167,7 @@ def clear_reloadable_connections!
end
def
clear_all_connections!
clear_cache!
(
@connection_pools
)
{
|
name
,
pool
|
pool
.
disconnect!
}
@connection_pools
.
each_value
{
|
pool
|
pool
.
disconnect!
}
end
# Verify active connections.
...
...
@@ -185,15 +184,6 @@ def retrieve_connection(klass) #:nodoc:
(
pool
&&
pool
.
connection
)
or
raise
ConnectionNotEstablished
end
def
retrieve_connection_pool
(
klass
)
loop
do
pool
=
@connection_pools
[
klass
.
name
]
return
pool
if
pool
return
nil
if
ActiveRecord
::
Base
==
klass
klass
=
klass
.
superclass
end
end
# Returns true if a connection that's accessible to this class has already been opened.
def
connected?
(
klass
)
retrieve_connection_pool
(
klass
).
connected?
...
...
@@ -210,16 +200,40 @@ def remove_connection(klass)
pool
.
spec
.
config
if
pool
end
synchronize
:retrieve_connection
,
:retrieve_connection_pool
,
:connected?
,
:remove_connection
,
:active_connections
,
:clear_active_connections!
,
:clear_reloadable_connections!
,
:clear_all_connections!
,
:verify_active_connections!
,
:with
=>
:connection_pools_lock
private
def
clear_cache!
(
cache
,
&
block
)
cache
.
each
(
&
block
)
if
block_given?
cache
.
clear
def
retrieve_connection_pool
(
klass
)
loop
do
pool
=
@connection_pools
[
klass
.
name
]
return
pool
if
pool
return
nil
if
ActiveRecord
::
Base
==
klass
klass
=
klass
.
superclass
end
end
end
# This connection handler is not thread-safe, as it does not protect access
# to the underlying connection pools.
class
SingleThreadConnectionHandler
include
ConnectionHandlerMethods
end
# This connection handler is thread-safe. Each access or modification of a thread
# pool is synchronized by an internal monitor.
class
MultipleThreadConnectionHandler
attr_reader
:connection_pools_lock
include
ConnectionHandlerMethods
def
initialize
(
pools
=
{})
super
@connection_pools_lock
=
Monitor
.
new
end
# Apply monitor to all public methods that access the pool.
synchronize
:establish_connection
,
:retrieve_connection
,
:connected?
,
:remove_connection
,
:active_connections
,
:clear_active_connections!
,
:clear_reloadable_connections!
,
:clear_all_connections!
,
:verify_active_connections!
,
:with
=>
:connection_pools_lock
end
end
end
\ No newline at end of file
activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
浏览文件 @
72d959d9
...
...
@@ -14,12 +14,24 @@ def initialize (config, adapter_method)
# The connection handler
cattr_accessor
:connection_handler
,
:instance_writer
=>
false
@@connection_handler
=
ConnectionAdapters
::
ConnectionHandler
.
new
@@connection_handler
=
ConnectionAdapters
::
SingleThread
ConnectionHandler
.
new
# Turning on allow_concurrency basically switches a null mutex for a real one, so that
# multi-threaded access of the connection pools hash is synchronized.
# Turning on allow_concurrency changes the single threaded connection handler
# for a multiple threaded one, so that multi-threaded access of the
# connection pools is synchronized.
def
self
.
allow_concurrency
=
(
flag
)
@@allow_concurrency
=
flag
if
@@allow_concurrency
!=
flag
@@allow_concurrency
=
flag
# When switching connection handlers, preserve the existing pools so that
# #establish_connection doesn't need to be called again.
if
@@allow_concurrency
self
.
connection_handler
=
ConnectionAdapters
::
MultipleThreadConnectionHandler
.
new
(
self
.
connection_handler
.
connection_pools
)
else
self
.
connection_handler
=
ConnectionAdapters
::
SingleThreadConnectionHandler
.
new
(
self
.
connection_handler
.
connection_pools
)
end
end
end
# Returns the connection currently associated with the class. This can
...
...
@@ -112,7 +124,7 @@ def connected?
connection_handler
.
connected?
(
self
)
end
def
remove_connection
(
klass
=
self
)
def
remove_connection
(
klass
=
self
)
connection_handler
.
remove_connection
(
klass
)
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录