CoreDBConnection.pm 29.1 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
4
Copyright [2016-2021] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut


=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <dev@ensembl.org>.

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut

=head1 NAME

Bio::EnsEMBL::Hive::DBSQL::CoreDBConnection

=head1 SYNOPSIS

  $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new(
    -user   => 'anonymous',
    -dbname => 'homo_sapiens_core_20_34c',
    -host   => 'ensembldb.ensembl.org',
    -driver => 'mysql',
  );

  # SQL statements should be created/executed through this modules
  # prepare() and do() methods.

  $sth = $dbc->prepare("SELECT something FROM yourtable");

  $sth->execute();

  # do something with rows returned ...

  $sth->finish();

=head1 DESCRIPTION

This class is a wrapper around DBIs datbase handle.  It provides some
additional functionality such as the ability to automatically disconnect
when inactive and reconnect when needed.

Generally this class will be used through one of the object adaptors or
the Bio::EnsEMBL::Registry and will not be instantiated directly.

=head1 METHODS

=cut


package Bio::EnsEMBL::Hive::DBSQL::CoreDBConnection;

use strict;
72
no strict 'refs';
73
use warnings;
74 75 76 77 78 79 80

use DBI;
use Bio::EnsEMBL::Hive::DBSQL::StatementHandle;

use Bio::EnsEMBL::Hive::Utils ('throw');


81 82 83 84 85 86 87 88 89 90
use vars qw(@ISA);      # If Ensembl Core code is available, inherit from its' DBConnection for compatibility.
BEGIN {
    if (eval { require Bio::EnsEMBL::DBSQL::DBConnection; 1 }) {
        @ISA = ('Bio::EnsEMBL::DBSQL::DBConnection');
    } else {
        @ISA = ();
    }
}


91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
=head2 new

  Arg [DBNAME] : (optional) string
                 The name of the database to connect to.
  Arg [HOST] : (optional) string
               The domain name of the database host to connect to.  
               'localhost' by default. 
  Arg [USER] : string
               The name of the database user to connect with 
  Arg [PASS] : (optional) string
               The password to be used to connect to the database
  Arg [PORT] : (optional) int
               The port to use when connecting to the database
               3306 by default if the driver is mysql.
  Arg [DRIVER] : (optional) string
                 The type of database driver to use to connect to the DB
                 mysql by default.
  Arg [DBCONN] : (optional)
                 Open another handle to the same database as another connection
                 If this argument is specified, no other arguments should be
                 specified.
  Arg [DISCONNECT_WHEN_INACTIVE]: (optional) boolean
                 If set to true, the database connection will be disconnected
                 everytime there are no active statement handles. This is
                 useful when running a lot of jobs on a compute farm
                 which would otherwise keep open a lot of connections to the
                 database.  Database connections are automatically reopened
118
                 when required.Do not use this option together with RECONNECT_WHEN_LOST.
119
  Arg [WAIT_TIMEOUT]: (optional) integer
120
                 Time in seconds for the wait_timeout to happen. Time after which
121 122 123 124
                 the connection is deleted if not used. By default this is 28800 (8 hours)
                 on most systems. 
                 So set this to greater than this if your connection are getting deleted.
                 Only set this if you are having problems and know what you are doing.
125
  Arg [RECONNECT_WHEN_LOST]: (optional) boolean
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                 In case you're reusing the same database connection, i.e. DISCONNECT_WHEN_INACTIVE is 
                 set to false and running a job which takes a long time to process (over 8hrs), 
                 which means that the db connection may be lost, set this option to true. 
                 On each prepare or do statement the db handle will be pinged and the database 
                 connection will be reconnected if it's lost.
                
  Example    : $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new
                  (-user   => 'anonymous',
                   -dbname => 'homo_sapiens_core_20_34c',
                   -host   => 'ensembldb.ensembl.org',
                   -driver => 'mysql');

  Description: Constructor for a Database Connection. Any adaptors that require
               database connectivity should inherit from this class.
  Returntype : Bio::EnsEMBL::DBSQL::DBConnection
  Exceptions : thrown if USER or DBNAME are not specified, or if the database
               cannot be connected to.
  Caller     : Bio::EnsEMBL::Utils::ConfigRegistry ( for newer code using the registry)
               Bio::EnsEMBL::DBSQL::DBAdaptor        ( for old style code)
  Status     : Stable

