in reply to how to tell if ->insert succeeded in DBIx::Class?

in_storage()?
  • Comment on Re: how to tell if ->insert succeeded in DBIx::Class?

Replies are listed 'Best First'.
Re^2: how to tell if ->insert succeeded in DBIx::Class?
by perl5ever (Pilgrim) on Feb 22, 2011 at 23:40 UTC
    This naive use of ->in_storage leads to a race condition:
    sub insert_row { my ($self, $run) = @_; if ($run->in_storage) { return; } else { # race condition here $run->insert; return 1; # ??? }

    Part of the problem is that the Oracle driver throws an exception for primary key violations. Catching that exception requires that I know what the exception looks like, and that leads to the Oracle-specific code.

    If, for instance, $run->insert returned false if the insert failed for a unique index violation, then I could write database independent code.

Re^2: how to tell if ->insert succeeded in DBIx::Class?
by ikegami (Patriarch) on Feb 22, 2011 at 23:26 UTC

    I don't think that checks the database at all. "in_storage tells us whether the Row object is in the database or not. This is set when fetching data, and when we insert a new row."

    Update: Confirmed:

    sub in_storage { my ($self, $val) = @_; $self->{_in_storage} = $val if @_ > 1; return $self->{_in_storage} ? 1 : 0; }