Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Command line Persistent DBI connections

by hakkr (Chaplain)
on Dec 13, 2004 at 16:05 UTC ( [id://414421]=perlquestion: print w/replies, xml ) Need Help??

hakkr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Obviously if you are running scripts at command line via cron etc you cant use Apache::DBI as you are not in a mod_perl environment.

So does this mean every time my cron runs I have to connect? Can I somehow grab a connection out of one of the Apache children? or is the only choice to run scripts as daemons with DBI connect_cached()
  • Comment on Command line Persistent DBI connections

Replies are listed 'Best First'.
Re: Command line Persistent DBI connections
by perrin (Chancellor) on Dec 13, 2004 at 16:10 UTC
    Connecting once per run via connect_cached() doesn't sound so terrible to me, but you could also just have your cron trigger a mod_perl handler that does the work by making an LWP request to your local server.
Re: Command line Persistent DBI connections
by mpeppler (Vicar) on Dec 13, 2004 at 16:18 UTC
    I agree with perrin - connecting once per cron job seems OK to me. The startup time of each cron job will likely dwarf the time it takes to establish the connection. If opening a new connection for each cron job really is a problem then I think you should consider daemonizing some of those cron jobs anyway.

    Michael

Re: Command line Persistent DBI connections
by Thilosophy (Curate) on Dec 14, 2004 at 06:06 UTC
    I agree with everyone that connecting once at the beginning of every cron run (that would be once a minute at the most, right?) is not an issue.

    If for some reason you really want a persistant Perl environment for cron jobs, have a look at PPerl on CPAN. This way, you can keep a database connection (and other things you might want to cache).

    But again, connecting every time seems okay. Since you are not using the connection in between cron runs, keeping it open just wastes resources on the DB server.

      pperl looks very interesting, mod_perl for command the command line. can you use that to test modperl scripts?

      the reason i ask about the crons is if you have a lot of web servers all running multipe crons against a single db then a lot of connections can start to occur.

      I suppose the frequency of the crons in question will dictate the need for persistance. if it they are every minute it might be worthwhile

      cheers
        pperl looks very interesting, mod_perl for command the command line. can you use that to test modperl scripts?

        Not really. If your modperl script is just "CGI on steroids" (still using the standard CGI interface) you test it by running it as plain CGI. If your modperl script is making use of Apache's internal interfaces, you have to have mod_perl to test it.

        the reason i ask about the crons is if you have a lot of web servers all running multipe crons against a single db then a lot of connections can start to occur

        I would say having a lot of crons on a lot of servers is rather a reason to not have persistant connections, because then you have a lot of persistent connections hanging around mostly idle. If you want all your cron jobs (on all servers) to share the same connection, you could move the logic into a mod_perl script that you poll via LWP as suggested by Perrin. This will probably not work very well if the query takes a lot of time.

        How many servers are we talking here? Go with new connections every time. Chances are your database can handle it just fine. The actual queries are likely to place more of a burden on it than establishing the connections anyway.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://414421]
Approved by mpeppler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found