so you can get the connection details from the session and connect to the database manually using those details and close it when done
Manually Connecting to a Database
$this->load->database();
The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file. Examples:
To choose a specific group from your config file you can do this:
$this->load->database('group_name');
Where group_name is the name of the connection group from your config file.
To connect manually to a desired database you can pass an array of values:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
For information on each of these values please see the configuration page.
Or you can submit your database values as a Data Source Name. DSNs must have this prototype:
$dsn = 'dbdriver://username:password@hostname/database';
$this->load->database($dsn);
To override default config values when connecting with a DSN string, add the config variables as a query string.
$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';
$this->load->database($dsn);
Manually closing the Connection
While CodeIgniter intelligently takes care of closing your database connections, you can explicitly close the connection.
$this->db->close();
for more about connecting to multiple databases read this https://ellislab.com/codeigniter/user-guide/database/connecting.html