Krang::DB - provides access to Krang database
# get a database handle use Krang::ClassLoader DB => qw(dbh forget_dbh); $dbh = dbh();
# forget about the current dbh for this instance, triggering a # reconnect on the next call to dbh() forget_dbh();
Use this class to get a DBI handle for the active instance. If the database for this instance does not yet exist, it will be created.
$dbh = dbh()RaiseError => 1 AutoCommit => 1
This call is guaranteed to return the same database handle on subsequent calls within the same instance and process. (Until a call to forget_dbh(), of course.)
This method will croak() if the database to which a connection is
requested does not match the version of Krang which is currently
installed. (This is only evaluated when a new connection is opened,
as opposed to retrieved from cache).
If you don't want the database connection to croak you have to call
dbh() with the ignore_version parameter set:
my $dbh = dbh( ignore_version => 1 );
If you don't want to use a cached handle to the db then you can specifically tell it:
my $dbh = dbh(no_cache => 1);
forget_dbh()dbh() to perform a fresh connect. This is
useful in cases where you know the currently cached dbh() is invalid.
For example, after forking a child process a call to forget_dbh() is
necessary to avoid the parent and child trying to use the same
database connection.
forget_all_dbhs()forget_dbh() is sufficient
but in some cases you want to forget all possible DB handles that could be cached, when
before forking when Addons and Element Libraries are in effect and could have done
their own queries.