• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
new to CI, need advice on db connections

Hi all,
as said, I'm a CI newbie, but have some experience on php/other frameworks, and programming in general.
my test app runs on a typical lamp stack, atm.

In CI's config/database.php, I have set a dsn, and it works. I tried a simple model and I get my data through the view, nicely,
but it seems that (nearly?) every page refresh opens a new mysql process, and I would like it to reuse the same connecion instead.

I guess this needs some configuration, somewhere: what are the steps needed to make the connection survive longer, that I am missing?
What are the topics to look for in the docs, or elsewhere?

here's the current dsn

$db['default'] = array(
        'dsn'   => 'mysql',
        'hostname' => '192.168.x.y',
        'username' => 'username',
        'password' => 'password',
        'database' => 'dbname',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE


You can autoload database inside autoload.php in config folder. Then you can use $this->db without load database first.
Keep calm.

(03-29-2016, 07:57 AM)arma7x Wrote: You can autoload database inside autoload.php in config folder. Then you can use $this->db without load database first.

Thanks, there I added a line like
$autoload['libraries'] = array('database');

it doesn't change much, though:
my app is a really simple test, like

where <X> is the customer ID,

but changing the <X> parameter, makes the server open a new connection...

I'm sure it's my fault, but I would like to test if results are quicker keeping the same connection, as atm they're quite slow.


When you say "makes the server open a new connection" do you mean it doesn't close the old one and instead opens another and therefore your mysql server connection pool is maxed out? Since in PHP every page is a "new request" it starts from "nothing" and builds the page with every HTTP request. This "normally" closes everything down when complete. Of course there is always PHP Persistent Database Connections http://php.net/manual/en/features.persis...ctions.php which is a option of most PHP database drivers (specified by CodeIgniters pconnect database property) and that's another ball of wax.

As you can see there are many interpretation for "makes the server open a new connection" can you provide a little more clarity?


Sorry to answer late...
yes, you're right, I was misunderstanding php connection persistence Smile

I had (to some extent still have) performance problems connecting CI to a middleware through postgresql odbc, and trying to catch what was causing horribly slow queries, I noticed on the server that other simple query tools opened and reused a single connection while CI opened a new one at every page refresh... and I thought that it could be to something in the php/odbclinux/postgresql driver due to my relative inexperience with odbclinux, postgresql ad CI.

It turned out that CI was fine, and the problem was between linux odbc driver and the middleware, mainly.

Middleware is Jboss Teiid (http://teiid.jboss.org/), btw, and after several thread posts in their support forum, we spotted a coulple of problems, and they fixed the most troubling ones in a new beta version, which I'm testing. See https://developer.jboss.org/thread/268880.

While Teiid is much more performant through (eg) jdbc and odata, the same queries through its emulated odbc/postrgresql interface were much, much slower. Now is better, even if not yet perfect, and I'm going to learn more of CI.

Thank you, all, and feel free to suggest me any resource that could help in this respect,


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.