diff --git a/Global_Indexer/ClassDatabase.vb b/Global_Indexer/ClassDatabase.vb index 5b2418c..937b41a 100644 --- a/Global_Indexer/ClassDatabase.vb +++ b/Global_Indexer/ClassDatabase.vb @@ -1,11 +1,17 @@ -Imports Oracle.ManagedDataAccess.Client +Imports System.Data.SqlClient +Imports Oracle.ManagedDataAccess.Client Public Class ClassDatabase + Private Const NETWORK_TIMEOUT As Integer = -1 + Private Shared SQLSERVERConnectionString As String Private Shared OracleConnectionString As String + + Public Shared DatabaseConnectionTimeout As Boolean = False + Public Shared Function Init() Try SQLSERVERConnectionString = MyConnectionString - Dim SQLconnect As New SqlClient.SqlConnection + Dim SQLconnect As New SqlConnection SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.Open() SQLconnect.Close() @@ -16,95 +22,126 @@ Public Class ClassDatabase End Try 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 - Dim SQLconnect As New SqlClient.SqlConnection - Dim SQLcommand As SqlClient.SqlCommand + Dim SQLconnect As New SqlConnection + Dim SQLcommand As SqlCommand SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.Open() SQLcommand = SQLconnect.CreateCommand - SQLcommand.CommandText = Select_anweisung - Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(SQLcommand) + SQLcommand.CommandText = sql_command + Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQLcommand) Dim dt As DataTable = New DataTable() adapter1.Fill(dt) 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 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 ClassLogger.Add("Unexpected error in Return_Datatable: " & ex.Message, True) - ClassLogger.Add("#SQL: " & Select_anweisung, False) + ClassLogger.Add("#SQL: " & sql_command, False) Return Nothing End Try 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 - Dim SQLconnect As New SqlClient.SqlConnection - Dim SQLcommand As SqlClient.SqlCommand + Dim SQLconnect As New SqlConnection + Dim SQLcommand As SqlCommand SQLconnect.ConnectionString = ConString SQLconnect.Open() 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() adapter1.Fill(dt) SQLconnect.Close() + + ' 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 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 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 End Try 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 - Dim SQLconnect As New SqlClient.SqlConnection - Dim SQLcommand As SqlClient.SqlCommand + Dim SQLconnect As New SqlConnection + Dim SQLcommand As SqlCommand SQLconnect.ConnectionString = SQLSERVERConnectionString SQLconnect.Open() SQLcommand = SQLconnect.CreateCommand 'Update Last Created Record in Foo - SQLcommand.CommandText = ExecuteCMD + SQLcommand.CommandText = sql_command SQLcommand.ExecuteNonQuery() SQLcommand.Dispose() SQLconnect.Close() + + ' Reset timeout counter when query was sucessful + DatabaseConnectionTimeout = False + Return True + Catch ex As SqlException + CatchDatabaseTimeout(ex, sql_command) + + Return Nothing Catch ex As Exception 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 ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True) - ClassLogger.Add("#SQL: " & ExecuteCMD, False) + ClassLogger.Add("#SQL: " & sql_command, False) Return False End Try 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 Try - Dim SQLconnect As New SqlClient.SqlConnection - Dim SQLcommand As SqlClient.SqlCommand + Dim SQLconnect As New SqlConnection + Dim SQLcommand As SqlCommand SQLconnect.ConnectionString = ConString SQLconnect.Open() SQLcommand = SQLconnect.CreateCommand 'Update Last Created Record in Foo - SQLcommand.CommandText = cmdscalar + SQLcommand.CommandText = sql_command result = SQLcommand.ExecuteScalar() SQLcommand.Dispose() SQLconnect.Close() + + ' Reset timeout counter when query was sucessful + DatabaseConnectionTimeout = False + Return result + Catch ex As SqlException + CatchDatabaseTimeout(ex, sql_command) + + Return Nothing Catch ex As Exception 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 ClassLogger.Add("Unexpected error in Execute_Scalar: " & ex.Message, True) - ClassLogger.Add("#SQL: " & cmdscalar, False) + ClassLogger.Add("#SQL: " & sql_command, False) Return Nothing End Try End Function @@ -175,4 +212,16 @@ Public Class ClassDatabase Return Nothing End Try 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 diff --git a/Global_Indexer/ClassFolderWatcher.vb b/Global_Indexer/ClassFolderWatcher.vb index 2e864f6..280c8d7 100644 --- a/Global_Indexer/ClassFolderWatcher.vb +++ b/Global_Indexer/ClassFolderWatcher.vb @@ -151,6 +151,11 @@ Public Class ClassFolderWatcher End If End Function 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 For Each row As DataRow In DTEXCLUDE_FILES.Rows Dim content As String = row.Item(0).ToString.ToLower @@ -158,7 +163,7 @@ Public Class ClassFolderWatcher Exit Sub End If Next - + Dim handleType As String If e.FullPath.ToLower.EndsWith(".msg") Then handleType = "|FW_OUTLOOK_MESSAGE|" @@ -172,11 +177,11 @@ Public Class ClassFolderWatcher Else ClassLogger.Add(">> Folderwatcher: File already exists:" & e.FullPath, False) End If - + Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created") End Try End Sub - + End Class diff --git a/Global_Indexer/frmStart.vb b/Global_Indexer/frmStart.vb index 7f5d04b..a8d5bd5 100644 --- a/Global_Indexer/frmStart.vb +++ b/Global_Indexer/frmStart.vb @@ -347,7 +347,7 @@ Public Class frmStart End If End If - + Catch ex As Exception MsgBox("Unexpected Error in Abort Indexing: " & vbNewLine & ex.Message, MsgBoxStyle.Critical) @@ -424,7 +424,7 @@ Public Class frmStart Try - + 'Dim sql = sql_UserID 'Dim splash As New frmSplash() @@ -669,11 +669,18 @@ Public Class frmStart End Sub 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 If FW_started = True Or FWSCAN_started = True Then '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 & "')" DTACTUAL_FILES = ClassDatabase.Return_Datatable(sql, True) + If DTACTUAL_FILES.Rows.Count > 0 Then ABORT_INDEXING = False ' Dim fil As String