In code I'm working on at present I have two classes of interest. One class is intended for creating a single instance from that manages the database connection. The second class is the base class for a bundle of classes that mediate access to different tables in the database.

The table accessors don't require a connection at create time and can do a limited range of stuff without a connection being provided, but they die if you try to use them in a way that would require a connection and you've not provided one yet. That allows a Task object to be created for example with a bunch of task information, then handed over to a scheduler that gets the task queued (added to the task table).

The table object base class provides members to insert a derived instance into its table and to update/fetch/delete entries matching various criteria. The base class will also create the table for a derived class if the table doesn't exist (the derived class provides a column spec). Typical derived class code looks like:

package Job; require Exporter; our @ISA = ('DBObject'); our @EXPORT = qw( ); my $jobTableDef = ['jobs', # Job specifications 'id INTEGER PRIMARY KEY NOT NULL, command VARCHAR(1024) NOT NULL, maxrunmins INTEGER, numruns INTEGER, runtimetotal INTEGER, flags VCHAR(20) ' ]; sub new { my ($class, %params) = @_; $params{flags} = '' unless exists $params{flags}; return DBObject::new ($class, %params, tableDef => $jobTableDef); } 1;

and typical usage looks like:

sub CheckTasks { my $self = shift; my $task = Task->new (dbh => $self->{db}); my @readyTasks = $task->getOldest (); if (@readyTasks) { my $taskInfo = $task->fetch (id => $readyTasks[0][0]); my $job = Job->new (dbh => $self->{db}); my $jobInfo = $job->fetch (id => $taskInfo->{jobid});

DWIM is Perl's answer to Gödel

In reply to Re: How to share DBI connections between objects? by GrandFather
in thread How to share DBI connections between objects? by blahblah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.