PetaPoco KeepConnectionAlive use

本文关键字:use KeepConnectionAlive PetaPoco | 更新日期: 2023-09-27 17:55:26

我正在使用此方法在PetaPoco中获取我的数据库对象:

internal static Database GetCurrentDatabase()
    {
        if (HttpContext.Current == null)
        {
            if (Thread.GetData(Thread.GetNamedDataSlot(_databaseKey)) != null)
                return Thread.GetData(Thread.GetNamedDataSlot(_databaseKey)) as Database;
            else
            {
                var database = new Database("testDB");
                database.KeepConnectionAlive = true;
                database.EnableAutoSelect = true;
                database.CommandTimeout = 180;
                Thread.SetData(Thread.GetNamedDataSlot(_databaseKey), database);
                return database;
            }
        }
        else
        {
            if (HttpContext.Current.Items.Contains(_databaseKey))
                return HttpContext.Current.Items[_databaseKey] as Database;
            else
            {
                Database database = new Database("testDB");
                database.EnableAutoSelect = true;
                database.CommandTimeout = 180;
                database.KeepConnectionAlive = true;
                HttpContext.Current.Items[_databaseKey] = database;
                return database;
            }
        }
    }

我在 web.config 中的连接字符串:

<add name="testDB" connectionString="Data Source=192.168.10.10;Initial Catalog=testDB;User Id=test;Password=test;pooling='true'; Max Pool Size=200"
      providerName="System.Data.SqlClient" />

问题是否已启用连接池。我必须KeepConnectionAlive设置为真还是假?此属性有什么用?

PetaPoco KeepConnectionAlive use

是的,池化已经启用,你不需要将 KeepConnectionAlive 设置为 true(事实上,如果你把它设置为 true,会带来麻烦)