=cut

sub new {
    my $class = shift @_;
    my %flags = @_;

    my ($driver, $user, $password, $host, $port, $dbname,
154
        $dbconn, $disconnect_when_inactive, $wait_timeout, $reconnect_when_lost)
155
     = @flags{qw(-driver -user -pass -host -port -dbname -dbconn
156
                 -disconnect_when_inactive -wait_timeout -reconnect_when_lost)};
157 158 159 160 161

    my $self = {};
    bless $self, $class;

  if($dbconn) {
162
    if($dbname || $host || $driver || $password || $port || $disconnect_when_inactive || $reconnect_when_lost) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
      throw("Cannot specify other arguments when -DBCONN argument used.");
    }

    $self->driver($dbconn->driver());
    $self->host($dbconn->host());
    $self->port($dbconn->port());
    $self->username($dbconn->username());
    $self->password($dbconn->password());
    $self->dbname($dbconn->dbname());

    if($dbconn->disconnect_when_inactive()) {
      $self->disconnect_when_inactive(1);
    }
  } else {
    $driver ||= 'mysql';
    
    if($driver eq 'mysql') {
        $user || throw("-USER argument is required.");
        $host ||= 'mysql';
        if(!defined($port)){
            $port   = 3306;
            if($host eq "ensembldb.ensembl.org"){
                if( $dbname =~ /\w+_\w+_\w+_(\d+)/){
                    if($1 >= 48){
                        $port = 5306;
                    }
                }
            }
        }
    } elsif($driver eq 'pgsql') {
        if(!defined($port)){
            $port   = 5432;
        }
    }

    $self->driver($driver);
    $self->host( $host );
    $self->port($port);
    $self->username( $user );
    $self->password( $password );
    $self->dbname( $dbname );
204
    $self->wait_timeout($wait_timeout);
205 206 207 208

    if($disconnect_when_inactive) {
      $self->disconnect_when_inactive($disconnect_when_inactive);
    }
209 210
    if($reconnect_when_lost) {
      $self->reconnect_when_lost($reconnect_when_lost);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    }
  }

#  if(defined $dnadb) {
#    $self->dnadb($dnadb);
#  }
  return $self;
}


=head2 connect

  Example    : $dbcon->connect()
  Description: Connects to the database using the connection attribute 
               information.
  Returntype : none
  Exceptions : none
  Caller     : new, db_handle
  Status     : Stable

=cut

