getting db connection through singleton class

前端 未结 6 1767
Happy的楠姐
Happy的楠姐 2020-11-30 02:38

I have created an singleton class, this class returns a database connection. So my question is this connection is also satisfying singleton criteria?
If no, than how I c

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 03:29

    I can't answer that question without seeing some code, I think. If you are saying that you will have only one DB connection instance in your application, that might work if you can guarantee that your application will run on only one thread (or at least that all operations using the DB connection does), since you can't (as far as I know anyway) run several operations in parallell on the same connection.

    Also, if it means that your application will keep the connection open in between uses, I would advise against it. DB connections are limited resources on the DB server, so you should keep them open only when they are needed, and then close them.

提交回复
热议问题