fhew has asked for the wisdom of the Perl Monks concerning the following question:

I need to get at the socket associated with a DBI connection to a mySQL databases so I can set some socket options on the connection, but all I have is the $dbh. Is there any way to get to the underlying stuff?

This is what I'd like to do:

my $dbh = DBI->connect('DBI:mysql:test:localhost', 'me', ''); setsockopt $dbh->{socket}, SOL_SOCKET, &SO_LINGER, pack("II", 1, 10);

Replies are listed 'Best First'.
Re: DBI & setsockopt
by derby (Abbot) on Nov 23, 2005 at 08:52 UTC

    Hmmm ... the Mysql module can do this:

    Database Handle

    As said above you get a database handle with the connect() method. The database handle knows about the socket, the host, and the database it is connected to.

    You get at the three values with the methods

    $scalar = $dbh->sock;
    $scalar = $dbh->host
    $scalar = $dbh->database;

    -derby
      I believe the sock in this context is actually the port number specified in the connect() call, and not the socket used to communicate. Even while trying to debug, I must be doing something stupid. Here's what I'm doing:
      $dbh = &db_connect; print "hi there 3\n"; #my $p = $dbh->sock; #my $h = $dbh->host; #my $d = $dbh->database; #print "port: p\nhost: $h\ndb : $d\n"; print "dbh: $dbh\n"; use Data::Dumper; Data::Dumper->Dump([$dbh]); foreach ( keys %$dbh) { print "$_=$dbh{$_}\n"; } print "got here\n";
      And here's what I get:
      hi there 3 dbh: DBI::db=HASH(0x3de72b0) got here
      a) Its telling me that $dbh is a hash, but why can't I print out its contents?

      b)The 4 lines are commented out, because if I leave them in, the application dies silently.

        Sorry for the confusion, I was talking about the Mysql module not the DBD::Mysql module (they're slightly different).

        -derby
Re: DBI & setsockopt
by steveAZ98 (Monk) on Nov 23, 2005 at 04:41 UTC
    Looks to me like you'll need to take a look at the DBD::mysql module. Looks like mysql.xs calls mysql_dr_connect in dbdimp.c. You may be able to set the options in one of these, but I'm not sure how you would correctly go about doing it.

    Once you figure it out I'd be interested in hearing what you ended up doing.

    Steve