From 68268a51b55bac1c99442415c6007ded56aee566 Mon Sep 17 00:00:00 2001 From: "Difrex(Denis Zheleztsov)" Date: Mon, 16 Jun 2014 12:49:05 +0400 Subject: [PATCH] DB optimization --- II/DB.pm | 19 +++++++++++++++++-- II/Get.pm | 9 +++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/II/DB.pm b/II/DB.pm index e44d628..caeea1d 100644 --- a/II/DB.pm +++ b/II/DB.pm @@ -20,6 +20,22 @@ sub new { return $self; } +sub begin { + my ($self) = @_; + my $dbh = $self->{_dbh}; + + # Begin transaction + $dbh->do('BEGIN'); +} + +sub commit { + my ($self) = @_; + my $dbh = $self->{_dbh}; + + # Commmit transaction + $dbh->do('COMMIT'); +} + sub write_out { my ( $self, %data ) = @_; my $dbh = $self->{_dbh}; @@ -51,8 +67,7 @@ sub write { my $sth = $dbh->prepare($stmt); $sth->execute(@bind); - - print "Message writed to DB!\n"; + $sth->finish(); } sub select_out { diff --git a/II/Get.pm b/II/Get.pm index a1e1843..4f57ba8 100644 --- a/II/Get.pm +++ b/II/Get.pm @@ -63,6 +63,10 @@ sub get_echo { = "

Новые сообщения

\n"; if ( defined($msgs) ) { my @msg_list = split /\n/, $msgs; + + # Begin transaction + print "Writing messages\n"; + $db->begin(); while (<@msg_list>) { my $mes_hash = $_; @@ -104,9 +108,10 @@ sub get_echo { # Write message to DB $db->write(%data); - - # $new_messages .= "$post

"; } + # Commit transaction + $db->commit(); + print "Messages writed to DB!\n"; } return $msgs; }