sub connect {
  my ($self) = @_;

  if ( $self->connected() ) { return }

  $self->connected(1);

  if ( defined( $self->db_handle() ) and $self->db_handle()->ping() ) {
    warn(   "unconnected db_handle is still pingable, "
             . "reseting connected boolean\n" );
  }

  my ( $dsn, $dbh );
  my $dbname = $self->dbname();

  if ( $self->driver() eq "Oracle" ) {

    $dsn = "DBI:Oracle:";

    eval {
      $dbh = DBI->connect( $dsn,
                           sprintf( "%s@%s",
                                    $self->username(), $dbname ),
                           $self->password(),
                           { 'RaiseError' => 1, 'PrintError' => 0 } );
    };

  } elsif ( $self->driver() eq "ODBC" ) {

    $dsn = sprintf( "DBI:ODBC:%s", $self->dbname() );

    eval {
      $dbh = DBI->connect( $dsn,
                           $self->username(),
                           $self->password(), {
                             'LongTruncOk'     => 1,
                             'LongReadLen'     => 2**16 - 8,
                             'RaiseError'      => 1,
                             'PrintError'      => 0,
                             'odbc_cursortype' => 2 } );
    };

  } elsif ( $self->driver() eq "Sybase" ) {
    my $dbparam = ($dbname) ? ";database=${dbname}" : q{};

    $dsn = sprintf( "DBI:Sybase:server=%s%s;tdsLevel=CS_TDS_495",
                    $self->host(), $dbparam );

    eval {
      $dbh = DBI->connect( $dsn,
                           $self->username(),
                           $self->password(), {
                             'LongTruncOk' => 1,
                             'RaiseError'  => 1,
                             'PrintError'  => 0 } );
    };

  } elsif ( lc( $self->driver() ) eq 'sqlite' ) {

    throw "We require a dbname to connect to a SQLite database"
      if !$dbname;

    $dsn = sprintf( "DBI:SQLite:%s", $dbname );

    eval {
      $dbh = DBI->connect( $dsn, '', '', { 'RaiseError' => 1, } );
    };

  } else {

    my $dbparam = ($dbname) ? "database=${dbname};" : q{};

    my $driver = $self->driver();
    $driver = 'Pg' if($driver eq 'pgsql');

    $dsn = sprintf( "DBI:%s:%shost=%s;port=%s",
                    $driver, $dbparam,
                    $self->host(),   $self->port() );

312 313 314
    my $parameters = { 'RaiseError' => 1 };
    $parameters->{'mysql_local_infile'} = 1 if lc($self->driver()) eq 'mysql';

315 316
    eval {
      $dbh = DBI->connect( $dsn, $self->username(), $self->password(),
317
                           $parameters );
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    };
  }
  my $error = $@;

  if ( !$dbh || $error || !$dbh->ping() ) {
    warn(   "Could not connect to database "
          . $self->dbname()
          . " as user "
          . $self->username()
          . " using [$dsn] as a locator:\n"
          . $error );

    $self->connected(0);

    throw(   "Could not connect to database "
           . $self->dbname()
           . " as user "
           . $self->username()
           . " using [$dsn] as a locator:\n"
           . $error );
  }

  $self->db_handle($dbh);

342
  if ( $self->wait_timeout() ) {
343 344 345
    my $driver = $self->driver();

    if( $driver eq 'mysql' ) {
346
        $dbh->do( "SET SESSION wait_timeout=" . $self->wait_timeout() );
347
    } else {
348
        warn "Don't know how to set the wait_timeout for '$driver' driver, skipping.\n";
349
    }
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  }

  #print("CONNECT\n");
} ## end sub connect


=head2 connected

  Example    : $dbcon->connected()
  Description: Boolean which tells if DBConnection is connected or not.
               State is set internally, and external processes should not alter state.
  Returntype : undef or 1
  Exceptions : none
  Caller     : db_handle, connect, disconnect_if_idle, user processes
  Status     : Stable

=cut

sub connected {
  my $self = shift;

  # Use the process id ($$) as part of the key for the connected flag.
  # This forces the opening of another connection in a forked subprocess.
  $self->{'connected'.$$} = shift if(@_);
  return $self->{'connected'.$$};
}

sub disconnect_count {
  my $self = shift;
  return $self->{'disconnect_count'} = shift if(@_);
  $self->{'disconnect_count'}=0 unless(defined($self->{'disconnect_count'}));
  return $self->{'disconnect_count'};
}

384
sub wait_timeout{
385 386 387
  my($self, $arg ) = @_;

  (defined $arg) &&
388
    ($self->{_wait_timeout} = $arg );
389

390
  return $self->{_wait_timeout};
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

}

sub query_count {
  my $self = shift;
  return $self->{'_query_count'} = shift if(@_);
  $self->{'_query_count'}=0 unless(defined($self->{'_query_count'}));
  return $self->{'_query_count'};
}

=head2 equals

  Example    : warn 'Same!' if($dbc->equals($other_dbc));
  Description: Equality checker for DBConnection objects
  Returntype : boolean
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

  
sub equals {
  my ( $self, $dbc ) = @_;
  return 0 if ! defined $dbc;
  my $return = 0;
  my $undef_str = q{!-undef-!};
  my $undef_num = -1;

  $return = 1 if  ( 
    (($self->host() || $undef_str)      eq ($dbc->host() || $undef_str)) &&
    (($self->dbname() || $undef_str)    eq ($dbc->dbname() || $undef_str)) &&
    (($self->port() || $undef_num)      == ($dbc->port() || $undef_num)) &&
    (($self->username() || $undef_str)  eq ($dbc->username() || $undef_str)) &&
    ($self->driver() eq $dbc->driver())
  );
  
  return $return;
}

