sql server - Trying to understand erratic behavior of DB Connection used by Dapper -


How Dapper handles DB connections, and why DB connection is not being handled properly, I need some help in understanding better. I

I have coded around various possible cases in my code, I'm checking to see if the _connection is zero, and accordingly it is making a new connection accordingly. On the request of the requests, I think that the connection is sometimes open, and the second time I think the connection string is missing in the object (it is useless, but not set it on tap) I handle these cases accordingly But I would like to understand why these are different states in the connection, and it has not been settled even if the customer has explicitly used the user to wrap the code code C is implementing the statement whether it is in dealing with ADO.net connections, is Dapar a side effect or is there a problem with my code?

Connection Management Code

  Public Class DatabaseContorts: IDBS Consort {Private Reddone String _ Concert String; Private DB connection connection; Public DatabaseContext (string connection string) {_connectionString = connectionString; } Public IDb Get connection {(if (_connection == null) _connection = new SqlConnection (_connectionString); if (string.IsNullOrEmpty (_connection.ConnectionString)) _connection.ConnectionString = _connectionString; if (_connection.State! = ConnectionState. Open) _connection.Open (); return _knekshn;}}}  

client code

  public Aianmemerebl & lt; PostSearResults & gt; Search (SearchPosts By searchPostsBy) {DynamicParameters param; Var sql = GetSearchSql (searchPosts by, ultimate); // using SQL (var connection = _databaseContext.Connection) {var posts = connection.Query & lt; PostSearResults & Gt; (SQL, Ultimate); Usage I use a dumpper in a web application: toll ();}}  

I also do and I apply a DapperContext equally to you, but I apply IDispose to the class as follows:

  #region IDisposable private bool disposed = false; Protect Virtual Whoid Dispojh (bool Displeng) {if (! It Diaisspojd) {if (Disjkshn) (if (transaction! = NULL) {transaction.Dispose ();} if (CN! = NULL) {CnkDispose (); }}} This.disposed = true;} remove public zeros () (disassemble (true); GC.SuppressFinalize (this);} # My version of Dumpers is that which opens the connection when we call it Closed, the query closes after execution, if the connection is opened when I call the dump Just execute the query and leave the connection open.  

I also use DI, so my DapperContext lifetime is managed by DI Container, so it is disposed of when the web request reference ends falls and is destroyed. My Dapr repository thus, GetPE

 with a sampling method  private Reedonli IDbConnection context; private Reedonli Depr Contact Depr Contact: public SFRepository (DapperContext dbContext) {dapperContext = dbContext; reference = dbContext.Connection;} public Peedata GetPE (Int pid) {Peedata rec = reference. CAG & Lt; Peadaa & gt; ("Where Pedadata Select from ID = @ID", New {ID = PAID}). FirstOver Default (); Return rec; }  

It has been very stable and I do not have any strange behavior.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -