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

storing database handle object in a variable

by Anonymous Monk
on Dec 09, 2009 at 10:40 UTC ( [id://811881]=perlquestion: print w/replies, xml ) Need Help??

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

How to store a database handle object in a variable. i.e.
$dbiConn = DBI->connect( "dbi:Oracle:$ORACLE_SID" , "$ORACLE_USER" , "$ORACLE_PASS" ) || die "Connection failure.\n$!\n$DBI::errstr\n$ORACLE_S +ID, $ORACLE_USER, $ORACLE_PASS";
Is there a way to store $dbh in a variable like
$dbhVar = $dbh; my $qryDbi = $dbiVar->prepare("some query");
Thanks

Replies are listed 'Best First'.
Re: storing database handle object in a variable
by kyle (Abbot) on Dec 09, 2009 at 15:25 UTC

    You may be trying to do this:

    1. Get a $dbh.
    2. Store the $dbh in a file on disk or something.
    3. Retrieve the $dbh and use it in a program other than the one that got it in the first place.

    If that's your goal, you're sunk. The $dbh is not a mere piece of data like an account number or the text of the Camel book that you can write and read back later. Instead, it is an object holding an open socket to a database. The network connection that has been opened can't be serialized for later.

    If you want to use the same connection to a database in two places concurrently, you're still sunk. Only one process can use a connection at a time. To get that behavior, you'd have to have a process that connects to the database and then acts as a traffic cop for other processes that want to use the connection. In that case, you might as well have the other processes connect directly.

    If what you want to do is open a connection to a database and then start another program that uses that connection, you can sort of do that. Have a look at DBI, fork, and clone. for a way to go.

    But really, why do you want to do this? What is the problem you face that causes you to seek this solution?

Re: storing database handle object in a variable
by apl (Monsignor) on Dec 09, 2009 at 12:06 UTC
    On top of everything else said,
    $dbhVar = $dbh; my $qryDbi = $dbiVar->prepare("some query");
    wouldn't work because $dbiVar isn't defined; you obviously meant $dbhVar.
Re: storing database handle object in a variable
by virtualsue (Vicar) on Dec 09, 2009 at 10:49 UTC
    What happens when you try it?
      Sorry to miss the point. This sored database handle object will be used in other script. i.e. it should be persistent for another script.

        Why you want this? May be you better think about separating connection code in a module?

        For persistent database connections, I think you should take a look at using mod_perl or FastCGI, along with something like Apache::DBI. Regular scripts will close the database connection when they exit. I don't think there's anyway to avoid that.
Re: storing database handle object in a variable
by pajout (Curate) on Dec 09, 2009 at 12:45 UTC
    I see this simplest way, or I am missing something important in your query:

    #create database handle: my $dbh = DBI->connect(...) or die "..."; #use the handle directly: $dbh->do("delete from mytable;"); #or prepare query: my $sth = $dbh->prepare("select 1 from dummy;");
    You can find proper snippets in man DBI.

    Definitely, use strict; use warnings, specially if you are not secure about the code - my opinion.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-19 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found