| 1 | package DBIx::QueryByName::DbhPool; | ||
| 2 | |||
| 3 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use DBIx::QueryByName::Logger qw(get_logger debug); | ||
| 9 | use Scalar::Util qw(weaken); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | return bless( { connections => {}, config => {} }, $_[0] ); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub parent { | ||
| 16 | my ($self, $parent) = @_; | ||
| 17 | $self->{sthpool} = $parent->_sth_pool; | ||
| 18 | weaken $self->{sthpool}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub add_credentials { | ||
| 22 | my ($self, $session, @params) = @_; | ||
| 23 | my $log = get_logger(); | ||
| 24 | $log->logcroak("undefined session name") if (!defined $session); | ||
| 25 | $log->logcroak("no session parameters provided") if (scalar @params == 0); | ||
| 26 | $log->logcroak("credentials for session [$session] are already declared") if ($self->knows_session($session)); | ||
| 27 | $self->{config}->{$session} = \@params; | ||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub knows_session { | ||
| 32 | my ($self, $session) = @_; | ||
| 33 | my $log = get_logger(); | ||
| 34 | $log->logcroak("undefined session name") if (!defined $session); | ||
| 35 | return (exists $self->{config}->{$session}) ? 1 : 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _inactivate_parent_connections { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | foreach my $pid ( keys %{$self->{connections}} ) { | ||
| 42 | foreach my $session ( keys %{$self->{connections}->{$pid}} ) { | ||
| 43 | if ( $$ != $pid ) { | ||
| 44 | if ( defined $self->{connections}->{$pid}->{$session}->{InactiveDestroy} && | ||
| 45 | $self->{connections}->{$pid}->{$session}->{InactiveDestroy} != 1 ) { | ||
| 46 | # the connection belongs to an other process than self. | ||
| 47 | # Prevent forked child (this pid) from disconnecting the database connection | ||
| 48 | debug "Setting connection for pid $$ and session $session as InactiveDestroy"; | ||
| 49 | $self->{connections}->{$pid}->{$session}->{InactiveDestroy} = 1; | ||
| 50 | delete $self->{connections}->{$pid}->{$session}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | # open database connection for the given session and return a database | ||
| 60 | # handler | ||
| 61 | sub connect { | ||
| 62 | my ($self, $session) = @_; | ||
| 63 | my $log = get_logger(); | ||
| 64 | $log->logcroak("undefined session name") if (!defined $session); | ||
| 65 | |||
| 66 | return $self->{connections}->{$$}->{$session} if (defined $self->{connections}->{$$}->{$session}); | ||
| 67 | |||
| 68 | # Before opening connection, we need to set InactiveDestroy on | ||
| 69 | # other processes connections. Even then, there is a risk that a | ||
| 70 | # process that just forks but open no own connections will close | ||
| 71 | # all the connections of related processes upon exit. | ||
| 72 | $self->_inactivate_parent_connections(); | ||
| 73 | |||
| 74 | # try to open database connection | ||
| 75 | # TODO: implement a giveup limit? | ||
| 76 | my $error_reported = 0; | ||
| 77 | while (1) { | ||
| 78 | $log->logcroak("don't know how to open connection [$session]") | ||
| 79 | if (!$self->knows_session($session)); | ||
| 80 | |||
| 81 | debug "Trying to connect to database for session $session"; | ||
| 82 | my $dbh = DBI->connect( @{$self->{config}->{$session}} ); | ||
| 83 | |||
| 84 | if (!defined $dbh) { | ||
| 85 | # TODO: croak after a number of attempts? | ||
| 86 | $log->error("Unable to connect to database [$session]: ".$DBI::errstr) if ($error_reported == 0); | ||
| 87 | $error_reported = 1; | ||
| 88 | sleep(1); | ||
| 89 | next; | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->{connections}->{$$}->{$session} = $dbh; | ||
| 93 | if ($error_reported == 1) { | ||
| 94 | $log->info( "Database is back online [$session]"); | ||
| 95 | } else { | ||
| 96 | debug "Connected to database"; | ||
| 97 | } | ||
| 98 | |||
| 99 | return $dbh; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub disconnect { | ||
| 104 | my ($self, $session) = @_; | ||
| 105 | |||
| 106 | my $log = get_logger(); | ||
| 107 | $log->logcroak("undefined session name") if (!defined $session); | ||
| 108 | $log->logcroak("not a known session name") if (!$self->knows_session($session)); | ||
| 109 | |||
| 110 | if (defined $self->{connections}->{$$}->{$session}) { | ||
| 111 | debug "Disconnecting session $session for pid $$"; | ||
| 112 | $self->{connections}->{$$}->{$session}->disconnect(); | ||
| 113 | delete $self->{connections}->{$$}->{$session}; | ||
| 114 | } | ||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub disconnect_all { | ||
| 119 | my $self = shift; | ||
| 120 | $self->_inactivate_parent_connections; | ||
| 121 | debug "Disconnecting all dbhs for process $$"; | ||
| 122 | foreach my $session ( keys %{$self->{connections}->{$$}} ) { | ||
| 123 | $self->disconnect($session); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub DESTROY { | ||
| 128 | my $self = shift; | ||
| 129 | |||
| 130 | debug "DESTROY DbhPool -> calling finish_all_sths and disconnect_all"; | ||
| 131 | # either this DESTROY is called first, or SthPool's DESTROY is | ||
| 132 | $self->{sthpool}->finish_all_sths() if (defined $self->{sthpool}); | ||
| 133 | $self->disconnect_all(); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 | |||
| 138 | __END__ | ||