简体   繁体   English

在C#中处理多个数据库连接

[英]Handle multiple database connections in c#

I'm currently trying to figure out what would be the best way to handle a lot of database-connections. 我目前正在尝试找出处理大量数据库连接的最佳方法。

First of all a basic outline of the project I need to realize: 首先,我需要实现该项目的基本概述:

We have a bunch of xml-files with instructions for the application. 我们有一堆带有应用程序说明的xml文件。 In the application they are represented as objects. 在应用程序中,它们表示为对象。 These objects are placed in a queue and which will be processed in one of four threads. 这些对象放置在队列中,并将在四个线程之一中进行处理。

Each of these xml-file objects will generate an export to an individual mysql-database and a big status-database. 这些xml文件对象中的每个对象都将生成一个导出到单个mysql数据库和一个大状态数据库。

My problem is, that I'm unsure how to handle these mysql-database connections best. 我的问题是,我不确定如何最好地处理这些mysql数据库连接。 The first hunch I had was to open the status-database once and pass it as an object to each of the xml-file objects. 我的第一步是打开状态数据库一次,并将其作为对象传递给每个xml文件对象。 However the application will run for a long time so I'd definitely run in an connection error. 但是该应用程序将运行很长时间,因此我肯定会在连接错误中运行。

The other way would be to open a connection in each xml-objects once the data-export is ready, which seems not really performant to me. 另一种方法是,一旦数据导出准备好,就在每个xml对象中打开一个连接,这对我来说似乎并不是很有效。

How should I handle this connection? 我应该如何处理此连接?

You shall not use separate sessions/connections for each "export task". 您不得为每个“导出任务”使用单独的会话/连接。 Each login to database will cause non-necessary operations (like writes to database log) and this will slow down your entire application. 每次登录数据库都将导致不必要的操作(例如写入数据库日志),这将减慢整个应用程序的速度。

If you are expecting connection problems - use try-catch and handle database exceptions and just reconnect when connection broken. 如果您预计会出现连接问题,请使用try-catch并处理数据库异常,并在连接断开时重新连接。

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

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