DBI connect error handeling.

[prev] [thread] [next] [Date index for 2005/01/17]

From: Brian Doig
Subject: DBI connect error handeling.
Date: 00:12 on 17 Jan 2005
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)

subclassing, relationship, has_a problem patch
Aleksandr Guidrevitch 13:53 on 15 Jan 2005

Re: subclassing, relationship, has_a problem patch
Aleksandr Guidrevitch 15:36 on 16 Jan 2005

Re: subclassing, relationship, has_a problem patch, revisited
Aleksandr Guidrevitch 16:12 on 16 Jan 2005

DBI connect error handeling.
Brian Doig 00:12 on 17 Jan 2005

RE: DBI connect error handeling.
Brian Doig 00:16 on 17 Jan 2005

Generated at 17:42 on 27 Jan 2005 by mariachi v0.52