【发布时间】:2016-04-18 00:38:25
【问题描述】:
我们有 Postgres 作为我们的后端数据库。我们的进程运行一些工作(即它在数据库中进行一些插入/更新),然后休眠一个小时。
这是我们注意到的。当我们的进程处于休眠状态时,我们的 Postgres 连接状态被视为空闲。
postgres 5045 0.3 0.4 231220 33780 ? Ss 12:13 0:16 postgres: scp scp_test x.x.x.x(60400) idle
现在我的问题是?
如果,我有一个睡眠一个小时的进程。
Postgres 是否会在一段时间后关闭空闲连接?
因为在下一次运行过程中无法插入/更新数据库中的任何记录。
我的代码如下所示。
$logger = Logger.new('log/checker.log')
last_modified = Time.now
while
if (last_modified == File.mtime(file_path))
$logger.info "Sleeping for 1 hour"
sleep 3600
else
$logger.info "Inserting the file mtime changed .."
last_modified = File.mtime(file_path)
$logger.info "File modified ....."
attributes = Test.testFile(file_path)
index = 0
$logger.info "........Index set......"
header_attributes = attributes.shift
$logger.info "...........Header removed..........."
trailer_attributes = attributes.pop
$logger.info "...Trailer removed......."
count = attributes.count
$logger.info "............Count calculated #{count} ........."
attributes.each_slice(50000) { |records|
_records = initialize_records(records)
_records.each { |record|
record.save
index += 1
$logger.info "Inserting ...... [#{index}/#{count}]"
}
}
$logger.info "Completed insertion of #{count}"
end
end
用
测试了这个Ruby-2.2.2 - ActiveRecord-4.2.6 - pg-0.18.0
Ruby-2.3.0 - ActiveRecord-4.2.6 - pg-0.18.0
Jruby-9.0.5.0 - ActiveRecord-4.2.6 - activerecord-jdbc-adapter
Postgres 版本。
PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
Ruby 和 JRuby 输出之间有 1 个区别?
虽然两个进程从睡眠中醒来后都卡住了
Ruby 进程因PG::UnableToSend: SSL SYSCALL error: EOF detected 错误而终止
但是 JRuby 进程永远卡住了(不会死)。
不确定问题出在哪里,因为目前我无法确定任何特定的库或代码。
注意:在环回接口上工作得非常好。有问题的 postgres 服务器是远程服务器..
【问题讨论】:
标签: ruby postgresql activerecord jruby ruby-2.3