Ilya,
We changed the code to use transaction and the database still fails.
Also, the write cache for the hdd is disabled.
This time it failed after 10 hours.
The behaviour is the same as before, the only difference is that now I did not get a lot of pages not found before the program stopped working.
This is the last entry in the database log before it crashed:
Source : D:\MC\7654321\DomainModel\EventLog.sdf
Destination : n/a
Verifying - verMajor: 3, verMinor: 0, verBuild: 5300, verPoint: 0
Time - 2007-22-03 22:38:19.821
Selected page not found - idPage: 1, iPage: 3007
Source : D:\MC\7654321\DomainModel\EventLog.sdf
Destination : C:\DOCUME~1\husky\LOCALS~1\Temp\sql1.tmp
Repairing - verMajor: 3, verMinor: 0, verBuild: 5300, verPoint: 0
Time - 2007-22-03 22:38:20.190
Selected page not found - idPage: 1, iPage: 3007
Here is the code where we are accessing the database:
#region
Database Access
private void InitializeDatabase()
{
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " EventManager.Constructor Started");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// Determine if database file exists
bool databaseExists = File.Exists(_DatabaseFilePath);
if (_CreateEmptyDatabase || !databaseExists)
{
// Remove old database file if we are forcing creating of an empty database
if (databaseExists)
{
File.Delete(_DatabaseFilePath);
}
// Create an empty database if it does not exist yet
CreateEmptyDatabase();
OpenConnection();
CreateTables();
}
else
{
VerifyAndRepairDatabase();
OpenConnection();
}
PrepareInsertEventCommand();
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " EventManager.Constructor Completed in " + stopwatch.ElapsedMilliseconds + " msec");
}
private void VerifyAndRepairDatabase()
{
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " VerifyAndRepairDatabase Started");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (SqlCeEngine sqlCeEngine = new SqlCeEngine(_ConnectionString))
{
LogFile.Log("Event Log Database", "Verified Started");
if (sqlCeEngine.Verify())
{
stopwatch.Stop();
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " Database Verify Completed in " + stopwatch.ElapsedMilliseconds + " msec");
LogFile.Log("Event Log Database", "Verified OK");
}
else
{
LogFile.Log("Event Log Database", "Verified Failed");
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " Database Corrupted --> Repair in Progress");
sqlCeEngine.Repair(
null, RepairOption.DeleteCorruptedRows);
stopwatch.Stop();
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " Database Verify and Repair Completed in " + stopwatch.ElapsedMilliseconds + " msec");
LogFile.Log("Event Log Database", "Repair Complete");
}
}
}
private void CreateEmptyDatabase()
{
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " CreateEmptyDatabase Started");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (SqlCeEngine sqlCeEngine = new SqlCeEngine(_ConnectionString))
{
sqlCeEngine.CreateDatabase();
}
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " CreateEmptyDatabase Completed in " + stopwatch.ElapsedMilliseconds + " msec");
}
private void OpenConnection()
{
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " OpenConnection Started");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
_Connection =
new SqlCeConnection(_ConnectionString);
_Connection.Open();
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " OpenConnection Completed in " + stopwatch.ElapsedMilliseconds + " msec");
}
private void CreateTables()
{
const string sqlCreateTableCommand =
@"CREATE TABLE " + TableName + " " +
@"(" +
@"[TimeStamp] bigint IDENTITY(1,1) PRIMARY KEY," +
@"[Name] nvarchar(255)," +
@"[DateTime] datetime," +
@"[EventType] integer," +
@"[SourceText] nvarchar(255)," +
@"[MessageText] nvarchar(255)" +
@");";
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " CreateTables Started");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (SqlCeCommand sqlCommand = new SqlCeCommand(sqlCreateTableCommand, _Connection))
{
sqlCommand.ExecuteNonQuery();
}
Trace.WriteLine(DateTime.Now.ToLongTimeString() + " CreateTables Completed in " + stopwatch.ElapsedMilliseconds + " msec");
}
private void PrepareInsertEventCommand()
{
const string sqlInsertCommand =
@"INSERT INTO " + TableName + @" " +
@"([Name], [DateTime], [EventType], [SourceText], [MessageText]) " +
@"VALUES (@Name, @DateTime, @EventType, @SourceText, @MessageText)";
_InsertEventCommand = _Connection.CreateCommand();
_InsertEventCommand.CommandText = sqlInsertCommand;
_InsertEventCommand.Parameters.Add(
"@Name", SqlDbType.NVarChar, 255);
_InsertEventCommand.Parameters.Add(
"@DateTime", SqlDbType.DateTime, 8);
_InsertEventCommand.Parameters.Add(
"@EventType", SqlDbType.Int);
_InsertEventCommand.Parameters.Add(
"@SourceText", SqlDbType.NVarChar, 255);
_InsertEventCommand.Parameters.Add(
"@MessageText", SqlDbType.NVarChar, 255);
_InsertEventCommand.Prepare();
}
public void UpdateEventHistory(Event machineEvent)
{
// Start transaction
SqlCeTransaction transaction = _Connection.BeginTransaction();
_InsertEventCommand.Transaction = transaction;
try
{
// Update command parameters and write values to database
_InsertEventCommand.Parameters[0].Value = machineEvent.Name;
_InsertEventCommand.Parameters[1].Value = machineEvent.TimeStamp;
_InsertEventCommand.Parameters[2].Value = machineEvent.EventType;
_InsertEventCommand.Parameters[3].Value = machineEvent.SourceText;
_InsertEventCommand.Parameters[4].Value = machineEvent.MessageText;
_InsertEventCommand.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception exception)
{
transaction.Rollback();
_Connection.Close();
throw exception;
}
}
#endregion
Thanks.