I am running unit tests and when I try to insert data in the database and getting it right after, I don’t get anything (I have tried with DataAdapter and DataReader).
However when I put a 3 seconds sleep (even with 1 second it doesn’t work…) between the insert and the select I get the result.
In SQL Server Profiler I can see the execution, the insert is well done and is completed about 10 miliseconds before the select begins.
I can’t find out where this comes
The code looks like this :
Insert method
SqlCommand command = new SqlCommand(sqlTemplate);
command.Parameters.Add(Sql4oConstants.Sql4oIdParameterName, SqlDbType.UniqueIdentifier).Value = id;
command.Parameters.Add(Sql4oConstants.Sql4oTimestampParamterName, SqlDbType.DateTime).Value = DateTime.Now;
command.CommandTimeout = dataSourceDescription.CommandTimeout;
DatabaseManager.ExecuteNonQuery(dataSourceDescription.ConnectionString, command);
Get method
public static void Fill(string connectionString, DataTable table, SqlCommand command)
{
try
{
LogStorageWriter.WriteLogEntry(log, EStorageLevelLog.Debug, string.Format("Execute query: {0}", command.CommandText));
using (SqlConnection conn = new SqlConnection(connectionString))
{
command.Connection = conn;
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
adapter.Fill(table);
}
}
}
catch (InvalidOperationException e)
{
LogStorageWriter.WriteLogEntry(log, EStorageLevelLog.Error, string.Format("Exception : {0}", e.ToString()));
}
}
I solved it.
In fact, it was because my request used a CONTAINS. I then discovered that using a CONTAINS calls the SQL Server Indexer to fetch data. But the engine does not index data instantly.
That’s why I had to wait 2 or 3 seconds to get my data back.