I've recently wrote a CGI Perl script that is querying a mysql database.
The only problem is that every time it is loaded by the same user a new
database connection needs to be made which takes time.
I thought it would be nice if I could store that database handle in a
CGI::Session object so I can reuse it every time and not need to re-connect
at least until the cookie used by the CGI::Session object expires.
The code I've used is the following:
It's also based on what I've read from
CGI::Session official documentation :
3 use CGI qw/:standard/;
4 use CGI::Session;
5 use Data::Dumper;
6 use DBI;
7 my $session = new CGI::Session();
8
9 my $dbh;
10 if($session->param('dbh')) {
11 $dbh = $session->param('dbh');
12 } else {
13 $dbh = DBI->connect("dbi:mysql...connection_string_continues_h
+ere");
14 $session->param('dbh',$dbh);
15 $session->save_param();
16 $session->flush;
17 };
So I'm expecting that after these lines are run I get a database handle
fast.I'm not sure if this code is working properly so I've come to ask the
wisdom of the monks to see how to do this?
Also,not really intentionally this arises the question What is really a database handle or
in short a dbh ?
What kind of information does it store ?
What is it really ?
Thank you
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.