jj: fix endless errors on db timeout \o/

This commit is contained in:
Jonathan Jenne 2018-06-26 14:41:58 +02:00
parent 7b445c8d36
commit 65b5ac95e9
3 changed files with 95 additions and 34 deletions

View File

@ -1,11 +1,17 @@
Imports Oracle.ManagedDataAccess.Client Imports System.Data.SqlClient
Imports Oracle.ManagedDataAccess.Client
Public Class ClassDatabase Public Class ClassDatabase
Private Const NETWORK_TIMEOUT As Integer = -1
Private Shared SQLSERVERConnectionString As String Private Shared SQLSERVERConnectionString As String
Private Shared OracleConnectionString As String Private Shared OracleConnectionString As String
Public Shared DatabaseConnectionTimeout As Boolean = False
Public Shared Function Init() Public Shared Function Init()
Try Try
SQLSERVERConnectionString = MyConnectionString SQLSERVERConnectionString = MyConnectionString
Dim SQLconnect As New SqlClient.SqlConnection Dim SQLconnect As New SqlConnection
SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open() SQLconnect.Open()
SQLconnect.Close() SQLconnect.Close()
@ -16,95 +22,126 @@ Public Class ClassDatabase
End Try End Try
End Function End Function
Public Shared Function Return_Datatable(Select_anweisung As String, Optional userInput As Boolean = False) Public Shared Function Return_Datatable(sql_command As String, Optional userInput As Boolean = False)
Try Try
Dim SQLconnect As New SqlClient.SqlConnection Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlClient.SqlCommand Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open() SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = Select_anweisung SQLcommand.CommandText = sql_command
Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQLcommand) Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQLcommand)
Dim dt As DataTable = New DataTable() Dim dt As DataTable = New DataTable()
adapter1.Fill(dt) adapter1.Fill(dt)
SQLconnect.Close() SQLconnect.Close()
Return dt
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return dt
Catch ex As SqlException
CatchDatabaseTimeout(ex, sql_command)
Return Nothing
Catch ex As Exception Catch ex As Exception
If userInput = True Then If userInput = True Then
MsgBox("Error in Return Datatable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & Select_anweisung, MsgBoxStyle.Critical) MsgBox("Error in Return Datatable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If End If
ClassLogger.Add("Unexpected error in Return_Datatable: " & ex.Message, True) ClassLogger.Add("Unexpected error in Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & Select_anweisung, False) ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing Return Nothing
End Try End Try
End Function End Function
Public Shared Function Return_Datatable_CS(Select_anweisung As String, ConString As String, Optional userInput As Boolean = False) Public Shared Function Return_Datatable_CS(sql_command As String, ConString As String, Optional userInput As Boolean = False)
Try Try
Dim SQLconnect As New SqlClient.SqlConnection Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlClient.SqlCommand Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = ConString SQLconnect.ConnectionString = ConString
SQLconnect.Open() SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = Select_anweisung SQLcommand.CommandText = sql_command
Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQLcommand) Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQLcommand)
Dim dt As DataTable = New DataTable() Dim dt As DataTable = New DataTable()
adapter1.Fill(dt) adapter1.Fill(dt)
SQLconnect.Close() SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return dt Return dt
Catch ex As SqlException
CatchDatabaseTimeout(ex, sql_command)
Return Nothing
Catch ex As Exception Catch ex As Exception
If userInput = True Then If userInput = True Then
MsgBox("Error in Return_Datatable_CS - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & Select_anweisung, MsgBoxStyle.Critical) MsgBox("Error in Return_Datatable_CS - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If End If
ClassLogger.Add("Unexpected error in Return_Datatable_CS: " & ex.Message, True) ClassLogger.Add("Unexpected error in Return_Datatable_CS: " & ex.Message, True)
ClassLogger.Add("#SQL: " & Select_anweisung, False) ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing Return Nothing
End Try End Try
End Function End Function
Public Shared Function Execute_non_Query(ExecuteCMD As String, Optional userInput As Boolean = False) Public Shared Function Execute_non_Query(sql_command As String, Optional userInput As Boolean = False)
Try Try
Dim SQLconnect As New SqlClient.SqlConnection Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlClient.SqlCommand Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open() SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo 'Update Last Created Record in Foo
SQLcommand.CommandText = ExecuteCMD SQLcommand.CommandText = sql_command
SQLcommand.ExecuteNonQuery() SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose() SQLcommand.Dispose()
SQLconnect.Close() SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return True Return True
Catch ex As SqlException
CatchDatabaseTimeout(ex, sql_command)
Return Nothing
Catch ex As Exception Catch ex As Exception
If userInput = True Then If userInput = True Then
MsgBox("Error in Execute non query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & ExecuteCMD, MsgBoxStyle.Critical) MsgBox("Error in Execute non query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If End If
ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True) ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True)
ClassLogger.Add("#SQL: " & ExecuteCMD, False) ClassLogger.Add("#SQL: " & sql_command, False)
Return False Return False
End Try End Try
End Function End Function
Public Shared Function Execute_Scalar(cmdscalar As String, ConString As String, Optional userInput As Boolean = False) Public Shared Function Execute_Scalar(sql_command As String, ConString As String, Optional userInput As Boolean = False)
Dim result Dim result
Try Try
Dim SQLconnect As New SqlClient.SqlConnection Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlClient.SqlCommand Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = ConString SQLconnect.ConnectionString = ConString
SQLconnect.Open() SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo 'Update Last Created Record in Foo
SQLcommand.CommandText = cmdscalar SQLcommand.CommandText = sql_command
result = SQLcommand.ExecuteScalar() result = SQLcommand.ExecuteScalar()
SQLcommand.Dispose() SQLcommand.Dispose()
SQLconnect.Close() SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return result Return result
Catch ex As SqlException
CatchDatabaseTimeout(ex, sql_command)
Return Nothing
Catch ex As Exception Catch ex As Exception
If userInput = True Then If userInput = True Then
MsgBox("Error in Execute Scalar - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & cmdscalar, MsgBoxStyle.Critical) MsgBox("Error in Execute Scalar - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If End If
ClassLogger.Add("Unexpected error in Execute_Scalar: " & ex.Message, True) ClassLogger.Add("Unexpected error in Execute_Scalar: " & ex.Message, True)
ClassLogger.Add("#SQL: " & cmdscalar, False) ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing Return Nothing
End Try End Try
End Function End Function
@ -175,4 +212,16 @@ Public Class ClassDatabase
Return Nothing Return Nothing
End Try End Try
End Function End Function
Public Shared Sub CatchDatabaseTimeout(ex As SqlException, sql_command As String)
Dim FatalErrors As New List(Of Integer) From {-1, -2, 121}
If DatabaseConnectionTimeout = False And FatalErrors.Contains(ex.Number) Then
DatabaseConnectionTimeout = True
MsgBox($"Database could not be reached. Global Indexer will NOT work without a database!{vbCrLf}{vbCrLf}Please check your connection and restart.", MsgBoxStyle.Critical, "Critical Error")
ClassLogger.Add("Network timeout error in Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
End If
End Sub
End Class End Class

View File

@ -151,6 +151,11 @@ Public Class ClassFolderWatcher
End If End If
End Function End Function
Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs) Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
If ClassDatabase.DatabaseConnectionTimeout = True Then
ClassLogger.Add(">> File handling aborted because of database timeout error!", False)
Exit Sub
End If
Try Try
For Each row As DataRow In DTEXCLUDE_FILES.Rows For Each row As DataRow In DTEXCLUDE_FILES.Rows
Dim content As String = row.Item(0).ToString.ToLower Dim content As String = row.Item(0).ToString.ToLower
@ -158,7 +163,7 @@ Public Class ClassFolderWatcher
Exit Sub Exit Sub
End If End If
Next Next
Dim handleType As String Dim handleType As String
If e.FullPath.ToLower.EndsWith(".msg") Then If e.FullPath.ToLower.EndsWith(".msg") Then
handleType = "|FW_OUTLOOK_MESSAGE|" handleType = "|FW_OUTLOOK_MESSAGE|"
@ -172,11 +177,11 @@ Public Class ClassFolderWatcher
Else Else
ClassLogger.Add(">> Folderwatcher: File already exists:" & e.FullPath, False) ClassLogger.Add(">> Folderwatcher: File already exists:" & e.FullPath, False)
End If End If
Catch ex As Exception Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created") MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created")
End Try End Try
End Sub End Sub
End Class End Class

