Ruby TCP server - ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections

Acacia

I have written a tcp server that receives packets from a terminal device. The TCP server interprets the data and saves it in the database using postgres.

The tcp server is multi-threaded. A sample of the code of the moment i open the db connection and save data looks like this;

conn = Sequel.connect('postgres://xxxxx:[email protected]: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

After a few packets, the db creates an error like this; ERROR:PG::ConnectionBad: FATAL: remaining connection slots are reserved for non-replication superuser connections

Even adding the line conn.disconnect does not help the issue.

Stan Brajewski

The idea to connect to DB every time you want to create a record is not the best approach. Think about changing it to some Connection Pool.

But if you still want to connect every time, try to use the block form of connect, which will assure connection is closed after block finishes.

Also there was no close/disconnect in your code, maybe you put it after return call (which means it was not executed)

Try something like this:

response = nil
Sequel.connect('postgres://xxxxx:[email protected]: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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django/Postgres: FATAL: remaining connection slots are reserved for non-replication superuser connections

From Dev

org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections

From Dev

AWS RDS PostgreSQL error "remaining connection slots are reserved for non-replication superuser connections"

From Java

PG::ConnectionBad - could not connect to server: Connection refused

From Dev

PG::ConnectionBad: could not connect to server: Connection refused

From Dev

PG::ConnectionBad: could not connect to server: Connection refused

From Dev

Rails - Postgres - could not connect to server: Connection refused (PG::ConnectionBad)

From Dev

Heroku: PG::ConnectionBad: could not connect to server: Connection refused

From Dev

PG::ConnectionBad error

From Dev

PG::ConnectionBad FATAL: role "Myname" does not exist

From Dev

PG::ConnectionBad FATAL: role "Myname" does not exist

From Dev

Switched to PG! DB migration error: PG::ConnectionBad:

From Dev

Receiving "ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() could not receive data from server: Connection timed out" in rake task

From Dev

Non persistent pg connections

From Dev

PG::ConnectionBad: could not connect to server (heroku)

From Dev

could not connect to server: No such file or directory (PG::ConnectionBad)

From Dev

PG::ConnectionBad FATAL: role "BetBook3" is not permitted to log in

From Dev

PG::ConnectionBad: FATAL: password authentication failed for user "alphauser"

From Dev

PG::ConnectionBad: FATAL: Ident authentication failed for user "rails_dev"

From Dev

Java connection pool is not limiting the number of TCP connections opened to the DB server

From Dev

Frequent PG::ConnectionBad: connection is closed errors with RDS and rails / activerecord

From Dev

Putty fatal error: Network error: connection refused (ubunty server)

From Dev

Putty fatal error: Network error: connection refused (ubunty server)

From Dev

Netty and non standard TCP connections

From Dev

Heroku + Rails + PG: ActiveRecord::StatementInvalid (PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly

From Dev

Cannot modify a default parameter group for remaining connection slots on Amazon RDS

From Dev

Docker - PG::ConnectionBad

From Dev

Rails 5 in prod: PG::ConnectionBad: FATAL: password authentication failed for user "postgres" with Docker

From Dev

Refusing incoming connections to TCP Server

Related Related

  1. 1

    Django/Postgres: FATAL: remaining connection slots are reserved for non-replication superuser connections

  2. 2

    org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections

  3. 3

    AWS RDS PostgreSQL error "remaining connection slots are reserved for non-replication superuser connections"

  4. 4

    PG::ConnectionBad - could not connect to server: Connection refused

  5. 5

    PG::ConnectionBad: could not connect to server: Connection refused

  6. 6

    PG::ConnectionBad: could not connect to server: Connection refused

  7. 7

    Rails - Postgres - could not connect to server: Connection refused (PG::ConnectionBad)

  8. 8

    Heroku: PG::ConnectionBad: could not connect to server: Connection refused

  9. 9

    PG::ConnectionBad error

  10. 10

    PG::ConnectionBad FATAL: role "Myname" does not exist

  11. 11

    PG::ConnectionBad FATAL: role "Myname" does not exist

  12. 12

    Switched to PG! DB migration error: PG::ConnectionBad:

  13. 13

    Receiving "ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() could not receive data from server: Connection timed out" in rake task

  14. 14

    Non persistent pg connections

  15. 15

    PG::ConnectionBad: could not connect to server (heroku)

  16. 16

    could not connect to server: No such file or directory (PG::ConnectionBad)

  17. 17

    PG::ConnectionBad FATAL: role "BetBook3" is not permitted to log in

  18. 18

    PG::ConnectionBad: FATAL: password authentication failed for user "alphauser"

  19. 19

    PG::ConnectionBad: FATAL: Ident authentication failed for user "rails_dev"

  20. 20

    Java connection pool is not limiting the number of TCP connections opened to the DB server

  21. 21

    Frequent PG::ConnectionBad: connection is closed errors with RDS and rails / activerecord

  22. 22

    Putty fatal error: Network error: connection refused (ubunty server)

  23. 23

    Putty fatal error: Network error: connection refused (ubunty server)

  24. 24

    Netty and non standard TCP connections

  25. 25

    Heroku + Rails + PG: ActiveRecord::StatementInvalid (PG::ConnectionBad: PQconsumeInput() SSL connection has been closed unexpectedly

  26. 26

    Cannot modify a default parameter group for remaining connection slots on Amazon RDS

  27. 27

    Docker - PG::ConnectionBad

  28. 28

    Rails 5 in prod: PG::ConnectionBad: FATAL: password authentication failed for user "postgres" with Docker

  29. 29

    Refusing incoming connections to TCP Server

HotTag

Archive