ASP.NET: Proper implementation of nested API calls using connection pool and nested transactions
I need to know the best way to do the following. I have nested business level APIs (say level 1 & level 2). L1 needs to call L2. Both APIs use the database layer directly at their own nesting levels.
Now, in the database layer, I fetch the db connection from t开发者_如何学Che pool each time as follows:
SqlConnection conn = new SqlConnection(connString);
conn.Open();
Is it proper to fetch the db connection each time on every DB level call as above? I know it will return a connection from the ASP.NET connection pool.
However, wouldn't it be better to maintain the same DB connection throughout the nested calls (or throughout the current http request lifetime)?
Will fetching a connection from the pool each time cause issues with nested TransactionScopes?
Answer to #1 It's ok, but if your system is really busy that you'll need to up the max connection parameter in your connection string. Ideally, (at least the way I do it is: - One connection for Request Response cycle.)
2,3 Depending on how you implement the two layers, and yes transactions will be a problem if you have only one connection that the two layers are using. I'm guessing the two layers are not really "connected" in that you don't need transactions to work across these layers. If so, then I'd go the route you have.
精彩评论