X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB.pm;h=a59fd6fdf503059f05878178fa3524795750f792;hb=f05bd96b031d7d4ffaf4804704684ae929a0890a;hp=f133e6af62c7e4ad81a5ff19576e8cfffb344d6b;hpb=0b84f51fd598e4b5a1cb139aa582297d089b1602;p=kivitendo-erp.git diff --git a/SL/DB.pm b/SL/DB.pm index f133e6af6..a59fd6fdf 100644 --- a/SL/DB.pm +++ b/SL/DB.pm @@ -6,11 +6,12 @@ use Carp; use Data::Dumper; use English qw(-no_match_vars); use Rose::DB; -use Rose::DBx::Cache::Anywhere; +use SL::DB::Helper::Cache; +use Scalar::Util qw(blessed); use base qw(Rose::DB); -__PACKAGE__->db_cache_class('Rose::DBx::Cache::Anywhere'); +__PACKAGE__->db_cache_class('SL::DB::Helper::Cache'); __PACKAGE__->use_private_registry; my (%_db_registered); @@ -34,6 +35,14 @@ sub create { return $db; } +sub client { + create(undef, 'KIVITENDO'); +} + +sub auth { + create(undef, 'KIVITENDO_AUTH'); +} + sub _register_db { my $domain = shift; my $type = shift; @@ -84,7 +93,7 @@ sub _register_db { my %flattened_settings = _flatten_settings(%connect_settings); $domain = 'KIVITENDO' if $type =~ m/^KIVITENDO/; - $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'dbpasswd' } keys %flattened_settings); + $type .= join($SUBSCRIPT_SEPARATOR, map { ($_, $flattened_settings{$_} || '') } sort grep { $_ ne 'password' } keys %flattened_settings); my $idx = "${domain}::${type}"; if (!$_db_registered{$idx}) { @@ -118,14 +127,30 @@ sub with_transaction { my ($self, $code, @args) = @_; return $code->(@args) if $self->in_transaction; - if (wantarray) { - my @result; - return $self->do_transaction(sub { @result = $code->(@args) }) ? @result : (); - } else { - my $result; - return $self->do_transaction(sub { $result = $code->(@args) }) ? $result : undef; - } + my (@result, $result); + my $rv = 1; + + local $@; + my $return_array = wantarray; + eval { + $return_array + ? $self->do_transaction(sub { @result = $code->(@args) }) + : $self->do_transaction(sub { $result = $code->(@args) }); + } or do { + my $error = $self->error; + if (blessed $error) { + if ($error->isa('SL::X::DBError')) { + # gobble the exception + } else { + $error->rethrow; + } + } else { + die $self->error; + } + }; + + return $return_array ? @result : $result; } 1; @@ -165,50 +190,61 @@ starting one only if none is currently active. Example: # do stuff with $self }); -There are two big differences between C and -L: the handling of an already-running -transaction and the handling of return values. +This is a wrapper around L that does a few additional +things, and should always be used in favour of the other: -The first difference revolves around when a transaction is started and -committed/rolled back. Rose's C will always start one, -then execute the code reference and commit afterwards (or roll back if -an exception occurs). +=over 4 + +=item Composition of transactions -This prevents the caller from combining several pieces of code using -C reliably as results committed by an inner -transaction will be permanent even if the outer transaction is rolled -back. +When C is called without a running transaction, a new one is +created. If it is called within a running transaction, it performs no +additional handling. This means that C can be safely used +within another C, whereas L can not. -Therefore our C works differently: it will only -start a transaction if no transaction is currently active on the -database connection. +=item Return values -The second big difference to L is the -handling of returned values. Basically our C will -return the values that the code reference C<$code_ref> returns (or -C if the transaction was rolled back). Rose's C -on the other hand will only return a value signaling the transaction's -status. +C adopts the behaviour of C in that it returns the +result of the inner block, and C if an error occurred. This way you can +use the same pattern you would normally use with C for +C: -In more detail: + SL::DB->client->with_transaction(sub { + # do stuff + # and return nominal true value + 1; + }) or do { + # transaction error handling + my $error = SL::DB->client->error; + } -=over 2 +or you can use it to safely calulate things. -=item * If a transaction is already active then C -will simply return the result of calling C<$code_ref> as-is preserving -context. +=item Error handling -=item * If no transaction is started then C<$code_ref> will be wrapped -in one. C's return value depends on the result of -that transaction. If the it succeeds then the return value of -C<$code_ref> will be returned preserving context. Otherwise C -will be returned in scalar context and an empty list in list context. +The original L gobbles up all exceptions and expects +the caller to manually check the return value and error, and then to process +all exceptions as strings. This is very fragile and generally a step backwards +from proper exception handling. -=back +C only gobbles up exceptions that are used to signal an +error in the transaction, and returns undef on those. All other exceptions +bubble out of the transaction like normal, so that it is transparent to typos, +runtime exceptions and other generally wanted things. -So if you want to differentiate between "transaction failed" and -"succeeded" then your C<$code_ref> should never return C -itself. +If you just use the snippet above, your code will catch everything related to +the transaction aborting, but will not catch other errors that might have been +thrown. The transaction will be rolled back in both cases. + +If you want to play nice in case your transaction is embedded in another +transaction, just rethrow the error: + + $db->with_transaction(sub { + # code deep in the engine + 1; + }) or die $db->error; + +=back =back