]> Frank Brehm's Git Trees - books.git/commitdiff
weiter Umbau auf Transaktionen
authorFrank Brehm <frank@brehm-online.com>
Fri, 26 Feb 2010 11:44:52 +0000 (11:44 +0000)
committerFrank Brehm <frank@brehm-online.com>
Fri, 26 Feb 2010 11:44:52 +0000 (11:44 +0000)
lib/FrBr/Books/Plugin/Author.pm
lib/FrBr/Books/Plugin/Book.pm

index dda732f8c87eefeb272734343bca71f9fda17ff0..460db96227277f48776ad3d125e7552103fed251 100644 (file)
@@ -409,63 +409,6 @@ sub b_save_autor {
     $c->stash->{'save_autor_result'} = 'OK';
     return $id;
 
-=pod
-
-    # Sperren der Autoren-Tabelle
-    $c->lock_tables( 'write' => [ 'autoren', 'autoren', 'as', 'me' ] );
-
-       $saved_aid = $c->b_get_autor_id( $search_params );
-       
-    # Und jetzt Test im gelocktem Zustand ...
-    if ( $autor->{'id'} ) {
-        if ( $saved_aid and ( $saved_aid != $autor->{'id'} ) ) {
-            $c->stash->{'save_autor_result'} = 'Es existiert bereits ein Autor mit den übergebenen Angaben.';
-            $c->log->info( $K . "Es existiert bereits ein Autor mit den übergebenen Angaben." );
-            $c->unlock_tables();
-            return undef;
-        }
-    }
-    else {
-        if ( $saved_aid ) {
-            $c->stash->{'save_autor_result'} = 'Es existiert bereits ein Autor mit den uebergebenen Angaben.';
-            $c->log->info( $K . "Es existiert bereits ein Autor mit den uebergebenen Angaben." );
-            $c->unlock_tables();
-            return undef;
-        }
-    }
-
-    my $new_id = undef;
-
-       eval {
-               if ( $autor->{'id'} ) {
-                       $new_id = $autor->{'id'};
-                       my $count = $c->model('Schema::Autoren')->count( { 'id' => $autor->{'id'} } );
-                       if ( $count ) {
-                               $c->model('Schema::Autoren')->search( { 'id' => $autor->{'id'} } )->update( $row );
-                       }
-                       else {
-                               $row->{'id'} = $new_id;
-                               $c->model('Schema::Autoren')->create($row);
-                       }
-               }
-               else {
-                       $c->model('Schema::Autoren')->create($row);
-               $new_id = $storage->last_insert_id();
-               }
-       };
-       if ( $@ ) {
-               $c->stash->{'save_autor_result'} = $@;
-               $c->log->info( $K . $@ );
-               $c->unlock_tables();
-               return undef;
-       }
-
-    $c->unlock_tables();
-    $c->stash->{'save_autor_result'} = 'OK';
-    return $new_id;
-
-=cut
-
 }
 
 #-----------------------------------------------------------------------------------
