[Dbix-class] An idea about "disconnecting cleanly"

Leandro Hermida leandro.hermida at unibas.ch
Mon Feb 6 23:44:24 CET 2006


Hello,

My apologies if this is not the right forum to express any ideas, this is my
first time ever writing to a development list.  I really like DBIx::Class
and have been testing it with some of my development databases.  I have an
idea about the area of DBIx::Class::Manual::Cookbook section called
"disconnecting cleanly".  In my homemade (and simple) ORM that we use here
the class library that makes up the ORM is used in projects not only for the
project web application, for example, but for underlying daemons, web
services and scripts working in conjunction with the web application.
Therefore it is important to have a robust way of cleaning up the ORM
objects if there are any kinds of signals that cause daemons or services to
stop.  The way I wrote it in my ORM (shown below) from what I understand
about Perl is that I needed to specify a DESTROY subroutine in the class
that contains the DBI database handle (dbh) and in the DESTROY subroutine I
first do a rollback (for transactions -- so as to not implicitly commit any
inserts/updates/deletes which some DBDs will implicitly do) and then do the
database handle disconnect.  In addition, for the base class which contains
the DBI statement handle (sth) I also have a DESTROY subroutine which calls
the finish method since some DBDs need this to happen before the statement
handle is cleaned up.  Then, most importantly, in the application code I use
the sigtrap pragma and tell it to die cleanly if any types of signals are
received.  From what I read, the die causes Perl to properly destroy objects
and call all of their DESTROY subroutines before terminating.

The code presented in the DBIx::Class::Manual::Cookbook "disconnecting
cleanly" section I don't think will work as robustly.  But I could be wrong.
Just trying to help and throw my ideas out.

Cheers,

Leandro Hermida


# In the base database class (all database classes inherit from this (e.g.
My::DB))

package DB;

... constructor, methods, instance data ...

sub DESTROY {
    my $self = shift;
    if (defined $self->dbh) {
        $self->dbh->rollback;
        $self->dbh->disconnect;
    }
}


# In the base adaptor class (all table adaptor classes inherit from this)
# Table adaptor classes insert, update, delete objects of that table
# I think this class in our system's is equivalent to DBIx::Class's
ResultSet class

package Adaptor;

... constructor, methods, instance data ...

sub DESTROY {
    my $self = shift;
    if (defined $self->sth) {
        $self->sth->finish;
    }
}


# In the application code

use sigtrap qw(die normal-signals error-signals);





More information about the Dbix-class mailing list