NTP Analyzer  0.8.2
Analyze the operation of time servers
SqlDatabaseChecker.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Data.Common;
24 using System.Threading;
25 using Ntp.Common.App;
26 using Ntp.Common.Log;
27 using Ntp.Data.Log;
28 
29 namespace Ntp.Data
30 {
31  public sealed class SqlDatabaseChecker
32  {
34  {
35  this.controller = controller;
36  this.factory = factory;
37  this.log = log;
38  stop = false;
39  }
40 
42  private readonly ISqlFactory factory;
43  private readonly LogBase log;
44  private bool stop;
45 
46  public void CheckConnection()
47  {
48  controller.ExitApplication += ControllerExitApplication;
49 
50  var connection = factory.CreateGenericConnection();
51  log.AwaitingDbLink(connection);
52 
53  bool first = true;
54  bool done = false;
55 
56  do
57  {
58  try
59  {
60  connection.Open();
61  done = true;
62  }
63  catch (DbException ex)
64  {
65  log.DbLinkError(ex);
66 
67  if (first)
68  {
69  log.DbLinkDown();
70  }
71 
72  first = false;
73  Thread.Sleep(5000);
74  }
75  } while (!done && !stop);
76 
77  if (done)
78  {
79  log.DbLinkUp();
80 
81  try
82  {
83  connection.Close();
84  }
85  catch (Exception e)
86  {
87  log.WriteLine(e);
88  }
89  }
90 
91  controller.ExitApplication -= ControllerExitApplication;
92  }
93 
94  private void ControllerExitApplication(object sender, EventArgs e)
95  {
96  stop = true;
97  }
98  }
99 }
readonly IApplicationController controller
function factory
Definition: jquery.js:15
var e
Definition: bootstrap.min.js:6
readonly ISqlFactory factory
SqlDatabaseChecker(ISqlFactory factory, IApplicationController controller, LogBase log)
void ControllerExitApplication(object sender, EventArgs e)