It's bad design that leveraging singleton design pattern in db connection

本文探讨了使用单例模式获取数据库连接时所遇到的问题,包括无法同时开启多个SqlDataReader/SqlCommand及由此导致的线程阻塞问题。文章还强调了连接池在处理昂贵的连接开销方面的重要性,并指出创建SqlConnection实例或打开连接并不会带来显著的性能损失。

Refer to http://stackoverflow.com/questions/814206/getting-db-connection-through-singleton-class

You won't be able to open multiple SqlDataReaders/Commands with the connection at the same time and will run into thread blocking issues if you are trying to share the same connection object with multiple threads.

If you are attempting to avoid a performance hit of "new SqlConnection()" or "connection.Open()" be advised that there really is no performance hit there because of the connection pooling going on behind the scenes. Connection Pooling handles the opening/closing of the expensive connections. Not the SqlConnection object.

转载于:https://www.cnblogs.com/webglcn/archive/2012/09/03/2668767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值