cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

duplicated key in DataTable

cdu
1-Newbie
1-Newbie

duplicated key in DataTable

When I call AddDataTableEntry or AddOrUpdateDataTableEntry very quickly, two data row with same key will be added.

How to settle it?

 

Both serialNumber and alarmType are primary key. I have test unique primary key, but same error.

Code:

 

var query = {

  "filters": {

    "type": "AND",

    "filters": [

      {

        "fieldName": "serialNumber",

        "type": "EQ",

        "value": serialNumber

      },

      {

        "fieldName": "alarmType",

        "type": "EQ",

        "value": alarmType

      }

    ]

  }

};

 

 

var params = {

    query: query,

    maxItems: 1

};

 

 

var old_times = 0;

 

 

var cc = Things["AlarmTimesTable"].QueryDataTableEntries( params );

if ( cc !== undefined && cc.getRowCount() != 0 ) {

    old_times = cc.getRow(0).times;

    //var values = cc;//.getRow(0);

}

 

 

var values = Things["AlarmTimesTable"].CreateValues();

values.gateway = me.name;

values.updateTime = updateTime;

values.serialNumber = serialNumber;

values.alarmType = alarmType;

values.times = old_times + 1;

 

 

var params = {

    //source :  me.name,

    values : values,

};

 

 

Things["AlarmTimesTable"].AddOrUpdateDataTableEntry( params );

logger.error("out");

4 REPLIES 4
cdu
1-Newbie
1-Newbie
(To:cdu)

"very quickly" means two http request at same time

LaurentGermain
6-Contributor
(To:cdu)

i met the same issue, and the only way to fix it is to create a service that locate duplicate and suppress them.

Seems there is  a delay before a newly written entry can be read

Hi Laurent,

I noticed that 6.5 settle the issue, but the second http request will report error, i.e. one of data be dropped.

I think the better solution is multithreading-locking, and a delay after Add ( not Update ) like you said.

Do you know is there any mutex/critical_section/thread_lockig functions? or task queue for one thread ?

This issue should be resolved in the recent 6.0.4 and 6.5 releases.


When I had the issue in prior versions, I added this code to a script to de-sync any concurrent requests:

var jitter = 200 * Math.random();

var delayUntil = new Date().getTime() + jitter;

while(new Date().getTime() < delayUntil); // busy-wait

Top Tags