Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created May 17, 2016 20:06
Show Gist options
  • Save arthurnn/40c5b812411af5e02e73644992c9f3b1 to your computer and use it in GitHub Desktop.
Save arthurnn/40c5b812411af5e02e73644992c9f3b1 to your computer and use it in GitHub Desktop.
require 'pg'
class Context
attr_reader :connections
def initialize
@connections = {}
end
def connection
@connections.fetch(Process.pid) { @connections[Process.pid] = PG.connect( dbname: 'test' ) }
end
end
context = Context.new
p context.connection.exec( "SELECT current_database();" ).to_a
parent_pid = Process.pid
fork do
at_exit do
context.connections[parent_pid].socket_io.close
end
end
Process.wait
p context.connection.exec( "SELECT current_database();" ).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment