MySQL Forums
Forum List  »  General

Duplicate Key error when setting a DateTime field(C++/CLI)
Posted by: Matt Brown
Date: May 26, 2023 02:24PM

At the bottom is the upper part of the error message I can getting. It essentially repeats this error with different files pointing back to the same line in the Notification DAO. I'm all sorts of "Wait, what?" on this. Is this somehow pointing to a line as having the error although the actual error lies elsewhere?

The line in question is this:

DateTime^ dt = DateTime::Now;

In the following function.
bool NotificationDAO::isPrimaryServerActive(){
	MySqlConnection ^conn;
	MySqlDataReader ^reader;

	try{
		conn = DatabaseWrapper::getConnection();
		conn->Open();

		DateTime^ dt = DateTime::Now;
		dt->AddMinutes(ACTIVE_CHECK);
		//Check to see if the primary has updated in the last 15 minutes. If not then take over.
		String ^sql = "select instanceId, aliveCheck, serverStatus from servers where serverStatus = 'PRIMARY' " +
			"and aliveCheck < addDate(now(), interval -15 MINUTE)";

		MySqlCommand ^cmd = gcnew MySqlCommand(sql, conn);
		cmd->Parameters->AddWithValue("@timedelay", dt);
		reader = cmd->ExecuteReader();
		if (reader->HasRows){
			return false;
		}
		else{
			return true;
		}
	}
	catch (MySqlException^ e){
		log->logError("NotificationDAO::isPrimaryServerActive", "find failed in NotificationDAO::isPrimaryServerActive(): " + e->Message, e->StackTrace->ToString());

	}
	finally{
		if (conn != nullptr)
		{
			conn->Close();
		}
	}
	return true;
}

Error starts here:
Unhandled Exception:Unhandled Exception:Unhandled Exception:   System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at MySql.Data.MySqlClient.MySqlPoolManager.<GetPoolAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__91.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at ENSAssembly.NotificationDAO.isPrimaryServerActive() in C:\apps\VSFiles\Projects\ensservice\ENSAssembly\DAO\NotificationDAO.cpp:line 1139
   at CmisChecker.threadProc() in C:\apps\VSFiles\Projects\ensservice\ENSService\CmisChecker.cpp:line 51
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Options: ReplyQuote


Subject
Written By
Posted
Duplicate Key error when setting a DateTime field(C++/CLI)
May 26, 2023 02:24PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.