Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
The stupid question is the question not asked
 
PerlMonks  

DB Connection using CGI::Application

by Anonymous Monk
on Apr 24, 2013 at 15:18 UTC ( #1030438=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to work with this code that uses CGI::Application and I am having an issue where in the "cgiapp_init" I need to connect to two different databases, it works on the first one but if I try to pass the dbh value for the second it doesn't work, maybe I am passing it wrong, if someone has any idea it would be great:
... sub cgiapp_init { my $self = shift; my $db = 'myServer'; my $user = 'user'; my $pass = 'pass'; my $sql_dbh = DBI->connect( "DBI:ODBC:$db", $user, $pass ) || print "Connect fail: $!"; my $as400_db = 'AS400'; my $as400_user = 'user; my $as400_pass = 'pass'; my $as400_driver = DBI->connect( "DBI:ODBC:$as400_db", $as400_use +r, $as400_pass ) || print "Connect fail: $!"; $self->param('mydbh' => $mssql_dbh, 'mydbh400' => $as400_driver ) +; } ... sub one { my $self = shift; # Get database connection my $dbh = $self->param('mydbh'); ... } sub two { my $self = shift; # Get database connection my $dbh = $self->param('mydbh400'); ... } ...
Thanks for the Help!

Comment on DB Connection using CGI::Application
Download Code
Re: DB Connection using CGI::Application
by Anonymous Monk on Apr 24, 2013 at 15:25 UTC

    for the second it doesn't work,

    what does that mean, it explodes?

Re: DB Connection using CGI::Application
by hdb (Parson) on Apr 24, 2013 at 15:29 UTC

    Just seeing that there is a variable $sql_dbh to open the ODBC connection but you store $mssql_dbh under param('mydbh'). Probably only a typo in your posting.

      Yes it was a typo, it should be $sql_dbh, trying to make it simple and a typo happened, and the connection doesn't work trying to pass both values thats what I am trying to explain.

        and the connection doesn't work trying to pass both values thats what I am trying to explain.

        But doesn't work is not diagnostic

Re: DB Connection using CGI::Application
by poj (Curate) on Apr 24, 2013 at 16:12 UTC
    try
    $self->param('mydbh',$sql_dbh); $self->param('mydbh400',$as400_driver);
    poj
Re: DB Connection using CGI::Application
by sundialsvc4 (Monsignor) on Apr 24, 2013 at 19:27 UTC

    If you are trying to pass a database connection-handle as a param(), then I would say .. “fuhgeddaboudit!.”   You’re barking up the wrong tree .. the intended solution is actually much simpler(!) than you are making it out to be.

    Pause for a moment and consider how CGIApp actually works.   (See also http://cgi-app.org/index.cgi?OrderOfOperations.)

    Your CGI application comes into existence to handle one request ... then, it dies.   The sequence of operations, during the lifetime of that one-and-only request, includes the following method calls:

    1. cgiapp_init()
    2. setup()
    3. your request handler, such as 'one' or 'two'
    Therefore ... the object exists for the entire time!

    Each of these methods occur within a single object, so you actually are free to instantiate any necessary database connection-handles at any point that suits you.   The only thing to bear in mind, from a program-design point of view, is that each of these are method calls, to a single (albeit short-lived...) object, which occur in a definite sequence.

    Literally...

    • The CGI process starts.   The object is instantiated.
    • The method-calls occur in the stated sequence.
    • The object coughs-up its output .. then the entire process dies.   (Hey.. life is short... sux.)

    I cordially suggest that you should now spend a bit of time at http://www.cgi-app.org.   Read it “entirely, thoughtfully, and carefully.”   Trust me ... the proverbial “little light” will come on.   :-)

      I will do that, can I get back to you?

      If you are trying to pass a database connection-handle as a param(), then I would say .. “fuhgeddaboudit!.”

      Typical sundialsvc4 , didn't read the material he advocates reading

Re: DB Connection using CGI::Application
by scorpio17 (Monsignor) on Apr 24, 2013 at 21:53 UTC

    You need to be using CGI::Application::Plugin::DBH, if you're not already, then do something like this:

    sub cgiapp_init { my $self = shift; ... $self->dbh_config('mydbh', [$dsn1, $user1, $pass1, \%options1,]); $self->dbh_config('mydbh400', [$dsn2, $user2, $pass2, \%options2,]); } sub one { my $self = shift; my $dbh = $self->dbh('mydbh'); ... } sub two { my $self = shift; my $dbh = $self->dbh('mydbh400'); ... }

      You need to be using ...

      param will work, plugins aren't mandatory

Re: DB Connection using CGI::Application
by sundialsvc4 (Monsignor) on Apr 25, 2013 at 12:53 UTC

    Good morning .. did someone forget to log-in first?

    The thing that I was trying to explain is simply how a CGI::Application process works.   An object that descends from that base class is instantiated, then you call its run() method.   During the object’s constructor, new(), and within run(), a series of method calls occur ... init, setup, page-handler, postrun, and so on.   This means that the object exists the entire time.   (And, once the CGI request is finished, everything goes away.)

    This, therefore, is how you take care of your database-handles or anything else that you need to refer during the course of the request.   Simply store them as properties of your object:   $self->{'dbh_1'} = ... ;.   Set these up, say, in the appropriately-named setup() method.   Or write a short subroutine that instantiates them on-demand.   It’s entirely up to you.

    Yes, there are plenty of plugins available.

    Could you use param()?   Take a look at its implementation and you will see that the answer is obviously “yes.”   But you don’t have to, and, I tend to think, that’s not really “the designer’s intent.”

      Good morning .. did someone forget to log-in first? ..

      Clever trick, responding to someone else again, you're number one troll

      The thing that I was trying to explain is simply how ...

      Yes, like you keep explaining, you don't care what the question is, you simply ignore it and tangent on with irrelevant tangents -- one connection works, the other one doesn't -- must be request cycle .... nonsense

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030438]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-04-24 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls