Ruby TCP server - ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections
Ruby TCP server - ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections
我已经编写了一个从终端设备接收数据包的 tcp 服务器。 TCP服务器解释数据并使用postgres将其保存在数据库中。
tcp 服务器是多线程的。我打开数据库连接并保存数据时的代码示例如下所示;
conn = Sequel.connect('postgres://xxxxx:xxxxxxxxxx@127.0.0.1:xxxxx/xxxxxxxxx',:max_connections => 100) # requires pg
transactions = conn.from(:transactions)
if transactions.insert(serial_number: card_serial, balance_before: balance_before, amount: transaction_amount, balance_after: balance_after, transaction_time: time, terminal_number: terminal_number, terminal_type: terminal_type, created_at: Time.now, updated_at: Time.now)
response = {message: "TT01000080", status: "SUCCESS" }
return response
else
response = {message: "", status: "FAILED" }
return response
end
几个数据包后,数据库会产生这样的错误;
ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections
即使添加行 conn.disconnect
也无助于解决问题。
每次要创建记录时都连接到数据库的想法并不是最好的方法。考虑将其更改为一些连接池。
但是如果你还想每次都连接,尝试使用block形式的connect,这样可以保证block结束后连接关闭。
你的代码中也没有close/disconnect,也许你把它放在return
调用之后(这意味着它没有被执行)
尝试这样的事情:
response = nil
Sequel.connect('postgres://xxxxx:xxxxxxxxxx@127.0.0.1:xxxxx/xxxxxxxxx',:max_connections => 100) do |conn| # requires pg
transactions = conn.from(:transactions)
if transactions.insert(serial_number: card_serial, balance_before: balance_before, amount: transaction_amount, balance_after: balance_after, transaction_time: time, terminal_number: terminal_number, terminal_type: terminal_type, created_at: Time.now, updated_at: Time.now)
response = {message: "TT01000080", status: "SUCCESS" }
else
response = {message: "", status: "FAILED" }
end
end
return response
我已经编写了一个从终端设备接收数据包的 tcp 服务器。 TCP服务器解释数据并使用postgres将其保存在数据库中。
tcp 服务器是多线程的。我打开数据库连接并保存数据时的代码示例如下所示;
conn = Sequel.connect('postgres://xxxxx:xxxxxxxxxx@127.0.0.1:xxxxx/xxxxxxxxx',:max_connections => 100) # requires pg
transactions = conn.from(:transactions)
if transactions.insert(serial_number: card_serial, balance_before: balance_before, amount: transaction_amount, balance_after: balance_after, transaction_time: time, terminal_number: terminal_number, terminal_type: terminal_type, created_at: Time.now, updated_at: Time.now)
response = {message: "TT01000080", status: "SUCCESS" }
return response
else
response = {message: "", status: "FAILED" }
return response
end
几个数据包后,数据库会产生这样的错误;
ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections
即使添加行 conn.disconnect
也无助于解决问题。
每次要创建记录时都连接到数据库的想法并不是最好的方法。考虑将其更改为一些连接池。
但是如果你还想每次都连接,尝试使用block形式的connect,这样可以保证block结束后连接关闭。
你的代码中也没有close/disconnect,也许你把它放在return
调用之后(这意味着它没有被执行)
尝试这样的事情:
response = nil
Sequel.connect('postgres://xxxxx:xxxxxxxxxx@127.0.0.1:xxxxx/xxxxxxxxx',:max_connections => 100) do |conn| # requires pg
transactions = conn.from(:transactions)
if transactions.insert(serial_number: card_serial, balance_before: balance_before, amount: transaction_amount, balance_after: balance_after, transaction_time: time, terminal_number: terminal_number, terminal_type: terminal_type, created_at: Time.now, updated_at: Time.now)
response = {message: "TT01000080", status: "SUCCESS" }
else
response = {message: "", status: "FAILED" }
end
end
return response