Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Venerable monks,

I'm looking for a module that will cache a database handle for me and correctly handle when the program forks. I know DBIx::Class does this, but I don't need or want the ache of defining a schema and such, just the handle.

I have a program that uses a database. In it, I have a simple function to get a connection to the database.

sub dbh { my ($u,$p) = ('user',':)'); my $dbh = DBI->connect( "dbi:Sybase:server=sql.example.com", $u, $p, { RaiseError => 1 }, ); $dbh->do( $initial_stuff ); $L->debug( "made new database connection $dbh" ); return $dbh; }

That works fine. It would be easy enough to write something that caches that using a state variable.

sub dbh { state $dbh; if ( ! defined $dbh ) { # blah blah bah } return $dbh; }

That way I don't open up a new connection every time I need to talk to the database. Unfortunately, that will get tripped up when I fork.

I know about DBI, fork, and clone. I know how to write the code to get around this. What I want is a module that's already done it for me. Does it exist?


In reply to Simple DBI handle caching. by kyle

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found