2015-10-21 28 views
0

爲什麼從Codeigniter 3.0 PHP連接時SQL Server @@ spid與所有會話相同?Codeiginiter SQLServer連接是否會一直存在?從Codeigniter for MSSQL連接可以建立非持久連接

我已成功從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連接相同。它的工作原理與預期一致。 ?

回答

0

在參考下面的鏈接, https://msdn.microsoft.com/en-us/library/cc644930%28SQL.90%29.aspx

連接池(用於PHP微軟的驅動程序爲SQL Server)

以下是需要注意的微軟驅動程序PHP的SQL連接池要點服務器:

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. 

我可以通過設置

解決問題

當我在連接期間設置pconnect'=> TRUE,'app_id'=>''。rand()時, 將創建新的@@ SPID。

因此,通過有效地設置「APP_ID」,它可以控制應用程序或由用戶組或由用戶

謹慎池:通過設置pconnect」 => TRUE,‘APP_ID’=>‘’ .rand(),可以有效地取消應用程序中連接池的效果。 建議謹慎使用。

+0

你意識到你這樣做會讓你的應用程序變得更慢嗎? :) – Mjh

+0

@Mjh。謝謝,我發佈了我的答案後意識到,通過使我沒有有效地使用連接池。這是一個測試,我正在理解連接池:)。 – srp