Created
May 16, 2016 22:06
-
-
Save arthurnn/ab57e0321c796e6c0c6dc7a0fc228c01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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