=head2 driver

  Arg [1]    : (optional) string $arg
               the name of the driver to use to connect to the database
  Example    : $driver = $db_connection->driver()
  Description: Getter / Setter for the driver this connection uses.
               Right now there is no point to setting this value after a
               connection has already been established in the constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub driver {
  my($self, $arg ) = @_;

  (defined $arg) &&
    ($self->{_driver} = $arg );
  return $self->{_driver};
}


=head2 port

  Arg [1]    : (optional) int $arg
               the TCP or UDP port to use to connect to the database
  Example    : $port = $db_connection->port();
  Description: Getter / Setter for the port this connection uses to communicate
               to the database daemon.  There currently is no point in 
               setting this value after the connection has already been 
               established by the constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub port {
  my ( $self, $value ) = @_;

  if ( defined($value) ) {
    $self->{'_port'} = $value;
  }

  return $self->{'_port'};
}


=head2 dbname

  Arg [1]    : (optional) string $arg
               The new value of the database name used by this connection. 
  Example    : $dbname = $db_connection->dbname()
  Description: Getter/Setter for the name of the database used by this 
               connection.  There is currently no point in setting this value
               after the connection has already been established by the 
               constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub dbname {
  my ($self, $arg ) = @_;
  ( defined $arg ) &&
    ( $self->{_dbname} = $arg );
  $self->{_dbname};
}


=head2 username

  Arg [1]    : (optional) string $arg
               The new value of the username used by this connection. 
  Example    : $username = $db_connection->username()
  Description: Getter/Setter for the username used by this 
               connection.  There is currently no point in setting this value
               after the connection has already been established by the 
               constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub username {
  my ($self, $arg ) = @_;
  ( defined $arg ) &&
    ( $self->{_username} = $arg );
  $self->{_username};
}

=head2 user

  Arg [1]    : (optional) string $arg
               The new value of the username used by this connection. 
  Example    : $user = $db_connection->user()
  Description: Convenience alias for the username method
  Returntype : String

=cut

sub user {
  my ($self, $arg) = @_;
  return $self->username($arg);
}


=head2 host

  Arg [1]    : (optional) string $arg
               The new value of the host used by this connection. 
  Example    : $host = $db_connection->host()
  Description: Getter/Setter for the domain name of the database host use by 
               this connection.  There is currently no point in setting 
               this value after the connection has already been established 
               by the constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub host {
  my ($self, $arg ) = @_;
  ( defined $arg ) &&
    ( $self->{_host} = $arg );
  $self->{_host};
}

=head2 hostname

  Arg [1]    : (optional) string $arg
               The new value of the host used by this connection. 
  Example    : $hostname = $db_connection->hostname()
  Description: Convenience alias for the host method
  Returntype : String

=cut

sub hostname {
  my ($self, $arg) = @_;
  return $self->host($arg);
}


=head2 password

  Arg [1]    : (optional) string $arg
               The new value of the password used by this connection.
  Example    : $host = $db_connection->password()
  Description: Getter/Setter for the password of to use for this
               connection.  There is currently no point in setting
               this value after the connection has already been
               established by the constructor.
  Returntype : string
  Exceptions : none
  Caller     : new
  Status     : Stable

=cut

sub password {
  my ( $self, $arg ) = @_;

  if ( defined($arg) ) {
    # Use an anonymous subroutine that will return the password when
    # invoked.  This will prevent the password from being accidentally
    # displayed when using e.g. Data::Dumper on a structure containing
    # one of these objects.

    $self->{_password} = sub { $arg };
  }

  return ( ref( $self->{_password} ) && &{ $self->{_password} } ) || '';
}

