initializing global data structures from a db

initializing global data structures from a db

am 11.07.2005 21:23:32 von John Saylor

hi

i have a problem with initializing perl data structures from the db. i
have an init routine that does all kinds of stuff and is called from
startup.pl. one of the things i wanted it to do was to grab some stuff
from the db and populate a hash with it.

btw, i'm not using Class::DBI and to implement it would be far more work
than i'd like to do.

it works on perl 5.8.x but not on earlier versions [upgrading is not an
option at this time]. so i was looking into other ways to do it.

one way that has been suggested is to tie the data structures to
subroutines to do the work of getting the data from the db on their
first call.

any suggestions?

--
\js oblique strategy: do something sudden, destructive and unpredictable

Re: initializing global data structures from a db

am 11.07.2005 22:43:02 von Perrin Harkins

On Mon, 2005-07-11 at 15:23 -0400, John Saylor wrote:
> i have a problem with initializing perl data structures from the db. i
> have an init routine that does all kinds of stuff and is called from
> startup.pl. one of the things i wanted it to do was to grab some stuff
> from the db and populate a hash with it.

Okay, where are you getting stuck?

> it works on perl 5.8.x but not on earlier versions [upgrading is not an
> option at this time].

What does?

- Perrin

Re: initializing global data structures from a db

am 11.07.2005 22:48:09 von mpeters

John Saylor wrote:
> hi
>
> i have a problem with initializing perl data structures from the db. i
> have an init routine that does all kinds of stuff and is called from
> startup.pl. one of the things i wanted it to do was to grab some stuff
> from the db and populate a hash with it.

> one way that has been suggested is to tie the data structures to
> subroutines to do the work of getting the data from the db on their
> first call.

That's probably not the best approach, but it really depends on how
often this data is used, how big it is and how often it changes. If it's
used a lot and isn't really large and rarely changes I would preload at
server startup. That way you only need to touch the db once and all of
your apache children will have reduced memory (since they will all share
it until it's changed) because of Copy-On-Write memory (if your system
has that).

--
Michael Peters
Developer
Plus Three, LP

Re: initializing global data structures from a db

am 11.07.2005 22:57:12 von Barry Hoggard

On Jul 11, 2005, at 3:23 PM, John Saylor wrote:

> hi
>
> i have a problem with initializing perl data structures from the db. i
> have an init routine that does all kinds of stuff and is called from
> startup.pl. one of the things i wanted it to do was to grab some stuff
> from the db and populate a hash with it.

I do stuff like that in modules that are use-d in my startup.pl. They
have code in BEGIN blocks, or just the body of the module outside of
subroutines, and set up hashes that can be reached by a class method
call. Does that not work in your case?


Barry Hoggard

Re: initializing global data structures from a db

am 11.07.2005 23:14:26 von John Saylor

( 05.07.11 16:43 -0400 ) Perrin Harkins:
> Okay, where are you getting stuck?

apache [mod_perl] seg faults on startup.

> > it works on perl 5.8.x but not on earlier versions [upgrading is not an
> > option at this time].

> What does?

calling a subroutine that initializes a global data structure in a
different namespace [ie, %PACKAGE::dbHash].

the subroutine goes to the db, grabs the data, and pushes it onto a
hash. i think the problem was traced to Apache::DBI not being able to
handle the request so early.

i've just found Tie::DBI which may help.

--
\js oblique strategy: decorate, decorate

Re: initializing global data structures from a db

am 11.07.2005 23:26:09 von Perrin Harkins

On Mon, 2005-07-11 at 17:14 -0400, John Saylor wrote:
> ( 05.07.11 16:43 -0400 ) Perrin Harkins:
> > Okay, where are you getting stuck?
>
> apache [mod_perl] seg faults on startup.

Now we're getting somewhere. When exactly does it segfault? What do
you have to take out to make it stop segfaulting? One possible cause
would be storing your database handles in a global or in a closure
variable (as opposed to letting Apache::DBI handle them).

> > > it works on perl 5.8.x but not on earlier versions [upgrading is not an
> > > option at this time].
>
> > What does?
>
> calling a subroutine that initializes a global data structure in a
> different namespace [ie, %PACKAGE::dbHash].

So this all worked fine when you developed it on 5.8, but when you tried
it on 5.6 it segfaulted?

> the subroutine goes to the db, grabs the data, and pushes it onto a
> hash. i think the problem was traced to Apache::DBI not being able to
> handle the request so early.

Apache::DBI handles requests during startup just fine. It will not
persist handles opened during startup.

> i've just found Tie::DBI which may help.

I don't see why it would. Connecting to the database during startup
should be fine, so if it isn't working for you, there is something wrong
with your build or your code.

- Perrin

Re: initializing global data structures from a db

am 12.07.2005 22:51:26 von John Saylor

hi

> On Mon, 2005-07-11 at 17:14 -0400, John Saylor wrote:
> > apache [mod_perl] seg faults on startup.

( 05.07.11 17:26 -0400 ) Perrin Harkins:
> When exactly does it segfault?

it never completes starting up. it segfaults when it goes to the db to
grab the data. i enabled DBI tracing and saw these lines:

! <- DESTROY(DBI::db=HASH(0x989468c))= undef during global destruction
! <- DESTROY(DBI::dr=HASH(0x82889ec))= (not implemented) during global
destruction

which makes me think your guess [below] is accurate

> One possible cause would be storing your database handles in a global
> or in a closure variable (as opposed to letting Apache::DBI handle
> them).

can you point me somewhere to read about this in greater detail so i
might be able to fix it?

--
\js oblique strategy: feedback recordings into an acoustic situation

Re: initializing global data structures from a db

am 12.07.2005 23:30:27 von Perrin Harkins

On Tue, 2005-07-12 at 16:51 -0400, John Saylor wrote:
> it never completes starting up. it segfaults when it goes to the db to
> grab the data.

The best way to debug is usually to take things out until it stops
segfaulting. Just comment things out until it runs. Then tell us what
specific line broke it.

> > One possible cause would be storing your database handles in a global
> > or in a closure variable (as opposed to letting Apache::DBI handle
> > them).
>
> can you point me somewhere to read about this in greater detail so i
> might be able to fix it?

Some of the introductory Perl material here might be useful to you:
http://modperlbook.org/

And of course there is a lot of documentation about globals and closures
in the main Perl docs. The thing you want to avoid here is having a
database handle that doesn't go out of scope, other than the one
Apache::DBI is keeping for you. (It is smart enough to avoid doing that
during server startup.)

- Perrin