Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
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");
"very quickly" means two http request at same time
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