Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created May 16, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/ab57e0321c796e6c0c6dc7a0fc228c01 to your computer and use it in GitHub Desktop.
Save arthurnn/ab57e0321c796e6c0c6dc7a0fc228c01 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
def disconnect
if @connections[Process.pid]
@connections.delete(Process.pid).close
end
end
end
context = Context.new
def before_fork(context)
context.disconnect
end
def after_fork(context)
end
p context.connection.exec( "SELECT current_database();" ).to_a
before_fork(context)
fork do
after_fork(context)
p context.connection.exec( "SELECT current_database();" ).to_a
end
sleep 5
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