=head2 pass

  Arg [1]    : (optional) string $arg
               The new value of the password used by this connection. 
  Example    : $pass = $db_connection->pass()
  Description: Convenience alias for the password method
  Returntype : String

=cut

sub pass {
  my ($self, $arg) = @_;
  return $self->password($arg);
}

=head2 disconnect_when_inactive

  Arg [1]    : (optional) boolean $newval
  Example    : $dbc->disconnect_when_inactive(1);
  Description: Getter/Setter for the disconnect_when_inactive flag.  If set
               to true this DBConnection will continually disconnect itself
               when there are no active statement handles and reconnect as
               necessary.  Useful for farm environments when there can be
               many (often inactive) open connections to a database at once.
  Returntype : boolean
  Exceptions : none
  Caller     : Pipeline
  Status     : Stable

=cut

sub disconnect_when_inactive {
  my ( $self, $value ) = @_;

  if ( defined($value) ) {
    $self->{'disconnect_when_inactive'} = $value;
    if ($value) {
      $self->disconnect_if_idle();
    }
  }

  return $self->{'disconnect_when_inactive'};
}


=head2 reconnect_when_lost

  Arg [1]    : (optional) boolean $newval
  Example    : $dbc->reconnect_when_lost(1);
  Description: Getter/Setter for the reconnect_when_lost flag.  If set
               to true the db handle will be pinged on each prepare or do statement 
               and the connection will be reestablished in case it's lost.
               Useful for long running jobs (over 8hrs), which means that the db 
               connection may be lost.
  Returntype : boolean
  Exceptions : none
  Caller     : Pipeline
  Status     : Stable

=cut

sub reconnect_when_lost {
  my ( $self, $value ) = @_;

  if ( defined($value) ) {
    $self->{'reconnect_when_lost'} = $value;
  }

  return $self->{'reconnect_when_lost'};
}



=head2 locator

  Arg [1]    : none
  Example    : $locator = $dbc->locator;
  Description: Constructs a locator string for this database connection
               that can, for example, be used by the DBLoader module
  Returntype : string
  Exceptions : none
  Caller     : general
  Status     : Stable

=cut


sub locator {
  my ($self) = @_;

  return sprintf(
    "%s/host=%s;port=%s;dbname=%s;user=%s;pass=%s",
    ref($self),      $self->host(),     $self->port(),
    $self->dbname(), $self->username(), $self->password() );
}


=head2 db_handle

  Arg [1]    : DBI Database Handle $value
  Example    : $dbh = $db_connection->db_handle() 
  Description: Getter / Setter for the Database handle used by this
               database connection.
  Returntype : DBI Database Handle
  Exceptions : none
  Caller     : new, DESTROY
  Status     : Stable

=cut

sub db_handle {
   my $self = shift;

   # Use the process id ($$) as part of the key for the database handle
   # this makes this object fork safe.  fork() does not makes copies
   # of the open socket which creates problems when one of the forked
   # processes disconnects,
   return $self->{'db_handle'.$$} = shift if(@_);
   return $self->{'db_handle'.$$} if($self->connected);

   $self->connect();
   return $self->{'db_handle'.$$};
}


=head2 prepare

  Arg [1]    : string $string
               the SQL statement to prepare
  Example    : $sth = $db_connection->prepare("SELECT column FROM table");
  Description: Prepares a SQL statement using the internal DBI database handle
               and returns the DBI statement handle.
  Returntype : DBI statement handle
  Exceptions : thrown if the SQL statement is empty, or if the internal
               database handle is not present
  Caller     : Adaptor modules
  Status     : Stable

=cut

sub prepare {
756 757
   my $self = shift @_;
   my $sql  = shift @_;
758

759
   if( ! $sql ) {
760 761 762
     throw("Attempting to prepare an empty SQL query.");
   }

763
   #warn "SQL(".$self->dbname."): " . join(' ', $sql, @_) . "\n";
764 765 766
   if ( ($self->reconnect_when_lost()) and (!$self->db_handle()->ping()) ) { 
       $self->reconnect();
   }
767 768

   my $sth = Bio::EnsEMBL::Hive::DBSQL::StatementHandle->new( $self, $sql, @_ );
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

   $self->query_count($self->query_count()+1);
   return $sth;
}

