当服务在引导时启动时数据库连接不工作,但在手动启动时工作 [英] database connection doesn't work when service starts at boot but works when it's manually started

查看:185
本文介绍了当服务在引导时启动时数据库连接不工作,但在手动启动时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要追踪我的应用程式无法连线至db伺服器的可能原因。

I'm trying to track down possible causes of my app not being able to connect to a db server.

我有一个Windows服务,在启动时连接到数据库。该服务在具有可靠有线网络连接的计算机上运行。它与安装启动自动,所以通常它开始时,窗口,并在几乎所有情况下,这工作正常。

I have a windows service that connects to the database when it starts. The service runs on machines with a reliable wired network connection. It's installed with startup Automatic so normally it starts when windows does, and in almost all cases this works fine.

但是,使用一组XP机器(我无法控制),当服务启动时,数据库连接失败。发生标准异常:

However, with one set of XP machines (that I don't have control of) the database connection fails when the service starts up with windows starting. The standard exception is raised:


System.Data.SqlClient.SqlException:建立
a时发生
错误连接到服务器。当
连接到SQL Server 2005时,此
失败可能是由于
在默认设置SQL Server
下不允许远程连接的事实造成的。
(提供程序:SQL网络接口,
错误:26 - 找不到
服务器/实例指定错误)

System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

在这些计算机上,如果用户登录并手动启动服务,它会正确连接到数据库,这是非常奇怪的。所以我想在Windows启动时的问题是:

On these machines if a user is logged in and starts the service manually it connects to the database correctly, which is pretty weird. So I guess the problem at windows startup is either:


  • 服务在网络连接之前启动

  • 该服务在计算机可以连接到DNS以解析服务器名称之前启动(如果这是解析数据库服务器名称)

  • 计算机上有策略/防火墙等

此问题不会发生在用户手动启动服务,所以发生了一些事情以解决问题。我想这是一个在启动时运行的进程,但是在我的服务启动时还没有完成,或者用户已经登录 - 可能是他们的登录脚本中的一些。

The problem doesn't occur when a user manually starts the service, so something must have happened to resolve the problem. I guess this is either a process that runs at startup but hasn't finished when my service starts, or the fact that a user has logged in - possibly something in their logon script.

我没有直接访问机器,所以需要提出一个好主意,什么问题可能是一个方法,以确定是否是正确的。我不能重复部署诊断程序,因此需要第一次彻底。

I don't have direct access to the machines, so need to come up with a good idea of what the problem could be and a way to identify if that's correct. I can't repeatedly deploy diagnostic programs so need to be thorough the first time.

所以第一个问题是:有谁知道桌面策略,网络策略或软件造成这种情况?第二个问题:我可以做什么来准确诊断发生了什么?

So first question is: does anyone know of desktop policies, network policies or software that could cause this situation? Second question: what can I do to diagnose exactly what is happening?

我想创建一个新的诊断服务,它也将被安装以自动启动,并将执行各种操作来查看发生了什么,并记录此信息。例如:

I'm thinking of creating a new diagnostic service which will also be installed to start automatically and will perform various actions to see what is going on, and log this info. eg:


  • 运行ipconfig / all(以查看是否有网络连接)

  • 通过IP地址ping数据库服务器(查看是否可以找到服务器)

  • 按名称ping数据库服务器

  • 检查HKLM注册表

  • 创建与数据库的sql连接(以了解此项开始工作的时间)

  • 每隔几秒钟重复一次这些步骤。

  • run "ipconfig /all" (to see if there is a network connection)
  • ping the database server by IP address (to see if it can find the server)
  • ping the database server by name
  • check the HKLM registry key that contains the server name (in case the HKLM registry is later updated)
  • create sql connection to the database (to find out when this starts working)
  • repeat these steps every few seconds.

我将安装此服务,重新启动计算机,然后一段时间后用户登录。诊断应显示一些有用的信息...但是任何人都有更好的想法或其他建议?

I'll have this service installed, restart the machine, then after a period have a user log in. The diagnostics should show some useful info... but anyone have a better idea or additional suggestions?

我也尝试更改服务作为用户帐户运行具有适当的权限,但didn不能解决问题。请注意,本地系统帐户有足够的权限连接到db服务器,因为该服务在手动启动时工作正常。因此与不同这个问题

I have also tried changing the service to run as a user account with suitable privileges, but that didn't resolve the problem. Note that the Local System account does have sufficient priveleges to connect to the db server, since the service works fine when manually started. so it's not the same as this question.

更新:麻烦的机器正在运行Win XP。

UPDATE: troublesome machines are running Win XP.

UPDATE:本文对错误代码26进行了很好的讨论。

UPDATE: this article gives a good discussion of error code 26

推荐答案

我们解决问题的方式是有一个启动线程,直到可以连接到数据库才退出。我们会经常尝试连接,并记录我们无法连接的事实。一旦我们建立连接,我们将继续服务。

We have encountered just this issue before. The way we solved the problem was to have a start thread that did not exit until a connection could be made to the database. We would attempt the connection every so often and log the fact that we could not connect. Once we established the connection we would continue the service.

换句话说,让您的服务正常开始,但不要让它执行任何工作,直到它知道它可以连接到数据库。如果您决定记录每个连接失败,请小心,因为您可能用无用的日志条目填充机器。

In otherwords, let your service start normally, but don't let it attmept to perform any work until it knows if it can connect to the database. Do be careful if you decide to log every connection failure as you could fill up a machine with useless log entries. We would log the first connection failure and not again unless the service was restarted.

当然,如果你的数据库总是在你的服务本地,更好的解决方案是创建一个依赖于数据库窗口服务,但它听起来不像你的数据库是本地的。

Of course if you database is always local to your service, the better solution is to create a dependency to the database windows service, but it doesn't sound like your database is local.

这篇关于当服务在引导时启动时数据库连接不工作,但在手动启动时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