Hi,
I have a question regarding how DB connections work within a single process perl script. Hope someone doesn't mind giving me a few pointers.

If I was to have a sub routine that setups a connection and returns a connection handler variable (or perhaps a reference), such as the code shown below. If I then call this sub everytime I need to make a query (my $dbh = &dbConnect), am I right in thinking that a new connection would NOT be openend every time but that DBI would use an existing connection if one existed and was not already in use; assuming that I hadn't yet called disconnect()

My second question is: Is the connection opened on the call to connect or is it delayed until the first query is prepared?

Regards to all,
Cyril

sub one { my $dbh = &dbConnect; # query continues here } sub two { my $dbh = &dbConnect; # query continues here } sub dbConnect { my $dsn = "DBI:mysql:database:localhost"; my $db_user_name = 'username'; my $db_password = 'password'; my $dbh; $dbh = DBI->connect ($dsn, $db_user_name, $db_password, { RaiseError => 1 } ) or die("cannot connect to DB: ".DBI::errstr. +"\n",$dbh); return $dbh; }

In reply to DBI / Mysql connections by cyril

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.