Skip to content

Commit

Permalink
Merge pull request #276 from ytetsuro/feat/db-connection-close
Browse files Browse the repository at this point in the history
fixed Too many connections error.
  • Loading branch information
kenjis committed Dec 16, 2018
2 parents e38a140 + ead6b48 commit 2bd32ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php
Expand Up @@ -85,6 +85,11 @@ public static function setUpBeforeClass()
chdir(FCPATH);
}

public static function tearDownAfterClass()
{
CIPHPUnitTestDbConnectionStore::destory();
}

/**
* Reset CodeIgniter instance and assign new CodeIgniter instance as $this->CI
*/
Expand Down
@@ -0,0 +1,27 @@
<?php

class CIPHPUnitTestDbConnectionStore
{
private static $connections = [];

public static function add(CI_DB $db)
{
self::$connections[] = $db;
}

public static function destory()
{
foreach (self::$connections as $db) {
self::closeConnection($db);
}

self::$connections = [];
}

private static function closeConnection(CI_DB $db)
{
if ($db->dsn !== 'sqlite::memory:' && $db->database !== ':memory:') {
$db->close();
}
}
}
4 changes: 3 additions & 1 deletion application/tests/_ci_phpunit_test/replacing/core/Loader.php
Expand Up @@ -400,7 +400,9 @@ public function database($params = '', $return = FALSE, $query_builder = NULL)

if ($return === TRUE)
{
return DB($params, $query_builder);
$result = DB($params, $query_builder);
CIPHPUnitTestDbConnectionStore::add($result);
return $result;
}

// Initialize the db variable. Needed to prevent
Expand Down

0 comments on commit 2bd32ec

Please sign in to comment.