X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI.pm;h=ac90066876f7b747187bf41807211200217a25f7;hb=aa0163d42e50f63a1bd40d910654008ee8ca7a09;hp=e2d0b41991d7e1c3a8e91a04dea494bf7d056db2;hpb=4068e05f67336c92ee83f71cdd86df10beee014e;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index e2d0b41..ac90066 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -42,7 +42,6 @@ __PACKAGE__->mk_group_accessors('inherited' => qw/ /); __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks'); - # Each of these methods need _determine_driver called before itself # in order to function reliably. This is a purely DRY optimization my @rdbms_specific_methods = qw/ @@ -117,9 +116,102 @@ sub new { $new->{_in_dbh_do} = 0; $new->{_dbh_gen} = 0; + # read below to see what this does + $new->_arm_global_destructor; + $new; } +# This is hack to work around perl shooting stuff in random +# order on exit(). If we do not walk the remaining storage +# objects in an END block, there is a *small but real* chance +# of a fork()ed child to kill the parent's shared DBI handle, +# *before perl reaches the DESTROY in this package* +# Yes, it is ugly and effective. +{ + my %seek_and_destroy; + + sub _arm_global_destructor { + my $self = shift; + my $key = Scalar::Util::refaddr ($self); + $seek_and_destroy{$key} = $self; + Scalar::Util::weaken ($seek_and_destroy{$key}); + } + + END { + local $?; # just in case the DBI destructor changes it somehow + + # destroy just the object if not native to this process/thread + $_->_preserve_foreign_dbh for (grep + { defined $_ } + values %seek_and_destroy + ); + } +} + +sub DESTROY { + my $self = shift; + + # destroy just the object if not native to this process/thread + $self->_preserve_foreign_dbh; + + # some databases need this to stop spewing warnings + if (my $dbh = $self->_dbh) { + local $@; + eval { + %{ $dbh->{CachedKids} } = (); + $dbh->disconnect; + }; + } + + $self->_dbh(undef); +} + +sub _preserve_foreign_dbh { + my $self = shift; + + return unless $self->_dbh; + + $self->_verify_tid; + + return unless $self->_dbh; + + $self->_verify_pid; + +} + +# handle pid changes correctly - do not destroy parent's connection +sub _verify_pid { + my $self = shift; + + return if ( defined $self->_conn_pid and $self->_conn_pid == $$ ); + + $self->_dbh->{InactiveDestroy} = 1; + $self->_dbh(undef); + $self->{_dbh_gen}++; + + return; +} + +# very similar to above, but seems to FAIL if I set InactiveDestroy +sub _verify_tid { + my $self = shift; + + if ( ! defined $self->_conn_tid ) { + return; # no threads + } + elsif ( $self->_conn_tid == threads->tid ) { + return; # same thread + } + + #$self->_dbh->{InactiveDestroy} = 1; # why does t/51threads.t fail...? + $self->_dbh(undef); + $self->{_dbh_gen}++; + + return; +} + + =head2 connect_info This method is normally called by L, which @@ -805,19 +897,11 @@ sub connected { sub _seems_connected { my $self = shift; + $self->_preserve_foreign_dbh; + my $dbh = $self->_dbh or return 0; - if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) { - $self->_dbh(undef); - $self->{_dbh_gen}++; - return 0; - } - else { - $self->_verify_pid; - return 0 if !$self->_dbh; - } - return $dbh->FETCH('Active'); } @@ -829,20 +913,6 @@ sub _ping { return $dbh->ping; } -# handle pid changes correctly -# NOTE: assumes $self->_dbh is a valid $dbh -sub _verify_pid { - my ($self) = @_; - - return if defined $self->_conn_pid && $self->_conn_pid == $$; - - $self->_dbh->{InactiveDestroy} = 1; - $self->_dbh(undef); - $self->{_dbh_gen}++; - - return; -} - sub ensure_connected { my ($self) = @_; @@ -875,7 +945,7 @@ sub dbh { # this is the internal "get dbh or connect (don't check)" method sub _get_dbh { my $self = shift; - $self->_verify_pid if $self->_dbh; + $self->_preserve_foreign_dbh; $self->_populate_dbh unless $self->_dbh; return $self->_dbh; } @@ -1874,7 +1944,7 @@ sub _select_args { #limited has_many ( $attrs->{rows} && keys %{$attrs->{collapse}} ) || - # limited prefetch with RNO subqueries + # limited prefetch with RNO subqueries (otherwise a risk of column name clashes) ( $attrs->{rows} && @@ -1885,7 +1955,7 @@ sub _select_args { @{$attrs->{_prefetch_select}} ) || - # grouped prefetch + # grouped prefetch (to satisfy group_by == select) ( $attrs->{group_by} && @{$attrs->{group_by}} @@ -1900,6 +1970,8 @@ sub _select_args { } elsif ( + # the RNO limit dialect mangles the SQL such that the join gets lost + # wrap a subquery here ($attrs->{rows} || $attrs->{offset}) && $sql_maker->limit_dialect eq 'RowNumberOver' @@ -1908,8 +1980,6 @@ sub _select_args { && scalar $self->_parse_order_by ($attrs->{order_by}) ) { - # the RNO limit dialect above mangles the SQL such that the join gets lost - # wrap a subquery here push @limit, delete @{$attrs}{qw/rows offset/}; @@ -2338,7 +2408,7 @@ sub create_ddl_dir { $dir = './'; } else { -d $dir or File::Path::mkpath($dir) - or croak "create_ddl_dir: could not create dir '$dir'"; + or $self->throw_exception("create_ddl_dir: $! creating dir '$dir'"); } $self->throw_exception ("Directory '$dir' does not exist\n") unless(-d $dir); @@ -2654,23 +2724,6 @@ sub relname_to_table_alias { return $alias; } -sub DESTROY { - my $self = shift; - - $self->_verify_pid if $self->_dbh; - - # some databases need this to stop spewing warnings - if (my $dbh = $self->_dbh) { - local $@; - eval { - %{ $dbh->{CachedKids} } = (); - $dbh->disconnect; - }; - } - - $self->_dbh(undef); -} - 1; =head1 USAGE NOTES