View File

@ -347,7 +347,7 @@ Public Class frmStart
End If End If
End If End If
Catch ex As Exception Catch ex As Exception
MsgBox("Unexpected Error in Abort Indexing: " & vbNewLine & ex.Message, MsgBoxStyle.Critical) MsgBox("Unexpected Error in Abort Indexing: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
@ -424,7 +424,7 @@ Public Class frmStart
Try Try
'Dim sql = sql_UserID 'Dim sql = sql_UserID
'Dim splash As New frmSplash() 'Dim splash As New frmSplash()
@ -669,11 +669,18 @@ Public Class frmStart
End Sub End Sub
Private Sub TimerFolderWatch_Tick(sender As Object, e As EventArgs) Handles TimerFolderWatch.Tick Private Sub TimerFolderWatch_Tick(sender As Object, e As EventArgs) Handles TimerFolderWatch.Tick
If ClassDatabase.DatabaseConnectionTimeout = True Then
TimerFolderWatch.Enabled = False
ClassLogger.Add(">> Timer stopped because of database timeout error!", False)
Exit Sub
End If
Try Try
If FW_started = True Or FWSCAN_started = True Then If FW_started = True Or FWSCAN_started = True Then
'Prüfen ob alle Files abgearbeitet wurden 'Prüfen ob alle Files abgearbeitet wurden
Dim sql = "SELECT * FROM TBGI_FILES_USER WHERE WORKED = 0 AND HANDLE_TYPE like '%|FW%' AND UPPER(USER@WORK) = UPPER('" & Environment.UserName & "')" Dim sql = "SELECT * FROM TBGI_FILES_USER WHERE WORKED = 0 AND HANDLE_TYPE like '%|FW%' AND UPPER(USER@WORK) = UPPER('" & Environment.UserName & "')"
DTACTUAL_FILES = ClassDatabase.Return_Datatable(sql, True) DTACTUAL_FILES = ClassDatabase.Return_Datatable(sql, True)
If DTACTUAL_FILES.Rows.Count > 0 Then If DTACTUAL_FILES.Rows.Count > 0 Then
ABORT_INDEXING = False ABORT_INDEXING = False
' Dim fil As String ' Dim fil As String