只是一种思路,解决出现:ora-02396的问题
出现问题的场景,从数据库查询出一万条记录,循环处理这一万条记录,处理完毕后,再继续查询,使用同一个连接或者连接池,这时候会提示连接超出最大空闲时间。
public static function getInstance($host=0){
$databaseConfig=self::$databaseConfig;
//设置超时时间
$cache=new Cache(["host" => 'localhost',"port" => 6379,"password"=>'']);
if(isset($databaseConfig[$host]) && is_array($databaseConfig[$host]) && !empty($databaseConfig))
{
if($cache->isExpire('db-connect-expire-time') || !isset(self::$instance[$host]))
{
if($databaseConfig[$host]['engine']=='oracle'){
try
{
self::$instance[$host] = new PDO('oci:dbname=//'.$databaseConfig[$host]['host'].';charset='.$databaseConfig[$host]['charset'],$databaseConfig[$host]['user'],$databaseConfig[$host]['password']);
$cache->set('db-connect-expire-time',180,180);
}
catch (PDOException $e)
{
throw new Exception($e,'500');
}
}
if($databaseConfig[$host]['engine']=='mysql'){
try
{
self::$instance[$host] = new PDO('mysql:dbname='.$databaseConfig[$host]['database'].';host='.$databaseConfig[$host]['host'].';charset='.$databaseConfig[$host]['charset'],$databaseConfig[$host]['user'],$databaseConfig[$host]['password']);
$cache->set('db-connect-expire-time',180,180);
}
catch (PDOException $e)
{
throw new Exception($e,'500');
}
}
}
}
else
{
throw new Exception('还未配置数据库服务器【'.$host.'】的信息,请先配置信息后重试!','500');
}
return self::$instance[$host];
}