Pros and Cons of using Singleton Pattern in DAL

前端 未结 6 1587
感动是毒
感动是毒 2021-02-06 16:26

I have asked to use singleton pattern implemented DAL, but I think its difficult to pool the connections,use transactions..etc

I would like to know the pros and cons and

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 17:20

    The best practice for connection pooling is to not implement it yourself, but instead let the ADO.NET framework take care of it.

    You can set connection pooling options as parameters within the connection string. Then, every connection that is opened with that string will be supplied from the connection pool that is implemented and managed by the framework. When you close or dispose of the OracleConnection, the underlying connection is not destroyed but will instead go back onto the pool.

    This is described here: http://msdn.microsoft.com/en-us/library/ms254502.aspx

    About the use of Singletons in general: I've used them to wrap the data access layer, and it has always worked well.

    Note that transactions only apply to specific connections, not the database as a whole. This means you can have several threads running, and each thread can read and write to the database through independent transactions, providing each thread uses a separate OracleConnection instance.

提交回复
热议问题