Perl Oracle Database Connections remain ESTABLISHED!!
vivek_an
created: 2006-01-02 09:58:06
Hi Folks,

We are developing a web based tool that needs access to the database for keeping track of users logging into our website. For this we used Perl independent DBI interface. We face an issue where even after calling $dbh->disconnect we still see connections to ORACLE from our Web Server remaining in ESTABLISHED state. Could you please give a clue on why this may happen? Please note that we are not useing APACHE::DBI persistent connection, since we donot need to do so at this time.

Thanks .. and Wish you a happy new year 2006.

Vivek AN

Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 11:06:56
Just hint:
Have you established connection with RaiseError=>1 or PrintError=>1 attribute?
Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 12:56:05
This node was taken out by the NodeReaper on 2006-01-02 13-37-48
Reason: [BrowserUk]: Personal attack

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 12:59:26
This node was taken out by the NodeReaper on 2006-01-02 13-35-23
Reason: [BrowserUk]: racism

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 13:44:57
This node was taken out by the NodeReaper on 2006-01-02 14-13-41
Reason: [Corion]: (reap) Dupe of Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!! (which is also an attack and got reaped as well)

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 13:47:02
This node was taken out by the NodeReaper on 2006-01-02 14-26-55
Reason: [jZed]: purposely offensive

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:39:23
This node was taken out by the NodeReaper on 2006-01-02 15-28-35
Reason: [jdporter]: inflammatory troll

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:58:00
This node was taken out by the NodeReaper on 2006-01-02 16-02-31
Reason: [tirwhan]: Reap, troll

You may view the original node and the consideration vote tally.

Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 11:50:25
It can take time for an Oracle connection to close. You alos may have multiple connections open. Try it from a simple command-line script and see how long it takes for the connection to close after you disconnect.
Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 12:57:44
This node was taken out by the NodeReaper on 2006-01-02 13-37-12
Reason: [BrowserUk]: Troll

You may view the original node and the consideration vote tally.

Reaped: Re: Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:47:42
This node was taken out by the NodeReaper on 2006-01-02 15-12-47
Reason: [jdporter]: inflammatory troll

You may view the original node and the consideration vote tally.

Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 13:48:43
This node was taken out by the NodeReaper on 2006-01-02 14-44-49
Reason: [jZed]: purposely offensive

You may view the original node and the consideration vote tally.

Reaped: Re: Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:48:57
This node was taken out by the NodeReaper on 2006-01-02 15-08-13
Reason: [jdporter]: inflammatory troll

You may view the original node and the consideration vote tally.

Reaped: Re: Reaped: Re: Reaped: Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 15:13:04
This node was taken out by the NodeReaper on 2006-01-02 15-56-40
Reason: [tirwhan]: Reap, same old troll

You may view the original node and the consideration vote tally.

Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:46:49

This is the correct answer.

Re^2: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 02:37:47
Thanks Perrin, for your valuable inputs.

I tried it from the command-line. The disconnect was
almost instantaneous. The TCP connection to the Oracle
server moved from 'ESTABLISHED' to 'TIME_WAIT' state as
soon as $dbh->disconnect was called.

So am facing this problem after mod_perl integration with
my snip below. .
I think mod_perl is not letting the database connection to
be torn down!! I am not using Apache DBI though, so
persistent connection concept can be ruled out.

Any more clues that I may want to take a look at?

Here's my Perl snip for reference:


use DBI;
my $dbh = 0;

sub connect_db {
   if (defined $dbh) {
        eval {
            $dbh->ping;
        };
        if (!$@) {
            #success return preserver database handle
            return $dbh;
        }
    }
      #ok, create a new database handle
        my $retry_count = 5;
        while ((!$dbh) && ($retry_count > 0)) {
                $dbh = DBI->connect('dbi:Oracle:linuxdb3','scott', 'tiger');
                $retry_count -= 1;
                sleep(1);
        }

        if (!$dbh) {
            # Database connection failed
            return 0;
        }
         # Database connection OK
        return $dbh;
}

sub disconnect_db {
    if (defined $dbh) {
        eval {
            $dbh->ping;
        };
        if ($@) {
            # Database handle invalid as ping failed
            return 0;
        }
    }
            # Disconnect Database connection
        $dbh->disconnect;
        return 1;
}

--
Thanks,

Vivek AN
Re^3: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 03:04:55

The eval check around $dbh->ping is pointless, DBD::Oracle does it's own eval internally. That subroutine seems weird anyhow, because you first check for definedness of $dbh, then try to call disconnect on it regardless of whether it was defined or not, then return 1 regardless of whether the disconnect was successful or not.

Not sure that any of this will help with your actual problem, but I thought I'd point it out.


A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Re^3: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 03:17:41

Your problem is that you've declared $dbh outside of your subroutine, but reference it inside your subroutine. You should pass it into the subroutine instead. Have a look at this mod_perl doco

(P.S. If you don't wnat to pass you database handle around, just connect and disconnect in the main trunk of your code)

Re^4: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 09:38:14
That would cause a scoping problem, but wouldn't prevent an actual disconnect() call from working.
Re^5: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 21:27:13
It's may not be just a scoping problem. Apache::Registry will cache the script inside a subroutine, similar to
#!/usr/local/bin/perl -w
use DBI;
use strict;
use diagnostics;

sub emulate_sub {
    my $dbh;
    
    sub connect {
        $dbh = DBI->connect('dbi:Oracle:rdb1', 'scott', 'tiger') ||
        die DBI::errstr;
    }
}
I suspect that there's there's a copy with a connection, but the diconnect is not available. UPDATE: I was updating the thread while perrin was replying. It probably won't affect what he says, as his answer still applies to the updated version
Re^6: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 22:36:34
Apache::Registry wraps it in a sub, but since $dbh is visible to both the connect and disconnect subs, it doesn't really matter. The $dbh variable will stay in scope, but the disconnect call will still work unless there is an actual bug in his code. The scoping seems to have been intentional.
Re^3: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-03 09:43:00
Have you tried use DBI_TRACE or putting in a warn statement to see if the disconnect() really gets called? Maybe the ping above it is failing and causing it to get skipped.

Incidentally, your connect_db() sub will always sleep for at least 1 second, even if it connects right away. That's probably not what you want.

Ultimately, I recommend you get rid of this code and use $dbh->connect_cached() instead. It is similar to Apache::DBI, but doesn't prevent disconnects.

Re^3: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-04 08:45:39
can you show a bit more code?
Reaped: Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:33:30
This node was taken out by the NodeReaper on 2006-01-02 15-08-15
Reason: [jdporter]: inflammatory troll

You may view the original node and the consideration vote tally.

Reaped: Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:41:46
This node was taken out by the NodeReaper on 2006-01-02 16-01-36
Reason: [graff]: reap - more useless trolling

You may view the original node and the consideration vote tally.

Reaped: Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 14:44:13
This node was taken out by the NodeReaper on 2006-01-02 16-04-29
Reason: [jdporter]: inflammatory troll

You may view the original node and the consideration vote tally.

Reaped: Re: Perl Oracle Database Connections remain ESTABLISHED!!
created: 2006-01-02 15:23:39
This node was taken out by the NodeReaper on 2006-01-02 16-04-02
Reason: [planetscape]: Reap, inflammatory, personal attack

You may view the original node and the consideration vote tally.

perlmonks.org content © perlmonks.org and Anonymous Monk, astroboy, NodeReaper, pajout, perrin, tirwhan, vivek_an

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03