简体   繁体   English

DB的长期连接还是按需连接?

[英]DB's long-live connections or on-demand connections?

I was assigned to implement an application (in C++) to evaluate pending submissions (a submission is a programming algorithm to a given problem). 我被分配去实现一个应用程序(用C ++)来评估未决的提交(提交是给定问题的编程算法)。 A site (in ASP.NET MVC) posts problems and allows the users to submit their answers, then marks the submissions as "pending to evaluation" on the database (SQL Server 2008R2) and that is when my work begins: 一个站点(位于ASP.NET MVC中)发布问题,并允许用户提交答案,然后在数据库(SQL Server 2008R2)上将提交的内容标记为“待评估”,这就是我的工作开始的时间:

I'll have 3 (or maybe more) instances of my application running as services. 我将有3个(或更多)应用程序实例作为服务运行。 Each instance has to check if any pending submissions exists in the DB every 2 seconds. 每个实例必须每2秒检查一次数据库中是否存在任何待处理的提交。 If it exists I retrieve and compile it, after successful compilation I execute it and finally, after execution, check the correctness of the answer. 如果存在,我将对其进行检索和编译,在成功编译之后,我将其执行,最后在执行后,检查答案的正确性。 Then I update that submission setting the results and deleting it from the pending table. 然后,我更新该提交,设置结果并将其从挂起的表中删除。 I need to specify in the DB the current status of the pending submission (compiling, running, judging). 我需要在数据库中指定挂起提交的当前状态(编译,运行,判断)。

The time to evaluate a submition is ~(1-3)s and the same instance never evaluates more that one submission at the same time. 评估提交的时间为〜(1-3)s,并且同一实例永远不会同时评估一个提交。

My problem is: How to connect to the DB server? 我的问题是:如何连接到数据库服务器? I have 3 possibles solutions and I need to know what should be better (in order to increase efficiency) and why: 1 - Establish a connection to the DB once I instantiate the application and never close it (close it when I delete the instance or shut down the server, that theoretically never will happen.) 2 - Open a connection each 2s in order to get the pending submission (if any one exists) wait for the full evaluation process to end, sets the evaluations results and then close the connection. 我有3种可能的解决方案,我需要知道哪种更好(以提高效率)以及原因: 1-一旦实例化应用程序就建立了与数据库的连接,并且从不关闭(删除实例或关闭实例时将其关闭)关闭服务器,从理论上讲是永远不会发生的。)2-每2秒打开一个连接,以获取挂起的提交(如果存在),等待完整的评估过程结束,设置评估结果,然后关闭连接。 3 - Same as 2, but closing the connection when I retrieve the submission, when the compilation finish, open it again and update pending submission's status, close it, when the execution finish, open it again and update pending submission's status, close it, finally when the judging finish open it and set the evaluation result. 3-与2相同,但是当我检索提交时关闭连接,当编译完成时,再次打开它并更新未决提交状态,关闭,执行完成后,再次打开并更新未决提交状态,关闭它,最后,在评审结束后将其打开并设置评估结果。

You don't say what database access library you are using (ODBC, ado.net, other?). 您没有说您正在使用什么数据库访问库(ODBC,ado.net等)。 Opening and closing database connections is a relatively expensive operation. 打开和关闭数据库连接是一个相对昂贵的操作。 You should be using some sort of connection pooling scheme in your db access framework. 您应该在数据库访问框架中使用某种连接池方案。 A pool of connections is opened for a period of time, and when your app opens a connection it will get handed an already open connection from a pool. 连接池会打开一段时间,当您的应用打开连接时,它将从池中移出一个已经打开的连接。 That will make it more efficient. 这样可以提高效率。 Go read about connection pooling for SQL Server 阅读有关SQL Server的连接池的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM