我一直试图在Symfony 2.3中为不同的MSSQL Server创建一个循环,但如果某些服务器关闭或MSSQL服务关闭,应用程序不会例外,它会中断功能。
这是主控制器:
function pruebaconAction()
{
$enlistaServers = new array (
10.10.10.1,
10.10.10.2,
10.10.10.3,
...
10.10.10.19,
10.10.10.20
);
foreach($enlistaServers as $datosServer)
{
$direccionIPParaConexion = $datosServer->getDireccionIp ();
$nombreLocalidad = $datosServer->getNombre();
register_shutdown_function(array($this, 'conectaSrv'), $direccionIPParaConexion, $nombreLocalidad);
}
return $this->render("GastoEnlaceBundle:Default:pruebacon.html.twig");
}
此函数创建连接:
function conectaSrv($direccionIpConexion, $nombreLocalidad)
{
$pruebaDBAL = new 'Doctrine'DBAL'Configuration();
$parametrosConexion = array(
'dbname' => 'MyDB',
'user' => 'user',
'password' => 'password',
'host' => $direccionIpConexion,
'driver' => 'pdo_sqlsrv'
);
$conexionDBAL = DriverManager::getConnection($parametrosConexion, $pruebaDBAL);
if($conexionDBAL->connect())
echo "Success<br />";
else
throw new Exception("Something is wrong :(<br />");
echo "=======================================================================<br />";
}
我总是得到相同的结果,对于所有在线的服务器,连接都是成功的,但如果一些服务器离线,函数永远不会发送异常,它会打破循环。
我希望有人能帮助我或给我建议,非常感谢。
register_shutdown_function注册要在脚本执行完成或exit()被称为
所以我认为即使你得到了新的连接,你也无法操作它。IMHO,更好的方法是创建自己的服务来处理DB连接,而不使用这个->getDoctrine()。在这个服务中,您可以调用不同的实体管理器(每个ip一个)并检查异常。若出现异常,您可以使用另一个实体管理器来运行查询,并可能将其设置为默认值;