简体   繁体   English

SQL Server Symfony2的循环连接

[英]Connections in loop for SQL Server Symfony2

i´ve been trying to create a loop for different MSSQL Server in Symfony 2.3, but if some of the servers is off or the MSSQL service is down, the app don't make an exception and it breaks the function. 我一直在尝试为Symfony 2.3中的其他MSSQL Server创建一个循环,但是如果某些服务器关闭或MSSQL服务已关闭,则该应用程序不会例外,并且会中断功能。

This is the main controller: 这是主控制器:

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");
}

This function create the conection: 此函数创建连接:

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 />";
}

I Always get the same result, for all the servers on line, the connection is sucessfully but if some of the servers is offline the function never send an exception and it breaks the loop. 我总是得到相同的结果,对于所有在线服务器,连接都是成功的,但是如果某些服务器处于脱机状态,该函数将永远不会发送异常,并且会中断循环。

I hope someone can help me or give an advice, thanks a lot. 我希望有人能帮助我或提供建议,非常感谢。

register_shutdown_function Registers a callback to be executed after script execution finishes or exit() is called register_shutdown_function注册在脚本执行完成或调用exit()之后要执行的回调

So I think you can't operate new connection even if you get it. 因此,我认为即使获得新连接也无法操作。 IMHO, the better approach will be creation of own service which handle DB connections and don't use this->getDoctrine(). 恕我直言,更好的方法是创建自己的服务来处理数据库连接,而不使用this-> getDoctrine()。 In this service you can call for different entity managers (one for every ip) and check for exception. 在此服务中,您可以要求不同的实体管理器(每个ip一个)并检查异常。 If exception rise you can use another entity manager to run query and maybe set it as default; 如果出现异常,则可以使用另一个实体管理器来运行查询,并将其设置为默认值;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM