duanmeng7865 2015-10-21 07:24
浏览 52
已采纳

Codeigniter SQL Server连接是否会一直存在? 可以从Codeigniter for MSSQL Connection连接非持久性

Why SQL Server @@spid is same for all session when connecting from Codeigniter 3.0 PHP ?

I have successfully connected to SQL Server from Codeigniter 3.0.2. I am using the sqlserv Driver. Why is the SQL Server @@spid always returning the same for different user sessions.
My database connection is not set to Persist. How can i make it not persist. Here is my DB connection.

$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
);

I tried the same for Mysql Connection from codeigniter. It works non persistent as expected. ?

  • 写回答

1条回答 默认 最新

  • doucan8246326 2015-10-22 08:36
    关注

    In reference to the below link, https://msdn.microsoft.com/en-us/library/cc644930%28SQL.90%29.aspx

    Connection Pooling (Microsoft Drivers for PHP for SQL Server)

    The following are important points to note about connection pooling in the 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.
    

    I could resolve the problem by setting

    $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
    );
    

    when i set pconnect' => TRUE , 'app_id'=>''.rand() during connection , new @@SPIDs are created.

    So by effectively setting the 'app_id' , it is possible to control the pooling for application or by users groups or by users

    caution:By setting pconnect' => TRUE , 'app_id'=>''.rand() , you are effectively nullifying the effect of connection pooling in your application. A careful use is recommended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算