且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

几个问题的***做法...

更新时间:2023-12-03 14:42:04

我没有对那些不是Web应用程序的数据库做任何事情,所以我可能会错,但我会精益一个单一的连接.正如您所说明的,当您不必担心在每个方法中创建和销毁连接时,代码会简单得多(尽管可以说,即使在Web应用程序中也不必这样做). >
但是,您可能会遇到的一个问题是,如果您有多个并发用户,并且始终保持连接处于打开状态,则数据库侧的连接可能用光了!

尽管在您的示例中我看到您正在关闭全局连接,但没有打开它-这将行不通! ;)
I haven''t done anything with databases that wasn''t a web app, so I could be wrong, but I''d lean toward having a single connection. As you illustrated, the code is much simpler when you don''t have to worry about creating and destroying connections in every single method (though arguably you shouldn''t necessarily have to do that even in a web application).

One issue you might run into, though, is if you have numerous concurrent users and you keep your connection open all the time, you might run out of connections on the database side!

Though I see in your example that you are closing the global connection, but not opening it - that won''t work! ;)