index 99fd03fc2da6662bbd863ca09d57e67a4b34e72b..5d1a266f31e89450a7e7f4d91c006f3962d8ebdf 100644 (file)
@@ -745,6 +745,7 @@ sub b_delete_book {
     'bindungsart_id' => 4,
     'book_nr'        => '',
     'druckjahr'      => '2006',
+    'id'             => 234,
     'isbn'           => '3-492-70076-4',
     'kategorien'     => [
       '2'
@@ -770,110 +771,129 @@ sub b_save_book {
 
     my $c    = shift;
     my $book = shift;
-    my $K    = ( caller(0) )[3] . "(): ";
+
+    my $K = ( caller(0) )[3] . "(): ";
+    my $k = $c->log->is_debug() ? $K : '';
 
     $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
 
     my $schema =  $c->model('Schema')->{'schema'};
     my $storage = $c->model('Schema')->storage;
 
-    my $tmp = $book->{'kurzinhalt'};
+    my $row_hash = {};
+    my $tmp;
+    my $id = undef;
+
+    $row_hash->{'id'} = $book->{'id'} if exists $book->{'id'};
+    $row_hash->{'title'} = defined $book->{'title'} ? $book->{'title'} : '';
+
+    $tmp = $book->{'original_title'};
+    $tmp = '' unless defined $tmp;
+    $tmp =~ s/^\s+//;
+    $tmp =~ s/\s+$//;
+    $row_hash->{'title_original'} = $tmp eq '' ? undef : $tmp;
+
+    $tmp = $book->{'untertitel'};
+    $tmp = '' unless defined $tmp;
+    $tmp =~ s/^\s+//;
+    $tmp =~ s/\s+$//;
+    $row_hash->{'untertitel'} = $tmp eq '' ? undef : $tmp;
+
+    $row_hash->{'bindungsart_id'} = $book->{'bindungsart_id'};
+    $row_hash->{'verlags_id'} = $book->{'verlags_id'};
+    $row_hash->{'orts_id'} = $book->{'orts_id'};
+
+    $tmp = $book->{'isbn'};
+    $tmp = '' unless defined $tmp;
+    $tmp =~ s/^\s+//;
+    $tmp =~ s/\s+$//;
+    $row_hash->{'isbn'} = $tmp eq '' ? undef : $tmp;
+
+    $tmp = $book->{'book_nr'};
+    $tmp = '' unless defined $tmp;
+    $tmp =~ s/^\s+//;
+    $tmp =~ s/\s+$//;
+    $row_hash->{'buch_nr'} = $tmp eq '' ? undef : $tmp;
+
+    $tmp = $book->{'ausgabejahr'};
+    $tmp = '' unless defined $tmp;
+    $tmp = to_int($tmp);
+    $tmp = '' unless $tmp;
+    $row_hash->{'ausgabejahr'} = $tmp eq '' ? undef : $tmp;
+
+    $tmp = $book->{'druckjahr'};
+    $tmp = '' unless defined $tmp;
+    $tmp = to_int($tmp);
+    $tmp = '' unless $tmp;
+    $row_hash->{'druckjahr'} = $tmp eq '' ? undef : $tmp;
+
+    $tmp = $book->{'preis'};
+    $tmp = '' unless defined $tmp;
+    $tmp = to_float($tmp);
+    $tmp = '' unless $tmp;
+    $row_hash->{'preis'} = $tmp eq '' ? undef : $tmp;
+
+    $row_hash->{'waehrungs_id'} = $book->{'waehrungs_id'};
+
+    $tmp = $book->{'kurzinhalt'};
     $tmp = '' unless defined $tmp;
     $tmp =~ s/^\s+//;
     $tmp =~ s/\s+$//;
-       $book->{'kurzinhalt'} = $tmp eq '' ? undef : $tmp;
+       $row_hash->{'kurzinhalt'} = $tmp eq '' ? undef : $tmp;
 
     $tmp = $book->{'notes'};
     $tmp = '' unless defined $tmp;
     $tmp =~ s/^\s+//;
     $tmp =~ s/\s+$//;
-       $book->{'notes'} = $tmp eq '' ? undef : $tmp;
-
-    my $save_func = sub {
-
-        my $storage = shift;
-        my $dbh     = shift;
-        my $buch    = shift;
-
-        my $sql = <<ENDE;
-INSERT INTO `buecher` (
-    `id`, `title`, `title_original`, `untertitel`, `verlags_id`, `orts_id`, `kurzinhalt`,
-    `bindungsart_id`, `isbn`, `buch_nr`, `ausgabejahr`, `druckjahr`, `seiten`, `preis`, `waehrungs_id` )
-  VALUES (
-    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
-  ON DUPLICATE KEY UPDATE
-    `id`             = LAST_INSERT_ID(`id`),
-    `title`          = ?,
-    `title_original` = ?,
-    `untertitel`     = ?,
-    `verlags_id`     = ?,
-    `orts_id`        = ?,
-    `kurzinhalt`     = ?,
-    `bindungsart_id` = ?,
-    `isbn`           = ?,
-    `buch_nr`        = ?,
-    `ausgabejahr`    = ?,
-    `druckjahr`      = ?,
-    `seiten`         = ?,
-    `preis`          = ?,
-    `waehrungs_id`   = ?
-ENDE
-
-        my @params = ();
-        push @params, $buch->{'id'};
-        push @params, $buch->{'title'};
-        push @params, $buch->{'original_title'};
-        push @params, $buch->{'untertitel'};
-        push @params, $buch->{'verlags_id'};
-        push @params, $buch->{'orts_id'};
-        push @params, $buch->{'kurzinhalt'};
-        push @params, $buch->{'bindungsart_id'};
-        push @params, $buch->{'isbn'};
-        push @params, $buch->{'book_nr'};
-        push @params, $buch->{'ausgabejahr'};
-        push @params, $buch->{'druckjahr'};
-        push @params, $buch->{'seiten'};
-        push @params, $buch->{'preis'};
-        push @params, $buch->{'waehrungs_id'};
-
-        push @params, $buch->{'title'};
-        push @params, $buch->{'original_title'};
-        push @params, $buch->{'untertitel'};
-        push @params, $buch->{'verlags_id'};
-        push @params, $buch->{'orts_id'};
-        push @params, $buch->{'kurzinhalt'};
-        push @params, $buch->{'bindungsart_id'};
-        push @params, $buch->{'isbn'};
-        push @params, $buch->{'book_nr'};
-        push @params, $buch->{'ausgabejahr'};
-        push @params, $buch->{'druckjahr'};
-        push @params, $buch->{'seiten'};
-        push @params, $buch->{'preis'};
-        push @params, $buch->{'waehrungs_id'};
-
-        if ( $storage->debug() ) {
-            my $text = $sql;
-            $text =~ s/\s+$//;
-            $text .= ": " . join( ", ", map { $dbh->quote($_) } @params ) . "\n";
-            warn $text;
-        }
+       $row_hash->{'notes'} = $tmp eq '' ? undef : $tmp;
 
-        my $sth = $dbh->prepare($sql);
-        $sth->execute( @params );
+    my $user_id = $c->stash->{'current_user_id'} || 1;
+    $row_hash->{'changed_at'} = \'NOW()';
+    $row_hash->{'changed_by'} = $user_id;
 
-    };
+    $c->log->debug( get_output_string( $K . "Buch-Row-Hash: ", $row_hash ) );
 
-    $storage->dbh_do( $save_func, $book );
+    # Alles, was innerhalb einer Transaktion ablaufen soll ...
+    my $txn_code = sub {
 
-    my $book_id = $storage->last_insert_id();
+        my $rs = $c->model('Schema::Buecher')->update_or_new( $row_hash, { 'key' => 'primary' },);
 
-    return undef unless $book_id;
+        $id = $rs->id();
+        die $k . "Keine Id für das Buch gefunden.\n" unless $id;
+        $c->log->debug( $K . "Verwende Buch-Id: " . $id );
 
-    $c->b_save_buch_autoren( $book_id, $book->{'autoren'} );
-    $c->b_save_buch_kategorien( $book_id, $book->{'kategorien'} );
-    $c->b_save_buch_serien( $book_id, $book->{'serien'} );
+        if ( $rs->in_storage() ) {
+            $c->log->debug( $K . "Das Buch gibt es schon ..." );
+            $rs->set_column( 'id' => \'LAST_INSERT_ID(`id`)' );
+            $rs->update();
+        }
+        else {
+            $c->log->debug( $K . "Neues Buch ..." );
+            $rs->set_column( 'created_by' => $user_id );
+            $rs->set_column( 'created_at' => \'NOW()' );
+            $rs->insert();
+        }
 
-    return $book_id;
+        $id = $storage->last_insert_id();
+
+        $c->b_save_buch_autoren( $id, $book->{'autoren'} );
+        $c->b_save_buch_kategorien( $id, $book->{'kategorien'} );
+        $c->b_save_buch_serien( $id, $book->{'serien'} );
+
+    };
+
+    # Und ab ...
+    eval {
+        $schema->txn_do($txn_code);
+    };
+
+    if ($@) {                             # Transaction failed
+        my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+        die $msg . "\n";
+        return undef;
+    }
+
+    return $id;
 
 }
 
@@ -889,74 +909,63 @@ sub b_save_buch_autoren {
     my $book_id   = shift;
     my $autor_ids = shift;
 
-    my $K    = ( caller(0) )[3] . "(): ";
+    my $K = ( caller(0) )[3] . "(): ";
+    my $k = $c->log->is_debug() ? $K : '';
 
+    my $schema =  $c->model('Schema')->{'schema'};
     my $storage = $c->model('Schema')->storage;
-    # $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
-    my $dbh = $storage->dbh();
-    # $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
 
     my $anzahl = 0;
     $anzahl = scalar( @$autor_ids ) if $autor_ids and ref($autor_ids) and ref($autor_ids) eq 'ARRAY';
 
-    my $in = '';
-    $in = '(' . join( ", ", map { $dbh->quote($_) } @$autor_ids ) . ")" if $anzahl;
-
-    my $sql;
-    $sql = <<ENDE;
-DELETE FROM `autor2buch`
- WHERE `buch_id` = ?
-ENDE
-    $sql .= "   AND `autor_id` NOT IN " . $in . "\n" if $anzahl;
-
-    my $qparams = [];
-    push @$qparams, $book_id;
-
-    if ( $storage->debug() ) {
-        my $text = $sql;
-        $text =~ s/\s+$//;
-        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-        warn $text;
-    }
-
-    my $sth = $dbh->prepare($sql);
-    $sth->execute( @$qparams );
-
-    return 1 unless $anzahl;
-
-    my $i = 0;
-
-    $sql = <<ENDE;
-INSERT INTO `autor2buch` (
-    `buch_id`, `autor_id`, `ord_num`
-  ) VALUES (
-    ?, ?, ? )
-  ON DUPLICATE KEY UPDATE
-    `ord_num` = ?
-ENDE
-
-    $sth = $dbh->prepare($sql);
+    # Alles, was innerhalb einer Transaktion ablaufen soll ...
+    my $txn_code = sub {
 
-    for my $autor_id ( @$autor_ids ) {
-
-        $qparams = [];
-        push @$qparams, $book_id;
-        push @$qparams, $autor_id;
-        push @$qparams, $i;
-        push @$qparams, $i;
+        my $search_opts = {};
+        $search_opts->{'buch_id'} = $book_id;
+        if ( $anzahl ) {
+            if ( $anzahl > 1 ) {
+                $search_opts->{'autor_id'} = { '-not_in' => $autor_ids };
+            }
+            else {
+                $search_opts->{'autor_id'} = { '!=' => $autor_ids->[0] };
+            }
+        }
+        $c->log->debug( get_output_string( $K . "Suchoptionen: ", $search_opts ) );
+
+        $c->model('Schema::Autor2buch')->search( $search_opts )->delete();
+
+        if ( $anzahl ) {
+
+            my $i = 0;
+            for my $autor_id ( @$autor_ids ) {
+                my $row_hash = {
+                    'buch_id'  => $book_id,
+                    'autor_id' => $autor_id,
+                    'ord_num'  => $i,
+                };
+                $c->log->debug( get_output_string( $K . "Autor2Buch-Hash: ", $row_hash ) );
+                $c->model('Schema::Autor2buch')->update_or_create( $row_hash, { 'key' => 'buch_id' } );
+                $i++;
+            }
 
-        if ( $storage->debug() ) {
-            my $text = $sql;
-            $text =~ s/\s+$//;
-            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-            warn $text;
         }
 
-        $sth->execute( @$qparams );
+    };
+
+    # Und ab ...
+    eval {
+        $schema->txn_do($txn_code);
+    };
 
-        $i++;
+    if ($@) {                             # Transaction failed
+        my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+        die $msg . "\n";
+        return undef;
     }
 
+    return $book_id;
+
 }
 
 #-----------------------------------------------------------------------------------
@@ -971,74 +980,66 @@ sub b_save_buch_serien {
     my $book_id    = shift;
     my $serien_ids = shift;
 
-    my $K    = ( caller(0) )[3] . "(): ";
+    my $K = ( caller(0) )[3] . "(): ";
+    my $k = $c->log->is_debug() ? $K : '';
 
+    my $schema =  $c->model('Schema')->{'schema'};
     my $storage = $c->model('Schema')->storage;
-    # $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
-    my $dbh = $storage->dbh();
-    # $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
 
     my $anzahl = 0;
     $anzahl = scalar( @$serien_ids ) if $serien_ids and ref($serien_ids) and ref($serien_ids) eq 'ARRAY';
 
-    my $in = '';
-    $in = '(' . join( ", ", map { $dbh->quote($_) } @$serien_ids ) . ")" if $anzahl;
-
-    my $sql;
-    $sql = <<ENDE;
-DELETE FROM `buch2serie`
- WHERE `buch_id` = ?
-ENDE
-    $sql .= "   AND `serien_id` NOT IN " . $in . "\n" if $anzahl;
-
-    my $qparams = [];
-    push @$qparams, $book_id;
-
-    if ( $storage->debug() ) {
-        my $text = $sql;
-        $text =~ s/\s+$//;
-        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-        warn $text;
-    }
-
-    my $sth = $dbh->prepare($sql);
-    $sth->execute( @$qparams );
-
-    return 1 unless $anzahl;
+    # Alles, was innerhalb einer Transaktion ablaufen soll ...
+    my $txn_code = sub {
 
-    my $i = 1;
-
-    $sql = <<ENDE;
-INSERT INTO `buch2serie` (
-    `buch_id`, `serien_id`, `ord_num`
-  ) VALUES (
-    ?, ?, ? )
-  ON DUPLICATE KEY UPDATE
-    `ord_num` = IFNULL( `ord_num`, ? )
-ENDE
-
-    $sth = $dbh->prepare($sql);
-
-    for my $serien_id ( @$serien_ids ) {
+        my $search_opts = {};
+        $search_opts->{'buch_id'} = $book_id;
+        if ( $anzahl ) {
+            if ( $anzahl > 1 ) {
+                $search_opts->{'serien_id'} = { '-not_in' => $serien_ids };
+            }
+            else {
+                $search_opts->{'serien_id'} = { '!=' => $serien_ids->[0] };
+            }
+        }
+        $c->log->debug( get_output_string( $K . "Suchoptionen: ", $search_opts ) );
+
+        $c->model('Schema::Buch2serie')->search( $search_opts )->delete();
+
+        if ( $anzahl ) {
+
+            for my $serien_id ( @$serien_ids ) {
+                my $row_hash = {
+                    'buch_id'   => $book_id,
+                    'serien_id' => $serien_id,
+                };
+                $c->log->debug( get_output_string( $K . "Buch2serie-Hash: ", $row_hash ) );
+                my $rs = $c->model('Schema::Buch2serie')->update_or_new( $row_hash, { 'key' => 'buch_id' } );
+                unless ( $rs->in_storage() ) {
+                    $c->log->debug( $K . "Neues Buch zur Serie." );
+                    $rs->set_column( 'ord_num' => 1 );
+                    $rs->insert();
+                }
 
-        $qparams = [];
-        push @$qparams, $book_id;
-        push @$qparams, $serien_id;
-        push @$qparams, $i;
-        push @$qparams, $i;
+            }
 
-        if ( $storage->debug() ) {
-            my $text = $sql;
-            $text =~ s/\s+$//;
-            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-            warn $text;
         }
 
-        $sth->execute( @$qparams );
+    };
 
-        $i++;
+    # Und ab ...
+    eval {
+        $schema->txn_do($txn_code);
+    };
+
+    if ($@) {                             # Transaction failed
+        my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+        die $msg . "\n";
+        return undef;
     }
 
+    return $book_id;
+
 }
 
 #-----------------------------------------------------------------------------------
@@ -1049,73 +1050,64 @@ ENDE
 
 sub b_save_buch_kategorien {
 
-    my $c             = shift;
-    my $book_id       = shift;
-    my $kategorie_ids = shift;
+    my $c       = shift;
+    my $book_id = shift;
+    my $cat_ids = shift;
 
-    my $K    = ( caller(0) )[3] . "(): ";
+    my $K = ( caller(0) )[3] . "(): ";
+    my $k = $c->log->is_debug() ? $K : '';
 
+    my $schema =  $c->model('Schema')->{'schema'};
     my $storage = $c->model('Schema')->storage;
-    # $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
-    my $dbh = $storage->dbh();
-    # $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
 
     my $anzahl = 0;
-    $anzahl = scalar( @$kategorie_ids ) if $kategorie_ids and ref($kategorie_ids) and ref($kategorie_ids) eq 'ARRAY';
-
-    my $in = '';
-    $in = '(' . join( ", ", map { $dbh->quote($_) } @$kategorie_ids ) . ")" if $anzahl;
-
-    my $sql;
-    $sql = <<ENDE;
-DELETE FROM `buch2kategorie`
- WHERE `buch_id` = ?
-ENDE
-    $sql .= "   AND `kategorie_id` NOT IN " . $in . "\n" if $anzahl;
-
-    my $qparams = [];
-    push @$qparams, $book_id;
-
-    if ( $storage->debug() ) {
-        my $text = $sql;
-        $text =~ s/\s+$//;
-        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-        warn $text;
-    }
+    $anzahl = scalar( @$cat_ids ) if $cat_ids and ref($cat_ids) and ref($cat_ids) eq 'ARRAY';
 
-    my $sth = $dbh->prepare($sql);
-    $sth->execute( @$qparams );
+    # Alles, was innerhalb einer Transaktion ablaufen soll ...
+    my $txn_code = sub {
 
-    return 1 unless $anzahl;
-
-    $sql = <<ENDE;
-INSERT INTO `buch2kategorie` (
-    `buch_id`, `kategorie_id`
-  ) VALUES (
-    ?, ? )
-  ON DUPLICATE KEY UPDATE
-    `kategorie_id` = `kategorie_id`
-ENDE
+        my $search_opts = {};
+        $search_opts->{'buch_id'} = $book_id;
+        if ( $anzahl ) {
+            if ( $anzahl > 1 ) {
+                $search_opts->{'kategorie_id'} = { '-not_in' => $cat_ids };
+            }
+            else {
+                $search_opts->{'kategorie_id'} = { '!=' => $cat_ids->[0] };
+            }
+        }
+        $c->log->debug( get_output_string( $K . "Suchoptionen: ", $search_opts ) );
 
-    $sth = $dbh->prepare($sql);
+        $c->model('Schema::Buch2kategorie')->search( $search_opts )->delete();
 
-    for my $kategorie_id ( @$kategorie_ids ) {
+        if ( $anzahl ) {
 
-        $qparams = [];
-        push @$qparams, $book_id;
-        push @$qparams, $kategorie_id;
+            for my $cat_id ( @$cat_ids ) {
+                my $row_hash = {
+                    'buch_id'      => $book_id,
+                    'kategorie_id' => $cat_id,
+                };
+                $c->log->debug( get_output_string( $K . "Buch2kategorie-Hash: ", $row_hash ) );
+                $c->model('Schema::Buch2kategorie')->update_or_create( $row_hash, { 'key' => 'buch_id' } );
+            }
 
-        if ( $storage->debug() ) {
-            my $text = $sql;
-            $text =~ s/\s+$//;
-            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
-            warn $text;
         }
 
-        $sth->execute( @$qparams );
+    };
 
+    # Und ab ...
+    eval {
+        $schema->txn_do($txn_code);
+    };
+
+    if ($@) {                             # Transaction failed
+        my $msg = $@ =~ /Rollback failed/i ? "Rollback nicht erfolgreich!" : $@;
+        die $msg . "\n";
+        return undef;
     }
 
+    return $book_id;
+
 }
 
 #-----------------------------------------------------------------------------------