Welcome Guest, Not a member yet? Register   Sign In
Can you close the default database groupd
#1

Hi everyone,

I have a question that is itching me to ask. I have search google and this forum but no answer. But none the less here goes:

Is it possible to close the default database server connection?

I'm asking this because I was developing a small application that would connect to one database server for the login credentials and then retrieve data from another database server.

In technical terms I connect to the database server which contains the user's credentials using the default database server configuration in the database.php configuration file:

Code:
$this->load->database();

then after validating the user's credentials I close the connection:

Code:
$this->db->close();


(However this does not seem to work as I will further explain in a minute.)

I then connect to a different database server using a different group name in my database.php configuration file:

Code:
$this->load->database('other_db_group');

However it seems that the original connection is still established as I used the the dbutil class to list the databases on the database server using the code:

Code:
$this->load->dbutil();

$dbs = $this->dbutil->list_databases();

foreach ($dbs as $db){
       echo $db;
}

An the list of databases shown was from the original database server I used to check the user's credentials. The error I was getting was that the SQL select that I was trying to execute was basically failing due to the table not being found. But I know it wasn't finding the table because it was not connected to the correct database server.

I read from the below stack overflow post that you have to set the pconnect variable to TRUE if you want codeigniter to close the default connection:

CodeIgniter database connections not being closed

However my pconnect is set to true. (So was the user who posted the question in the above link state as well)

I have no problems using multiple database connection in the form:

Code:
$DB1 = $this->load->database('db1',TRUE);

$DB2 = $this->load->database('db2',TRUE);

Matter of fact that is my current solution.

But for knowledge sake I want to know if you can indeed close the default connection and open another (using a different group) and used the default db object to get your data from the database?

Thanks in advance,

jerg
Reply
#2

You are indeed closing the connection, but if you try to use it again (which you do via dbutil) the database class will re-open it.

'pconnect' has no relation to this.
Reply
#3

(05-20-2016, 02:01 AM)Narf Wrote: You are indeed closing the connection, but if you try to use it again (which you do via dbutil) the database class will re-open it.

'pconnect' has no relation to this.

Thanks for the reply, so dbutil will open the default(active_group) connection even after I have executed:

Code:
$this->load->database('other_db_group');

Because I loaded and use the dbutil after executing the above line. From what I have read in the documentation I interpreted the above line to mean "If you specify the db_group as the first parameter it becomes the default connection hence you can use:

Code:
$this->load->dbutil();


 to refer to it instead of manually specifying:

Code:
$this->myutil = $this->load->dbutil($this->other_db_group,TRUE);

Likewise it leads me to believe that if perform the connect in this manner:

Code:
$this->load->database('other_db_group');


 you can also use, the default db object like this:

Code:
$this->db->query($sql);

and it will be querying the other_db_group database instead of the default(active_group).

I took a break from writing this post and modify the code to not call the dbutil at all so it has no chance of reconnecting to the old db_group. As you stated:

Quote:You are indeed closing the connection, but if you try to use it again (which you do via dbutil) the database class will re-open it.

'pconnect' has no relation to this.

so if I don't use the dbutil there should be no chance of it reconnecting.

However I still received the following error:
PHP Code:
A PHP Error was encountered

Severity
Warning

Message
pg_query(): Query failedERRORrelation "some_table" does not exist LINE 1: ...field1 as namefield2 as balance from some_table wher... ^

Filenamepostgre/postgre_driver.php

Line Number
242

Backtrace
:

File: /home/user/lappstack-5.6.20-1/apps/myapp/application/models/testlogin/Some_table_model.php
Line
31
Function: query

File
: /home/user/lappstack-5.6.20-1/apps/myapp/application/controllers/Some_table_controller.php
Line
64
Function: get_balance

File
: /home/user/lappstack-5.6.20-1/apps/myapp/application/controllers/Some_table_controller.php
Line
48
Function: get_balance

File
: /home/user/lappstack-5.6.20-1/apps/myapp/htdocs/index.php
Line
315
Function: require_once 

I have replace the file names, table name, field name and directory paths with random info as the info I have is fragile.

A co-worker of mind told me in the same stack overflow link I have in my original post someone stated its an error with the database persistent in mysql and told me it might be the same with mine. I believe I am spending too much time on this issue I already have a solution to use multiple db connection. I was just trying to see if you can use just one db connection (for possible resource management) but it doesn't seem to work. I will just use multiple database connection.

Thanks for the assistance none the less

jerg
Reply




Theme © iAndrew 2016 - Forum software by © MyBB