We have just migrated our messagebus implementation to a new SQL Server 2014 instance (we have the same language and collation on old and new) and we seem to be getting the following error repeatedly in our logs:
12:24:06.0039|T12|M|Error|SQLMT_sql://MessageBus/Q_SlatePrintManagerHub|Error deleting processed messages: System.Data.SqlClient.SqlException (0x80131904): The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NGinnBPM.MessageBus.Impl.SqlMessageTransport2.CleanupProcessedMessages()
We are in the UK ('dd/mm/yy') and it is 28/10/2014 today.
Looking at the code, the following SQL statement is used (NamedQueries.sqlserver.json):
"delete top(10000) {0} with(READPAST) where retry_time <= @lmt and subqueue='X'"
(where @lmt is set to DateTime.Now - MessageRetentionPeriod, i.e. 12 hours ago)
I can replicate this error by running the following against the DB:
declare @lmt varchar(50)
set @lmt = '28/10/2014 01:05:48'
select top(1000) * from Q_Mybus with(READPAST) where retry_time <= @lmt and subqueue='X'
This is because the value of DateTime.Now is dd/MM/yy and SQL server expects MM/dd/yy.
The only way I can think to fix this is to change CommonQueueOps.CleanupProcessedMessages line 300 to:
_sql.AddParameter(cmd, "lmt", lmt.ToString("dd MMM yy HH:mm:ss"));
Is there anything else we can do?
Thanks