Revision history for DBIx::Class
+ - ithreads compat added, fork compat improved
- weaken result_source in all resultsets
0.05999_03 2006-03-14 01:58:10
# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
# brain damage and presumably various other packaging systems too
-$VERSION = '0.05999_03';
+$VERSION = '0.05999_04';
sub MODIFY_CODE_ATTRIBUTES {
my ($class,$code,@attrs) = @_;
__PACKAGE__->load_components(qw/AccessorGroup/);
__PACKAGE__->mk_group_accessors('simple' =>
- qw/connect_info _dbh _sql_maker _connection_pid debug debugfh cursor
- on_connect_do transaction_depth/);
+ qw/connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugfh
+ cursor on_connect_do transaction_depth/);
sub new {
my $new = bless({}, ref $_[0] || $_[0]);
sub connected {
my ($self) = @_;
- my $dbh;
- (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping)
+ if(my $dbh = $self->_dbh) {
+ if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) {
+ $self->_sql_maker(undef);
+ return $self->_dbh(undef);
+ }
+ elsif($self->_conn_pid != $$) {
+ $self->_dbh->{InactiveDestroy} = 1;
+ $self->_sql_maker(undef);
+ return $self->_dbh(undef)
+ }
+ return ($dbh->FETCH('Active') && $dbh->ping);
+ }
+
+ return 0;
}
sub ensure_connected {
sub dbh {
my ($self) = @_;
- if($self->_connection_pid && $self->_connection_pid != $$) {
- $self->_dbh->{InactiveDestroy} = 1;
- $self->_dbh(undef)
- }
$self->ensure_connected;
return $self->_dbh;
}
$self->_dbh->do($sql_statement);
}
- $self->_connection_pid($$);
+ $self->_conn_pid($$);
+ $self->_conn_tid(threads->tid) if $INC{'threads.pm'};
}
sub _connect {
else {
--$self->{transaction_depth} == 0 ?
$self->dbh->rollback :
- die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new;
+ die DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION->new;
}
};
my ($self, $schema, $type, $sqltargs) = @_;
foreach(split(";\n", $self->deployment_statements($schema, $type, $sqltargs))) {
$self->debugfh->print("$_\n") if $self->debug;
- $self->dbh->do($_) or warn "SQL was:\n $_";
+ $self->dbh->do($_) or warn "SQL was:\n $_";
}
}
storage => $storage,
args => $args,
pos => 0,
- attrs => $attrs };
+ attrs => $attrs,
+ pid => $$,
+ };
+
+ $new->{tid} = threads->tid if $INC{'threads.pm'};
+
return bless ($new, $class);
}
sub next {
my ($self) = @_;
+
+ $self->_check_forks_threads;
if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
$self->{sth}->finish if $self->{sth}->{Active};
delete $self->{sth};
sub all {
my ($self) = @_;
+
+ $self->_check_forks_threads;
return $self->SUPER::all if $self->{attrs}{rows};
$self->{sth}->finish if $self->{sth}->{Active};
delete $self->{sth};
sub reset {
my ($self) = @_;
+
+ $self->_check_forks_threads;
$self->{sth}->finish if $self->{sth}->{Active};
+ $self->_soft_reset;
+}
+
+sub _soft_reset {
+ my ($self) = @_;
+
delete $self->{sth};
$self->{pos} = 0;
delete $self->{done};
return $self;
}
+sub _check_forks_threads {
+ my ($self) = @_;
+
+ if($INC{'threads.pm'} && $self->{tid} != threads->tid) {
+ $self->_soft_reset;
+ $self->{tid} = threads->tid;
+ }
+
+ if($self->{pid} != $$) {
+ $self->_soft_reset;
+ $self->{pid} = $$;
+ }
+}
+
sub DESTROY {
my ($self) = @_;
+
+ $self->_check_forks_threads;
$self->{sth}->finish if $self->{sth}->{Active};
}
-use Class::C3;
use strict;
-use Test::More;
use warnings;
+use Test::More;
-# This test passes no matter what in most cases. However, prior to the recent
-# fork-related fixes, it would spew lots of warnings. I have not quite gotten
-# it to where it actually fails in those cases.
+# README: If you set the env var to a number greater than 10,
+# we will use that many children
my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
+my $num_children = $ENV{DBICTEST_FORK_STRESS};
plan skip_all => 'Set $ENV{DBICTEST_FORK_STRESS} to run this test'
- unless $ENV{DBICTEST_FORK_STRESS};
+ unless $num_children;
plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
. ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
-plan tests => 15;
+if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
+ $num_children = 10;
+}
+
+plan tests => $num_children + 5;
use lib qw(t/lib);
use_ok('DBICTest::Schema');
-DBICTest::Schema->compose_connection('PgTest' => $dsn, $user, $pass, { AutoCommit => 1 });
+my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1 });
-my ($first_rs, $joe_record);
+my $parent_rs;
eval {
- my $dbh = PgTest->schema->storage->dbh;
+ my $dbh = $schema->storage->dbh;
- eval {
- $dbh->do("DROP TABLE cd");
+ {
+ local $SIG{__WARN__} = sub {};
+ eval { $dbh->do("DROP TABLE cd") };
$dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(255) NOT NULL UNIQUE, year VARCHAR(255));");
- };
+ }
- PgTest->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
- PgTest->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
+ $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
+ $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
- $first_rs = PgTest->resultset('CD')->search({ year => 1901 });
- $joe_record = $first_rs->next;
+ $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
+ $parent_rs->next;
};
ok(!$@) or diag "Creation eval failed: $@";
-my $num_children = 10;
my @pids;
while(@pids < $num_children) {
}
elsif($pid) {
push(@pids, $pid);
- next;
+ next;
}
$pid = $$;
- my ($forked_rs, $joe_forked);
- $forked_rs = PgTest->resultset('CD')->search({ year => 1901 });
- $joe_forked = $first_rs->next;
- if($joe_forked && $joe_forked->get_column('artist') =~ /^(?:123|456)$/) {
- PgTest->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
+ my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
+ my $row = $parent_rs->next;
+ if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
+ $schema->resultset('CD')->create({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
}
sleep(3);
exit;
while(@pids) {
my $pid = pop(@pids);
- my $rs = PgTest->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
+ my $rs = $schema->resultset('CD')->search({ title => "test success $pid", artist => $pid, year => scalar(@pids) });
is($rs->next->get_column('artist'), $pid, "Child $pid successful");
}
ok(1, "Made it to the end");
-PgTest->schema->storage->dbh->do("DROP TABLE cd");
+$schema->storage->dbh->do("DROP TABLE cd");
--- /dev/null
+use strict;
+use warnings;
+use Test::More;
+use Config;
+
+# README: If you set the env var to a number greater than 10,
+# we will use that many children
+
+BEGIN {
+ plan skip_all => 'Your perl does not support ithreads'
+ if !$Config{useithreads} || $] < 5.008;
+}
+
+use threads;
+
+my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
+my $num_children = $ENV{DBICTEST_THREAD_STRESS};
+
+plan skip_all => 'Set $ENV{DBICTEST_THREAD_STRESS} to run this test'
+ unless $num_children;
+
+plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
+ . ' (note: creates and drops a table named artist!)' unless ($dsn && $user);
+
+diag 'It is normal to see a series of "Scalars leaked: ..." messages during this test';
+
+if($num_children !~ /^[0-9]+$/ || $num_children < 10) {
+ $num_children = 10;
+}
+
+plan tests => $num_children + 5;
+
+use lib qw(t/lib);
+
+use_ok('DBICTest::Schema');
+
+my $schema = DBICTest::Schema->connection($dsn, $user, $pass, { AutoCommit => 1, RaiseError => 1, PrintError => 0 });
+
+my $parent_rs;
+
+eval {
+ my $dbh = $schema->storage->dbh;
+
+ {
+ local $SIG{__WARN__} = sub {};
+ eval { $dbh->do("DROP TABLE cd") };
+ $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(255) NOT NULL UNIQUE, year VARCHAR(255));");
+ }
+
+ $schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
+ $schema->resultset('CD')->create({ title => 'vacation in antarctica part 2', artist => 456, year => 1901 });
+
+ $parent_rs = $schema->resultset('CD')->search({ year => 1901 });
+ $parent_rs->next;
+};
+ok(!$@) or diag "Creation eval failed: $@";
+
+my @children;
+while(@children < $num_children) {
+
+ my $newthread = async {
+ my $tid = threads->tid;
+ my $dbh = $schema->storage->dbh;
+
+ my $child_rs = $schema->resultset('CD')->search({ year => 1901 });
+ my $row = $parent_rs->next;
+ if($row && $row->get_column('artist') =~ /^(?:123|456)$/) {
+ $schema->resultset('CD')->create({ title => "test success $tid", artist => $tid, year => scalar(@children) });
+ }
+ sleep(3);
+ };
+ die "Thread creation failed: $! $@" if !defined $newthread;
+ push(@children, $newthread);
+}
+
+ok(1, "past spawning");
+
+{
+ $_->join for(@children);
+}
+
+ok(1, "past joining");
+
+while(@children) {
+ my $child = pop(@children);
+ my $tid = $child->tid;
+ my $rs = $schema->resultset('CD')->search({ title => "test success $tid", artist => $tid, year => scalar(@children) });
+ is($rs->next->get_column('artist'), $tid, "Child $tid successful");
+}
+
+ok(1, "Made it to the end");
+
+$schema->storage->dbh->do("DROP TABLE cd");