Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database connection

Status
Not open for further replies.

campbellc

IS-IT--Management
Jul 3, 2007
26
US
I am trying to test some error logic for my database conneciton. Should the connection not be established the program should issue the "die" signal:

my $dbh = DBI->connect($DSN, $username, $passwrd) or die "$DBI::errstr\n";

But it is not. It is issuing a warning or that's what I think it is doing as the "die" subroutine is not being executed.

Within the DBI function module, does it override the "die" with a "warn"???

Thanks...
-Chris
 
What warning are you getting?

Die works just fine there as I use it all the time.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
I'm not getting any error. Just the fact that the "die" subroutine is not being executed, only the warn subroutine is being executed.
 
Code:
$DSN = 'dbi:PgPP:dbname=test';
$user = 'test';
$passwd = 'test';

use DBI;

$dbh = DBI->connect($DSN, $user, $passwd) or die "Error: $DBI::errstr\n";

returns
DBI connect('dbname=test','test',...) failed: Couldn't connect to /tmp/.s.PGSQL.5432: at C:/Perl/site/lib/DBD/PgPP.pm line 124
at C:\test.pl line 8
Error: Couldn't connect to /tmp/.s.PGSQL.5432: at C:/Perl/site/lib/DBD/PgPP.pm
line 124

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
I'm not getting any error. Just the fact that the "die" subroutine is not being executed, only the warn subroutine is being executed.

Well, what the warning you're getting?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sub WARN_handler
{
print STDOUT "Inside the warn handler\n";
my($signal) = @_;
warnToLogfile("WARN: $signal");
return();
}

sub DIE_handler
{
print STDOUT "Inside the die handler\n";
my($signal) = @_;
sendToLogfile("DIE: $signal");
exit();
}

sub warnToLogfile
{
print STDOUT "Inside WARN subroutine\n";
my(@array) = @_;
print RESULTS (@array) if ( -w $outfile );
print ERRLOG ("\n");
print ERRLOG scalar localtime();
print ERRLOG (@array);
close( IN );
$subject = "Error occured during FTP Outbound Processing";
# &emailnotify;
&datetime;
print STDOUT "Going to rename the file : $file\n";
my $oldfile = "$dirname"."\\"."$file";
my $newfile = "$dirname"."\\"."send_failed"."_".$day.$hour.$min.$sec;
rename( $oldfile, "$newfile" ) or warn "Cound not rename file $!\n";
print STDOUT "Going to close the file in WARN subroutine\n";
$command = "next OUTER;";
$/ = "~";
$counter = 0;
return();
}

sub sendToLogfile
{
my(@array) = @_;
print ERRLOG ("\n");
print ERRLOG scalar localtime();
print ERRLOG (@array);
$subject = "Problem encountered with database connection";
# &emailnotify;
close ( ERRLOG );
return();
}

