2

I am fetching my Database object in PetaPoco using this method:

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

My connection string in 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" />

Question is already connection pooling is enabled. Do I have to set KeepConnectionAlive as true or false? What is the use of this property?

Tim Liberty
  • 2,099
  • 4
  • 23
  • 39

1 Answers1

2

Yes, pooling is already enable and you don't need to set KeepConnectionAlive to true (in fact, it will bring trouble if you set it to true)

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
  • 1
    Sorry, what kind of trouble is it? – Tim Liberty Jul 12 '16 at 06:26
  • 1
    Like getting errors for sharing the same Conn for the same record set – Eduardo Molteni Jul 12 '16 at 13:23
  • Sorry could you explain what do you mean by that? I have turned it off already but I need to understand what your valuable comments mean. – Tim Liberty Jul 12 '16 at 14:36
  • If you let your connection open for more than one request or thread and you try to access the same set of records (the same query) in different request or threads, PetaPoco, (in fact ADO.net, the library behind PetaPoco) will error out. See https://github.com/CollaboratingPlatypus/PetaPoco/issues/103 or http://stackoverflow.com/questions/7052350/how-to-create-a-dal-using-petapoco/9995413 for more info – Eduardo Molteni Jul 12 '16 at 18:49