Welcome Guest, Not a member yet? Register   Sign In
Datebase close() function
#1

[eluser]haraldo[/eluser]
Hi there,

i'm currently opening a custom db connection within a hook. Recording a record and then closing the db. Something like below:

Code:
$this->_openDBConn();
// print_r( $this->oDB ) BEFORE
$this->recordVisit();
$this->_closeDBConn();

// print_r( $this->oDB ) AFTER
$this->recordVisit();

This works fine and the record is recorded in the db. No error etc.

However, i make another call to
Code:
$this->recordVisit();
immediatelty after i've closed the db connection. This record is also put in the db even though i've closed the connection correctly.

I know i'm closing it cause i can see i'm getting into the _close() function in the mysql driver. Also in the databse config i've set the following:

Code:
$db['default']['pconnect'] = FALSE;

When i print_r( $this->oDB ) before i get this (exert):

Code:
[pconnect] =>
[conn_id] => Resource id #58

after:

Code:
[pconnect] =>
[conn_id] =>

Why does the second record go in? I would expect to get a PHP error, no? Since the conn_id is empty.

Thanks for any advice.
#2

[eluser]Randy Casburn[/eluser]
[quote author="haraldo" date="1225579770"]Why does the second record go in? I would expect to get a PHP error, no? Since the conn_id is empty. Thanks for any advice.[/quote]

The blank "conn_id" clearly doesn't mean anything. So the cause & effect have not been connected correctly right? One of two things must be happening:

1) a connection (the real one being used perhaps) is still open regardless of what's in your hook code

or

2) a new connection is being created just prior to the second query being sent.

Can't think of anything else, so I would try to track one of those two possibilities down. Without seeing more of the code, we can't do that here.

Hope that helps,

Randy
#3

[eluser]haraldo[/eluser]
Thanks for the reply. Code is as below. This class is instantiated from the pre-controller hook.
Code:
protected $oDB;

    function __construct() {
        
        $this->_openDBConn();
        $this->recordVisit();
        $this->_closeDBConn();
        
        $this->recordVisit();
    }
    
    public function recordVisit() {

        $aData = array( 'remote_addr'  => $_SERVER['REMOTE_ADDR']
                      , 'http_referer' => isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'NOT SET');
    
        $this->oDB->insert( 'visitor', $aData );
    }


    protected function _openDBConn() {
        
        require_once( BASEPATH.'database/DB'.EXT );
        
        if( !isset( $this->oDB ) ) {
            
            $this->oDB = DB();
        }
    }
    
    protected function _closeDBConn() {

        $this->oDB->close();
    }

I changed to the snipet below and this stops the second insert however there is no error with the second insert. Not in my PHP log or via codeigniter. I thought there would be.

Code:
protected function _closeDBConn() {
        
        //$this->oDB->close();
        mysql_close( $this->oDB->conn_id );
    }

So essentially its working how i would imagine. Have to look into why
Code:
$this->oDB->close();

Is not working correctly. Its not good enough really the way it is so if anyone has any thoughts.
#4

[eluser]haraldo[/eluser]
Just to mention this class is actually instantiated from the pre-controller hook, its not actually the hook itself.
#5

[eluser]Randy Casburn[/eluser]
Now that makes perfectly good sense, and is directly in line with what I said above. Depending on what driver you are using, there may not be a close() method in the DB class. As an example, the mysql and mysqli drivers both use _close() as the method to close the connection. So you were never closing the connection.

So when you shifted to the native PHP function it works because you are actually closing the connection then.

That explains it all,

Randy
#6

[eluser]haraldo[/eluser]
Hi Randy,

There is a close function in the db_driver which calls the mysql_driver _close() function.

If there was no close function i'd get a php error originating from my _closeDBConn() function.

Also i can echo out in the mysql_driver class my conn_id. So its going all the way through fine. It seems to be the mysql_close() in mysql_driver.php isn't closing the mysql connection properly.

I have no idea why.
#7

[eluser]Randy Casburn[/eluser]
[quote author="haraldo" date="1225588193"]Hi Randy,

There is a close function in the db_driver which calls the mysql_driver _close() function.

[/quote]

Give me the line number please.
#8

[eluser]haraldo[/eluser]
line 1162

system/database/DB_driver.php

Code:
function close()
    {
        if (is_resource($this->conn_id) OR is_object($this->conn_id))
        {
            $this->_close($this->conn_id);
        }
        $this->conn_id = FALSE;
    }
#9

[eluser]Randy Casburn[/eluser]
OK...

A couple of follow on questions. We're getting close to a conclusion:

1) What driver is assigned in your database config file?

2) What does the following code imply to you? (from lines 121-133 of database/DB.php)

Code:
require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);

    // Instantiate the DB adapter
    $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    $DB =& new $driver($params);

Randy
#10

[eluser]haraldo[/eluser]
The driver reads:
Code:
$db['default']['dbdriver'] = "mysql";

The code will load the correct driver, which it does cause i can see that from my debugging.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB