开发者

Is passing a Command object/Reader to a function a good idea?

I've been getting some inexplicable errors (running out of connections from the connection pool) with MySql database with .Net 4 (C#). Till now all my attempts at finding a reason for this have been in vain. Now I also have a situation in which a lock on a table is not clear开发者_JS百科ed for a long time even though all I have been doing is read operations from it.

My code looks okay (I've put all readers and connections in using blocks). The only anomaly I have is that I've been passing MySqlCommand and MySqlDataReader objects as parameters to functions who work with them.

That doesn't seem like a bad idea for me because it avoids some repetition (DRY!). But since I can't find any other explanation I have to suspect that this is causing the problem.

What do you think?


No, passing references to connections, commands and data readers is not a problem. You can throw the references around any way you like, as long as there is a clear responsibility for which part of the code is disposing them.

You didn't say anything about disposing command objects. They have a Dispose method, and should also be disposed to be on the safe side.


As Guffa says, there aren't any technical problems with passing references to these instances. However, a better practice would be to populate a DTO within the method doing the data access, and either passing that to the next method in the chain, or returning it. It would reduce your complexity and alleviate confusion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