=head2 reconnect

  Example    : $dbcon->reconnect()
  Description: Reconnects to the database using the connection attribute 
               information if db_handle no longer pingable.
  Returntype : none
  Exceptions : none
  Caller     : new, db_handle
  Status     : Stable

=cut

sub reconnect {
  my ($self) = @_;
  $self->connected(undef);
  $self->db_handle(undef);
  $self->connect();
  return;
}


=head2 work_with_db_handle

  Arg [1]    : CodeRef $callback
  Example    : my $q_t = $dbc->work_with_db_handle(sub { my ($dbh) = @_; return $dbh->quote_identifier('table'); });
  Description: Gives access to the DBI handle to execute methods not normally
               provided by the DBConnection interface
  Returntype : Any from callback
  Exceptions : If the callback paramater is not a CodeRef; all other 
               errors are re-thrown after cleanup.
  Caller     : Adaptor modules
  Status     : Stable

=cut

sub work_with_db_handle {
  my ($self, $callback) = @_;
  my $wantarray = wantarray;
  if( $self->reconnect_when_lost() && !$self->db_handle()->ping()) { 
    $self->reconnect();
  }
  my @results;
  eval {
    if($wantarray) { 
      @results = $callback->($self->db_handle())
    }
    elsif(defined $wantarray) {
      $results[0] = $callback->($self->db_handle());
    }
    else {
      $callback->($self->db_handle());
    }
  };
  my $original_error = $@;
  
  $self->query_count($self->query_count()+1);
  eval {
    if($self->disconnect_when_inactive()) {
      $self->disconnect_if_idle();
    }
  };
  if($@) {
    warn "Detected an error whilst attempting to disconnect the DBI handle: $@";
  }
  if($original_error) {
    throw "Detected an error when running DBI wrapper callback:\n$original_error";
  }
  
  if(defined $wantarray) {
    return ($wantarray) ? @results : $results[0];
  }
  return;
}

=head2 prevent_disconnect

  Arg[1]      : CodeRef $callback
  Example     : $dbc->prevent_disconnect(sub { $dbc->do('do something'); $dbc->do('something else')});
  Description : A wrapper method which prevents database disconnection for the
                duration of the callback. This is very useful if you need
                to make multiple database calls avoiding excessive database
                connection creation/destruction but still want the API
                to disconnect after the body of work. 
                
                The value of C<disconnect_when_inactive()> is set to 0 no
                matter what the original value was & after $callback has
                been executed. If C<disconnect_when_inactive()> was 
                already set to 0 then this method will be an effective no-op.
  Returntype  : None
  Exceptions  : Raised if there are issues with reverting the connection to its
                default state.
  Caller      : DBConnection methods
  Status      : Beta

=cut

sub prevent_disconnect {
  my ($self, $callback) = @_;
  my $original_dwi = $self->disconnect_when_inactive();
  $self->disconnect_when_inactive(0);
  eval { $callback->(); };
  my $original_error = $@;
  eval {
    $self->disconnect_when_inactive($original_dwi);    
  };
  if($@) {
    warn "Detected an error whilst attempting to reset disconnect_when_idle: $@";
  }
  if($original_error) {
    throw "Detected an error when running DBI wrapper callback:\n$original_error";
  }
  return;
}


=head2 disconnect_if_idle

  Arg [1]    : none
  Example    : $dbc->disconnect_if_idle();
  Description: Disconnects from the database if there are no currently active
               statement handles. 
               It is called automatically by the DESTROY method of the
               Bio::EnsEMBL::Hive::DBSQL::StatementHandle if the
               disconect_when_inactive flag is set.
               Users may call it whenever they want to disconnect. Connection will
               reestablish on next access to db_handle()
  Returntype : 1 or 0
               1=problem trying to disconnect while a statement handle was still active
  Exceptions : none
  Caller     : Bio::EnsEMBL::Hive::DBSQL::StatementHandle::DESTROY
               Bio::EnsEMBL::Hive::DBSQL::CoreDBConnection::do
  Status     : Stable

