PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] PHP Codeigniter using Multiple Databases - is to many connections

 March 03, 2022     codeigniter, mysql, php     No comments   

Issue

I'd like to change in my app in CodeIgniter 3 from one database to two database. I'm using Aurora AWS so I need split database to two instance one for read and second one for write.

enter image description here

I think I made all, but after split to two instance my DB connections increase very high in the same traffic. I think it is because in every models I loaded in construct second database ($this->db2 = $this->load->database('writer_instance', TRUE);).

enter image description here

Red area is when I split to two instances. Blue area is when I don't split to two instances.

What should I do so that the number of connections does not increase? Is any option to load writer instance once as reader instance?

Here is my code:

I add in database.php

$active_group = 'reader_instance';
$query_builder = TRUE;

$db['reader_instance'] = array(
    'dsn'   => '',
    'hostname' => '*',
    'username' => '*',
    'password' => '*',
    'database' => '*',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    '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,
    'port' => 3306
);

$db['writer_instance'] = array(
    'dsn'   => '',
    'hostname' => '*',
    'username' => '*',
    'password' => '*',
    'database' => '*',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    '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,
    'port' => 3306
);

Next in each models added construct when I load second database.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ActionModerator_model extends CI_Model {

    function __construct(){
        parent::__construct();
        // db - reader_instance
        $this->db2 = $this->load->database('writer_instance', TRUE);
    }

    public function add($object)
    {
        $this->db2->insert( 'parameter_action' , $object );
    }

    public function get($id_group)
    {
        $this->db->select('id, name');
        $this->db->where( 'id_group' , $id_group );
        $q = $this->db->get( 'user' );
        $q = $q->result();
        
        return $q;
    }
}

Solution

I made this myself. I create library:

<?php

class Databaseloader {

         public function __construct() {
            $this->load();
         }

         public function load() {
            $CI = &get_instance();

            $CI->db = $CI->load->database('reader_instance', TRUE);
            $CI->db2 = $CI->load->database('writer_instance', TRUE);
         }
}

Next in autoload.php replaced in variable $autoload['libraries'] database with databaseloader and remove load database in each models.

$autoload['libraries'] = array('databaseloader');


Answered By - Mat
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing