We are working on a web application built o开发者_JAVA百科n .NET and SQL server. In production environment, the database and the web server would be running on different systems.
Given this, what would be the best way to ensure that the date and time between the business logic layer (running on the web server) and the database server are synchronized?
Would firing a query to the server with SELECT GETDATE() be efficient?
For instance instead of initializing date time properties in our classes to DateTime.Now we will initialize with DBServer.Now where "Now" is a static property which would fire the SELECT GETDATE() query and get a response.
Thanks.
Depending on how accurate you need this, it should be sufficient to have the machines joined to a domain and synchronize the time with the domain server. In this setup the worstations will all sync their time to the Primary Domain Controller.
Don't mix the two sources of date information. Use your development environment's datetime implementation (and only that), or use your database's implementation. This way, you know the date values always come from the same place, and the time setting on the database server doesn't matter at all. It shouldn't be a great hurdle to stop using WHERE releaseDate < NOW()
and using a date parameter from the application instead.
Apart from having a single source of date info, it also pushes you to deal with dates in the "right" way - using correct data types. I don't know about .NET, but I've seen a lot of date string fiddling in the PHP world.
Another thing you can (and should) do, is to have your time set on all servers using NTP - or any other such solutions (see Chris Taylor's answer).
Depending on how precise you need, rather than DBServer.Now always hitting the database every time, maybe have it cache the difference between the database and web server. Then when you need the database time, use that cached offset value. Re-calculate the cached value every X minutes to keep them in synch.
精彩评论