Thu Jul 5 16:28:21 2007WARN: DBI connect('DRIVER={SQL Server};Server=VITxx32;attr=database=EDIDB','reports',...) failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001)
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000)
[Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (SQL-01S00)(DBD: db_login/SQLConnect err=-1) at C:\PerlWorkFolder\outbound_ftp.pl line 89

I'm also getting the "STDOUT" message in the warn subroutine that I have placed to show me where my code is going.
 
Can anyone tell me if the DBI module in and of itself would issue a “warn” signal before the “die” signal that I coded in the program. I’ve made some changes based on previous suggestions and am now getting the following of both the “WARN”[/color red] and the“DIE” [/color red] error messages.

Also looking at the structure and the content of the error message they are both different. So I’m really grasping at air, but something the DBI connect is issuing the warning.

Looking forward to your replies…
-Chris

Fri Jul 6 14:41:03 2007WARN: DBI connect('DRIVER={SQL Server};Server=VITzz32;attr=database=EDIDB','reports',...) failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001)
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000)
[Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (SQL-01S00)(DBD: db_login/SQLConnect err=-1) at C:\PerlWorkFolder\outbound_ftp.pl line 84

Fri Jul 6 14:41:03 2007DIE: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001)
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000)
[Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (SQL-01S00)(DBD: db_login/SQLConnect err=-1) at C:\PerlWorkFolder\outbound_ftp.pl line 84.
 
<rant>
I would just like to point out that this endeavor feels like it's wasting time. Why does it matter if DBI is issuing a warning before the die statement is called? I honestly haven't heard of such an issue before, but the primary goal here should be to simply get a connection that works.

The errors are simply a means to help you create a correctly formatted connect string. Is this not your goal?
</rant>

- Miller

 
Thank you Miller for the “rant”.

The connection to the database works just fine.

I am attempting, as I would hope most programmers would do, would be to make code that works and functions as intended. I’m working for a company that would let a production system go without a backup for over a month. My technical training and 20+ years in the computer industry would tell me this is absolutely stupid…but yet it was done as no one could seem to get off there backside to do the work. I say this as a point of reference to the quick response in problem resolution that this company does not seem to have.

So to make things as error proof, bullet proof and simpleton proof, I am trying to design a program that reports on an error, the exact error, so that some yahoo behind me can figure it out. Unfortunately you have me at a disadvantage to Perl Coding as I’ve just started a new project that no one else in the company deem worthy enough to take on…as the company was not going to pay for education or training in Perl.

The relevancy of the warning being issues is the fact that additional code is being executed for a given reason in the warn subroutine that is not being executed in the die subroutine. I’m trying to do a complete, full and total unit as well as system test. I would assume the goal of a good program is to function as intended. Perhaps the intent based on lack of knowledge of Perl was wrong.

I apologize that your time was wasted but mine wasn’t if I can continue to grow and become half the Perl programmer that you seem to be.

Thanks…
Chris
 
Why not look at the DBI module itself and see what it has to say about it.

If you look at my example above you will see that first the module complains about the DB connection then I actually die from my || die statement. If that is how it behaves I would just document that as expected behavior.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Maybe what you are seeing is just what is going to STDOUT.. why not capture your errors like this? (untested and definitely needs work)

Code:
$DSN = 'dbi:PgPP:dbname=test';
$user = 'test';
$passwd = 'test';


use DBI;

$dbh = DBI->connect($DSN, $user, $passwd) or &loganddie("Error: $DBI::errstr");

sub loganddie {
	open(LOG, ">>log.txt");
	print LOG "@_\n";
	close(LOG);
}

Now i get the warning to STDOUT but the real error message that I care about to log.txt

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Hello Chris,

I didn't mean to disparage your choice of where to put your time. I was simply wanting to relate that the best use of a database is when you don't even know that you're using one. All of my database tables are encapsulated by classes. It's been so long since I've regularly dealt with a database that I have to look up how to connect and what the proper syntax is now to remember.

However, I failed to realize that this was a continuation of another thread.


It would have been better if you had continued the in the same thread as this is essentially the same issue/goal that you had before, and I honestly tend to forget questions and people immediately after helping them. Knowing that it's the same issue now and after reviewing your script posted in the other thread, I understand better what it is that you're trying to accomplish, and what I said doesn't really apply.

Anyway, I don't really have time to refactor your entire script right now, which is honestly what it kind of needs. As you say, there are a lot of design practices that take a while to learn and apply. But it's really good that you at least attempting to do that.

I might post back here later on if I find some time.

- M
 
I included a DBI->trace in the execution of the database connection and I see a warn statement. I don't know if this is causing the program to go into the wrong section of code. But it seem interesting that it exists in the trace.

Code:
DBI 1.57-ithread default trace level set to 0x0/2 (pid 3512) at outbound_ftp.pl line 79
    -> DBI->connect(dbi:ODBC:DRIVER={SQL Server};Server=VITzz32;attr=database=EDIDB, reports, ****)
    -> DBI->install_driver(ODBC) for MSWin32 perl=5.008008 pid=3512 ruid=0 euid=0
       install_driver: DBD::ODBC version 1.13 loaded from C:/Perl/site/lib/DBD/ODBC.pm
    <- install_driver= DBI::dr=HASH(0x1d58af4)
    !! [COLOR=red][b]warn[/b][/color red]: 0 CLEARED by call to connect method
    -> connect for DBD::ODBC::dr (DBI::dr=HASH(0x1d58af4)~0x1d58b60 'DRIVER={SQL Server};Server=VITzz32;attr=database=EDIDB' 'reports' **** HASH(0x198f928)) thr#225014
db_login/SQLConnect error -1 recorded: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001)

I have reviewed the release notes and from what I can understand, there is no documentation for this warning and why. Perhaps I am taking this a little too deep and should just develop code around the various idiosyncrasies. But if anyone can shed light on this or perhaps how to get around it, I would greatly appreciate it.

-Chris
 
Thank you everyone for all your help. I think I have figured it out...at least the program is acting the way I want.

FYI:
According to the PERL DBI book Chapter 4, two additional argument can be passes in the connection string (PrintError and RaiseError). The PrintError will signal a "WARNING" if enabled and the RaiseError will signal a "DIE" is enabled.

Code:
my $dbh  = DBI->connect($DSN, $username, $passwrd, {
                       PrintError => 0,   ###disable warning
                       RaiseError => 1    ###report error through die
                       }) or die "Can't connect to the database: $DBI;;errstr\n";

Code:
If both RaiseError and PrintError are enabled, an error will cause warn( ) and die( ) to be executed sequentially

 


Programming the Perl DBI
Database programming with Perl
By Alligator Descartes & Tim Bunce
1st Edition February 2000
1-56592-699-4, Order Number: 6994
350 pages, $34.95
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top