DBI connect error handeling.
[prev]
[thread]
[next]
[Date index for 2005/01/17]
Hi, I'm having problems with DBI connect failing and causing the use
statements to fail during compile with the following error:
DBI connect('database=cafedb;host=localhost','cafeuser',...) failed: Can't
connect to MySQL server on 'localhost' (10061) at
C:/Perl/site/lib/Ima/DBI.pm line 317
The code trys to create the table if it doesn't exist, and then uses
set_up_tables to populate the column. The problem comes if the database
server isn't running. Then it gives me the error above. How can I catch
this error and still use set_up_tables to intialize my class? Do I have to
'ping' the database server before I do the require CafeDB::Status in the
code? Would I be better off just manually updating the database objects
every time I add a column to the database tables? Any advise would be
appreciated as I need to catch that there was a connection problem and
display a meaningfull message to the user in the GUI instead of failing to
run and dumping a bunch of errors. I've tried reading though the source
perl modules for DBI, DBD::mysql, Class::DBI, Class:DBI::mysql, Ima::DBI,
and Apache::DBI (for experimenting to see if adding a ping helped) but
package CafeDB::DBI;
use strict;
use base 'Class::DBI::mysql';
use CafeOptions;
#---------------------------------------------------------------------------
--
# Get an options object to grab our dsn, username and password and connect
#---------------------------------------------------------------------------
--
my $opt = CafeOptions->new();
my $dsn = defined $opt->dsn ? $opt->dsn : '';
my $userName = defined $opt->userName ? $opt->userName : '';
my $userPass = defined $opt->userPassword ? $opt->userPassword :'';
#---------------------------------------------------------------------------
--
# Declare our main database connection
#---------------------------------------------------------------------------
--
__PACKAGE__->set_db('Main', $dsn, $userName, $userPass, { AutoCommit =>
0 });
__PACKAGE__->_remember_handle('Main');
#---------------------------------------------------------------------------
--
# Declare our create table statement
#---------------------------------------------------------------------------
--
__PACKAGE__->set_sql(
create_table_innodb => 'CREATE TABLE IF NOT EXISTS %s (%s) TYPE =
InnoDB;');
#---------------------------------------------------------------------------
--
# This creates a table of the type InnoDB
#---------------------------------------------------------------------------
--
sub create_table_innodb
{
my ($class, $table, $schema) = @_;
eval { __PACKAGE__->sql_create_table_innodb($table, $schema)->execute; }
}
package CafeDB::Status;
use base 'CafeDB::DBI';
use CafeDB::Admins;
use CafeDB::Users;
my $stable = "status";
my $stable_schema = q{
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`status` VARCHAR(45) NOT NULL,
PRIMARY KEY(`id`),
INDEX(`status`)};
CafeDB::Status->create_table_innodb($stable, $stable_schema);
CafeDB::Status->set_up_table($stable);
CafeDB::Status->add_constructor(find_status => 'status = ?');
CafeDB::Status->add_constructor(find_id => 'id = ?');
sub new_table {CafeDB::Status->create_table_innodb($stable,
$stable_schema);}
|
(message missing)
|