=cut

sub disconnect_if_idle {
  my $self = shift;

  return 0 if(!$self->connected());
  my $db_handle = $self->db_handle();
  return 0 unless(defined($db_handle));

  #printf("disconnect_if_idle : kids=%d activekids=%d\n",
  #       $db_handle->{Kids}, $db_handle->{ActiveKids});

  #If InactiveDestroy is set, don't disconnect.
  #To comply with DBI specification
  return 0 if($db_handle->{InactiveDestroy});

  #If any statement handles are still active, don't allow disconnection
  #In this case it is being called before a query has been fully processed
  #either by not reading all rows of data returned, or not calling ->finish
  #on the statement handle.  Don't disconnect, send warning
  if($db_handle->{ActiveKids} != 0) {
     warn("Problem disconnect : kids=",$db_handle->{Kids},
            " activekids=",$db_handle->{ActiveKids},"\n");
     return 1;
  }
  
  $db_handle->disconnect();
  $self->connected(undef);
  $self->disconnect_count($self->disconnect_count()+1);
  #print("DISCONNECT\n");
  $self->db_handle(undef);
  return 0;
}


942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962

#
# We have to redefine do() to avoid inheriting Core's do().
# However our do() is no different from any other DBI-enhanced methods in that they are all AUTOLOADed.
# So we switch off warnings and enforce AUTOLOADing.
#

no warnings 'redefine';
sub do {
    my $autoload    = __PACKAGE__.'::AUTOLOAD';
    $$autoload      = __PACKAGE__.'::do';
    goto &AUTOLOAD;
}


sub AUTOLOAD {
    our $AUTOLOAD;

    $AUTOLOAD=~/^.+::(\w+)$/;
    my $method_name = $1;

963 964 965 966 967 968
    # Mechanism to call on the dbc a db_handle method
    # Used for "prepare" because the latter also exists on dbc
    if ($method_name =~ /^protected_(.*)/) {
        $method_name = $1;
    }

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
#    warn "[AUTOLOAD instantiating '$method_name'] ($AUTOLOAD)\n";

    *$AUTOLOAD = sub {
#        warn "[AUTOLOADed method '$method_name' running] ($AUTOLOAD)\n";

        my $self = shift @_;
        my $db_handle = $self->db_handle() or throw( "db_handle returns false" );
        my $wantarray = wantarray;

        my @retval;
        eval {
            if( $wantarray ) {
                @retval = $db_handle->$method_name( @_ );
            } else {
                $retval[0] = $db_handle->$method_name( @_ );
            }
            1;
        } or do {
            my $error = $@;
            if( $error =~ /MySQL server has gone away/                      # mysql version  ( test by setting "SET SESSION wait_timeout=5;" and waiting for 10sec)
989
             or $error =~ /Lost connection to MySQL server during query/    # mysql version  ( test by setting "SET SESSION wait_timeout=5;" and waiting for 10sec)
990 991 992
             or $error =~ /server closed the connection unexpectedly/ ) {   # pgsql version

                warn "trying to reconnect...";
993
                # NOTE: parameters set via the hash interface of $dbh will be lost
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
                $self->reconnect();
                my $db_handle = $self->db_handle() or throw( "db_handle returns false" );

                warn "trying to re-$method_name...";
                if( $wantarray ) {
                    @retval = $db_handle->$method_name( @_ );
                } else {
                    $retval[0] = $db_handle->$method_name( @_ );
                }
            } else {
                throw( $error );
            }
        };

1008
        if($self->disconnect_when_inactive() && ($method_name !~ /^prepare/)) { # we shouldn't disconnect right after prepare() otherwise the statement handle would be linked to a closed connection
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
            $self->disconnect_if_idle();
        }

        return $wantarray ? @retval : $retval[0];
    };
    goto &$AUTOLOAD;
}


sub DESTROY { } # needed because of AUTOLOAD

1020 1021
1;