Changeset 273

Show
Ignore:
Timestamp:
08/06/07 20:03:17 (3 years ago)
Author:
eevee
Message:

Attempt to fix duplicate-entry problem with sessions, 2.0!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • veekun/trunk/lib/Vee.pm

    r272 r273  
    5555 
    5656# grab the current revision number, if applicable 
    57 __PACKAGE__->cache->{svn_revision} =  
     57__PACKAGE__->cache->{svn_revision} = 0 and 
    5858    Vee::Utils::timed_exec( 5 => sub { 
    5959        open my $fh, '-|', 'svnversion' or return; 
     
    8080    # DBIx::Class's find_or_create is incredibly stupid and allows for race 
    8181    # conditions with critical code like this. 
    82     # This is MySQL-specific!  Also it is a horrendous hack. 
    8382    # CAVEAT EMPTOR: may break if you upgrade 
    84     *{'Catalyst::Plugin::Session::Store::DBIC::Delegate::session'} = sub { 
    85         my ($self, $key) = @_; 
    86  
    87         my $row = $self->_session_row; 
    88  
    89         unless ($row) { 
    90             eval { 
    91                 $row = $self->model->find_or_create({ $self->id_field => $key }); 
    92             }; 
    93             if ($@ and $@ =~ /Duplicate entry/) { 
    94                 $row = $self->model->create({ $self->id_field => $key }); 
    95             } elsif ($@) { 
    96                 die $@; 
    97             } 
    98             $self->_session_row($row); 
     83    *{'DBIx::Class::ResultSet::find_or_create'} = sub { 
     84        my $self     = shift; 
     85        my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}); 
     86        my $hash     = ref $_[0] eq 'HASH' ? shift : {@_}; 
     87        my $exists   = $self->find($hash, $attrs); 
     88        # this bit is different 
     89        return $exists if defined $exists; 
     90        eval { $exists = $self->create($hash) }; 
     91        if ($@ and $@ =~ /Duplicate entry/) { 
     92            return $self->create($hash); 
     93        } elsif ($@) { 
     94            die $@; 
     95        } else { 
     96            return $exists; 
    9997        } 
    100  
    101         return $row; 
    10298    }; 
    10399}