【问题标题】:Will the Codeiginiter SQLServer Connection always persists ? Can a connection be non persistent from Codeigniter for MSSQL ConnectionCodeigniter SQL Server 连接会一直持续吗? Codeigniter for MSSQL Connection 的连接可以是非持久的吗
【发布时间】:2015-10-21 07:24:24
【问题描述】:

从 Codeigniter 3.0 PHP 连接时,为什么 SQL Server @@spid 对所有会话都相同?

我已从 Codeigniter 3.0.2 成功连接到 SQL Server。我正在使用 sqlserv 驱动程序。 为什么 SQL Server @@spid 总是为不同的用户会话返回相同的结果。
我的数据库连接未设置为 Persist。 我怎样才能让它不持久。这是我的数据库连接。

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '127.0.0.1',
    'username' => 'user',
    'password' => 'password',
    'database' => 'Testdb',
    'dbdriver' => 'sqlsrv',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

我对来自 codeigniter 的 Mysql Connection 进行了同样的尝试。它按预期工作非持久性。 ?

【问题讨论】:

    标签: php sql-server codeigniter


    【解决方案1】:

    参考以下链接, https://msdn.microsoft.com/en-us/library/cc644930%28SQL.90%29.aspx

    连接池(Microsoft Drivers for PHP for SQL Server)

    以下是有关 Microsoft Drivers for PHP for SQL Server 中连接池的重要注意事项:

    The Microsoft Drivers for PHP for SQL Server uses ODBC connection pooling.
    
    By default, connection pooling is enabled. When you connect to a server, the driver attempts to use a pooled connection before it creates a new one. If an equivalent connection is not found in the pool, a new connection is created and added to the pool. The driver determines whether connections are equivalent based on a comparison of connection strings.
    
    When a connection from the pool is used, the connection state is reset.
    
    Closing the connection returns the connection to the pool.
    

    我可以通过设置解决问题

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => '127.0.0.1',
        'username' => 'user',
        'password' => 'password',
        'database' => 'Testdb',
        'dbdriver' => 'sqlsrv',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    

    当我设置 pconnect' => TRUE 时,'app_id'=>''.rand() 在连接期间, 创建新的@@SPID。

    因此,通过有效地设置 'app_id' ,可以控制应用程序或用户组或用户的池

    警告:通过设置 pconnect' => TRUE , 'app_id'=>''.rand() ,您可以有效地消除应用程序中连接池的影响。 建议谨慎使用。

    【讨论】:

    • 您意识到这样做会无缘无故地让您的应用程序变慢吗? :)
    • @Mjh。谢谢,我在发布答案后意识到通过这样做我没有有效地使用连接池。这是一个测试,我正在做以了解连接池:)。
    猜你喜欢
    • 2011-02-25
    • 2012-01-29
    • 2011-08-09
    • 1970-01-01
    • 2017-07-12
    • 2016-10-08
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    相关资源
    最近更新 更多