[Mimedefang] initializing database connection

Aleksandar Milivojevic alex at milivojevic.org
Tue Nov 15 11:10:54 EST 2005


I need to connect to SQLite database from some functions in mimedefang-filter. 
Not sure if I got the docs right.  What I currently have is something like
this.  The semaphores stuff is just to serialize transactions (SQLite pukes
when more than one process is accessing same file).  I'm also using semaphores
in other functions, so only one slave at a time can access database.

use DBI;

sub filter_initialize {
    $semid = 0;
    if ($semid = semget($semkey, 1, 0600 | IPC_CREAT | IPC_EXCL)) {
        # we are creating the semaphore, set the green light
        semop($semid, pack("s!3", 0, 1, 0));
    } else {
        # assume semaphore was already created and in use
        $semid = semget(0x12345678, 1, 0600) || die "$!";

    # red light
    semop($semid, pack("s!3", 0, -1, SEM_UNDO));
    if (-f $dbfile) {
        $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
    } else {
        $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
        my $sth_create = $dbh->prepare($sql_create_table);
    $dbh->{AutoCommit} = 0;
    $sth_insert = $dbh->prepare($sql_insert);
    $sth_update = $dbh->prepare($sql_update);
    $sth_select = $dbh->prepare($sql_select);
    $sth_delete = $dbh->prepare($sql_delete);
    # green light
    semop($semid, pack("s!3", 0, 1, SEM_UNDO));

And than I'm using $dbh, $sth_insert, $sth_update and so on in other filter_*
functions (again, using semaphores to serialize access to database).  Is this
the right way to do it?

Even if this is the right way of doing it, if I got manual page right, each
slave will have its own connection to the database.  Since I needed to use
semaphores to serialize things anyhow (AFAIK, limitation of SQLite), is it
possible to (somehow) create one connection and share it among all the slaves?

This message was sent using IMP, the Internet Messaging Program.

More information about the MIMEDefang mailing list