Re: DBI and modules

From: Doug Silver (dsilver_at_urchin.com)
Date: 10/17/03

  • Next message: Thomas A. Lowery: "Re: DBI and modules"
    To: "Simon Taylor" <set@nortelnetworks.com>,        "'dbi-users@perl.org'" <dbi-users@perl.org>
    Date: Fri, 17 Oct 2003 10:38:29 -0700
    
    

    On Friday 17 October 2003 08:22 am, Simon Taylor wrote:
    > I am trying to create a module that sets ups environment variables from
    > another module, creates a database handle and passes it back to the calling
    > script.
    > I understand that an object exists as long as there is a reference to it
    > but cant quite see why this code generates the following errors when it
    > tries to destroy the dbh even though it is always an object in the hash?
    > No doubt I am confused about a fundamental here.
    >
    > Issuing rollback() for database handle being DESTROY'd without explicit
    > disconnect() at ./poe_ctrl.pl line 38.
    > $VAR1 = bless( {
    > '_dbh' => bless( {}, 'DBI::db' )
    > }, 'SDU::DB' );
    > Issuing rollback() for database handle being DESTROY'd without explicit
    > disconnect().
    >
    > ---------------------------------------------------------------------------
    >-
    > ---------------------------------------------------------------------------
    >- ------------------
    > Script to create the connection poe_ctrl.pl
    >
    > use warnings;
    > use strict;
    > use Data::Dumper;
    > use POE qw(Wheel::Run Filter::Reference);
    > use SDU::CommandIterator;
    > use SDU::DB;
    > use SDU::Config;
    > sub MAX_CONCURRENT_TASKS () { 5 }
    >
    >
    > # Create database connection
    > my $dbh = SDU::DB->new;
    > print Dumper($dbh);
    > exit;
    >
    > ---------------------------------------------------------------------------
    >-
    > ---------------------------------------------------------------------------
    >- --------------------
    > Package to create the connection
    >
    > package SDU::DB;
    > require 5.008 ;
    > use SDU::Config;
    > use strict;
    > use Carp;
    > use Carp qw(cluck);
    > use DBI;
    > use DBI qw(:sql_types);
    > use DBD::Oracle;
    > use DBD::Oracle qw(:ora_types);
    > use TNBException;
    >
    > sub new {
    > my $class = shift;
    > my $SC = SDU::Config->new;
    > my $DB = { _dbh => &ConnectToOracle($SC)
    > };
    > my $DC = &ConnectToOracle($SC);
    > my $self = bless $DB, $class;
    > return $self;
    > }
    >
    >
    > sub ConnectToOracle{
    > my ($SC) = @_;
    > $ENV{ORACLE_HOME}=$SC->{ORA_HOME};
    > $ENV{TWO_TASK}=$SC->{ORA_SID};
    > $ENV{ORACLE_SID}=$SC->{ORA_SID};
    > my $combined=$SC->{ORA_USER}."@".$SC->{ORA_SID};
    > my $trace_level=0;
    > my $dbh = undef;
    > $dbh = DBI->connect('dbi:Oracle:',$combined , $SC->{ORA_PASS}) or throw
    > TNBCriticalException($dbh->errstr);
    > $dbh->func( 1000000, 'dbms_output_enable' );
    > $dbh->{RaiseError} = 1;
    > $dbh->{PrintError} = 0;
    > $dbh->{AutoCommit} = 0;
    > $dbh->trace($trace_level);
    > return $dbh;
    > }
    >
    > 1;
    >
    > Simon Taylor
    > Managed Services Technology Consultant
    > Nortel Networks
    > p - 01279 404289 (ESN 742 4289)
    > m - 07740 533743 (ESN 748 3743)
    > e - set@nortelnetworks.com
    >
    > "I code therefore I am"

    Hi Simon -

    I am also in the process of writing something similar and came up with the
    following:

    #this will automatically handle all DBI calls that we make
    #using this module, e.g. disconnect, selectrow_array, do, etc
    sub AUTOLOAD {
       my $self = shift;
       my $name = our $AUTOLOAD;
       $name=~s/.*::DB::(.*)/$1/;
       #safely do a db disconnect if the script is not called
       #from the web server
       return $self->disconnect if $name=~/DESTROY$/ && ! defined
       $ENV{SERVER_SOFTWARE};
       return $self->{dbh}->$name(@_);
    }

    If you run some tests, particularly from command line, you'll see that when
    the script ends, it always calls the DESTROY method, and that's where the
    AUTOLOAD comes into play and it will safely disconnect from the database
    handle.

    Of course, YMMV!

    -- 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Doug Silver
    Network Manager
    Urchin Software Corp.	http://www.urchin.com
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • Next message: Thomas A. Lowery: "Re: DBI and modules"

    Relevant Pages

    • Re: why dataset in ado.net is disconnected
      ... need to be able to support thousands of concurrent users on a server ... application - all interacting with the database in a real-time mode. ... Either maintain an open connection all the time. ... possible and to encourage you to disconnect as soon as possible - for the ...
      (microsoft.public.dotnet.framework.adonet)
    • Re: To connect to a database.
      ... to connect and disconnect on every single database request, ... are loading a listbox manually from a database and on the same page, ... I would certainly close the connection after each query, but you could have a Populatemethod in your code which calls one stored procedure that returns multiple resultsets for all the information required for the page, close the connection, and then populate all the controls from the data. ...
      (microsoft.public.dotnet.framework.aspnet)
    • Re: Can we trigger event for abnormal loss of connection to oracle databse?
      ... I want a trigger to get fired when my application’s connection to the ... database gets disconnected abnormally. ... It records the error status at every disconnect. ...
      (comp.databases.oracle.server)
    • Re: Best Practices for implementing db conns from asp.net business ob
      ... > effective way to connect/disconnect from a database within a business ... > When should the MyClass class connect and disconnect from the database? ... Connect in the constructor, then disconnect in Dispose ... connection alive in code. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: ADO Connection Timeout
      ... so what happens when a connection failure forces one station to revert ... to a local database? ... Further, you *will* have contention issues, Jet does not support record ... to the central server, but you are willing to live with periods where it ...
      (microsoft.public.data.ado)