DB optimization

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-06-16 12:49:05 +04:00
parent 3ff0f7b936
commit 68268a51b5
2 changed files with 24 additions and 4 deletions

View File

@ -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 {

View File

@ -63,6 +63,10 @@ sub get_echo {
= "<!DOCTYPE html><meta charset=utf8><body><h1>Новые сообщения</h1>\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</p>";
}
# Commit transaction
$db->commit();
print "Messages writed to DB!\n";
}
return $msgs;
}