Detecting connection errors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting connection errors

Alberto Luaces
Hi,

I stumped over a problem when learning to use DBIx::Class.  I had a typo
on the filename holding the database, but no exception was thrown until
I wanted to retrieve some results, which was not intuitive for me.  How
do you check for a successful connection?

--8<---------------cut here---------------start------------->8---
use My::Schema;

# Does not die even the filename is wrong!
my $r = My::Schema->connect("dbi:SQLite:expgraf.db") or die('conn error');
--8<---------------cut here---------------end--------------->8---

Thanks,

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

David Ihnen-2
I think we did something silly like doing a meaningless select/ping type operation just to make sure everything was cool in the DBIx application I worked on years ago.  There may be a better answer these days though?

David

On Wed, Oct 14, 2015 at 12:53 PM, Alberto Luaces <[hidden email]> wrote:
Hi,

I stumped over a problem when learning to use DBIx::Class.  I had a typo
on the filename holding the database, but no exception was thrown until
I wanted to retrieve some results, which was not intuitive for me.  How
do you check for a successful connection?

--8<---------------cut here---------------start------------->8---
use My::Schema;

# Does not die even the filename is wrong!
my $r = My::Schema->connect("dbi:SQLite:expgraf.db") or die('conn error');
--8<---------------cut here---------------end--------------->8---

Thanks,

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...



--
David Ihnen
Voice contact (562) 743-1807

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Dagfinn Ilmari Mannsåker
In reply to this post by Alberto Luaces
Alberto Luaces <[hidden email]> writes:

> Hi,
>
> I stumped over a problem when learning to use DBIx::Class.  I had a typo
> on the filename holding the database, but no exception was thrown until
> I wanted to retrieve some results, which was not intuitive for me.  How
> do you check for a successful connection?

First of all, you never need to do "or die ..." with DBIx::Class
operations, since they always throw exceptions on error.

Sencondly, DBIx::Class only hits the database when it absolutely has to,
e.g. to fetch restults.  This includes the initial connection.  However,
you can force it to connect early by calling

   $schema->storage->ensure_connected;

Which will throw an exception if it fails to connect.

--
"The surreality of the universe tends towards a maximum" -- Skud's Law
"Never formulate a law or axiom that you're not prepared to live with
 the consequences of."                              -- Skud's Meta-Law


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Alberto Luaces
[hidden email] (Dagfinn Ilmari Mannsåker) writes:

> Alberto Luaces <[hidden email]> writes:
>
>> Hi,
>>
>> I stumped over a problem when learning to use DBIx::Class.  I had a typo
>> on the filename holding the database, but no exception was thrown until
>> I wanted to retrieve some results, which was not intuitive for me.  How
>> do you check for a successful connection?
>
> First of all, you never need to do "or die ..." with DBIx::Class
> operations, since they always throw exceptions on error.
>

Ok. I was just trying after realizing no exception was happening.
Actually I was using the autodie module in the beginning.

>
> Sencondly, DBIx::Class only hits the database when it absolutely has
>to,
> e.g. to fetch restults.  This includes the initial connection.  However,
> you can force it to connect early by calling
>
>    $schema->storage->ensure_connected;
>
> Which will throw an exception if it fails to connect.

Yes, it sounds sensible for an ORM to be lazy, but I did not imagine
that it could even involve connections.  Thanks a lot for the pointer.

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Alberto Luaces
Alberto Luaces <[hidden email]> writes:

>> Sencondly, DBIx::Class only hits the database when it absolutely has
>>to,
>> e.g. to fetch restults.  This includes the initial connection.  However,
>> you can force it to connect early by calling
>>
>>    $schema->storage->ensure_connected;
>>
>> Which will throw an exception if it fails to connect.
>
> Yes, it sounds sensible for an ORM to be lazy, but I did not imagine
> that it could even involve connections.  Thanks a lot for the pointer.

Oops, I spoke too soon.  ensure_connected is not throwing.  Here is the
code for reference:

 1 use lib './lib';
 2 use DAAO;
 3
 4 my $r = DAAO->connect("dbi:SQLite:non-existent.db");
 5 $r->storage->ensure_connected;
 6
 7 my $resultset = $r->resultset('Alumno')->search;
 8
 9 my $person = $resultset->next; # Iterator
10
11 print $person->asistencia;

It does not crash until line 9, when the iterator is used.

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Will Crawford
This is because you are using sqlite. You could be intending to call
->deploy() rather than trying to select data. Sqlite will create the
file for you (complete with typo) in this case. But as you haven't
created any tables in the alternately-spelt database file, you'll get
an exception.

On 14 October 2015 at 20:14, Alberto Luaces <[hidden email]> wrote:

> Alberto Luaces <[hidden email]> writes:
>
>>> Sencondly, DBIx::Class only hits the database when it absolutely has
>>>to,
>>> e.g. to fetch restults.  This includes the initial connection.  However,
>>> you can force it to connect early by calling
>>>
>>>    $schema->storage->ensure_connected;
>>>
>>> Which will throw an exception if it fails to connect.
>>
>> Yes, it sounds sensible for an ORM to be lazy, but I did not imagine
>> that it could even involve connections.  Thanks a lot for the pointer.
>
> Oops, I spoke too soon.  ensure_connected is not throwing.  Here is the
> code for reference:
>
>  1 use lib './lib';
>  2 use DAAO;
>  3
>  4 my $r = DAAO->connect("dbi:SQLite:non-existent.db");
>  5 $r->storage->ensure_connected;
>  6
>  7 my $resultset = $r->resultset('Alumno')->search;
>  8
>  9 my $person = $resultset->next;       # Iterator
> 10
> 11 print $person->asistencia;
>
> It does not crash until line 9, when the iterator is used.
>
> --
> Alberto
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Will Crawford
On 15 October 2015 at 11:13, Will Crawford <[hidden email]> wrote:
[stupid top-post elided]

Sorry about that.

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Alberto Luaces
In reply to this post by Will Crawford
Will Crawford writes:

> This is because you are using sqlite. You could be intending to call
> ->deploy() rather than trying to select data. Sqlite will create the
> file for you (complete with typo) in this case. But as you haven't
> created any tables in the alternately-spelt database file, you'll get
> an exception.

Thanks, Will.

This makes complete sense, and furthermore, it answers another of my
beginner questions (how to deploy a schema over a new database).

Maybe the saner approach with sqlite could be to check first the
existence of the database file, before trying to connect to it.

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection errors

Alberto Luaces
Peter Rabbitson writes:

> On 10/15/2015 04:09 PM, Alberto Luaces wrote:
>>
>> Maybe the saner approach with sqlite could be to check first the
>> existence of the database file, before trying to connect to it.
>>
>
> You could do that yourself by changing the default open flags, as
> described in
> https://metacpan.org/pod/DBD::SQLite#Database-Name-Is-A-File-Name

[...]

Great, thank you for the pointer!

--
Alberto


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...