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

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

问题描述

我想追查我的应用程序不能够连接到数据库服务器可能的原因。

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:在一个   在建立时发生错误   到服务器的连接。什么时候   连接到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)

在这些机器上,如果用户登录并启动服务手动将其正确连接到数据库,这是pretty的怪异。所以我想在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解析服务器名称(如果那是多么数据库服务器名称解析)
  • 有明确的政策/防火墙/等机器上,最初prevents传出的连接
  • 在别的...

当用户手动启动该服务不会发生的问题,所以一定有什么事要解决的问题。我想这是要么在启动时运行,但还没有完成我的服务启动时一个过程,或一个事实,即用户登录 - 可能的东西在他们的登录脚本

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地址数据库服务器(看是否能找到服务器)
  • 平数据库的名字服务器
  • 检查包含服务器名称(如果HKLM注册表稍后更新)
  • 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?

我也试图改变服务与适当的权限的用户帐户运行,但这并没有解决问题。需要注意的是本地系统帐户没有足够的权限连接到数据库服务器,因为该服务工作正常时手动启动。所以它是不一样的<一个href="http://stackoverflow.com/questions/249928/executable-started-by-a-windows-service-using-the-local-system-account-cannot-acc">this问题。

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.

更新:<一href="http://blogs.msdn.com/sql%5Fprotocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx"相对=nofollow>这篇文章给出了错误code商量好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.

。换句话说,让你的服务正常启动,但不要让它attmept,直到它知道它是否可以连接到数据库进行任何工作。如果你决定要记录每一个连接失败,你可以填写一份机器无用的日志条目千万要小心。我们会记录第一次连接失败,而不是再次除非重新启动该服务。

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天全站免登陆