Synchronet v3.19b-Win32 (install) has been released (Jan-2022).

You can donate to the Synchronet project using PayPal.

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
service:jsondb [2022/11/28 22:18] – Fixed links to other pages on the wiki Nightfoxservice:jsondb [2022/12/08 14:01] (current) – [Best Practices] Nightfox
Line 61: Line 61:
  
 ===== Best Practices ===== ===== Best Practices =====
-When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it would be a good practice to lock that location for writing, read the data, write your changesthen unlock it.  There are a couple of functions, lock() and unlock() that you can use to do this:+When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it is a good practice to lock that location for writing when you're writing an update.  The idea is to prevent two or more systems from writing data to the same place at the same timewhere one write might overwrite another write, or perhaps another system reading data would get the wrong copy of the data just as it has been updatedetc.\\ 
 +\\ 
 +Note that the write function has a lock parameter: 
 +  JSONCLient.write(scope, data, location, lock) 
 +If you specify the lock parameter (such as 2, the write lock), it will lock, write, and unlock all at once.  For instance: 
 +  var JSON_DB_LOCK_WRITE = 2; 
 +  jsonClient.write("scores", "player1.TestGame", scoresData, JSON_DB_LOCK_WRITE); 
 +\\ 
 +There are also a couple of functions, lock() and unlock() that you can use for locking and unlocking:
   jsonClient.lock(scope,location,lock);   jsonClient.lock(scope,location,lock);
   jsonClient.unlock(scope,location);   jsonClient.unlock(scope,location);
 +When you use those, then you would //not// specify the lock parameter for write().  If you lock before you write, you know that you can read after that and whatever value you get back will not have changed by some other system.\\ 
 +\\
 For instance: For instance:
   var JSON_DB_LOCK_READ = 1;   var JSON_DB_LOCK_READ = 1;
Line 70: Line 79:
   var jsonClient = new JSONClient("servername", 10088);   var jsonClient = new JSONClient("servername", 10088);
   jsonClient.lock("scores", "player1.TestGame", JSON_DB_LOCK_WRITE);   jsonClient.lock("scores", "player1.TestGame", JSON_DB_LOCK_WRITE);
-  var scoresData = jsonClient.read("scores", "player1.TestGame", JSON_DB_LOCK_READ);+  var scoresData = jsonClient.read("scores", "player1.TestGame");
   // Change scoresData ...   // Change scoresData ...
-  jsonClient.write("scores", "player1.TestGame", scoresData, JSON_DB_LOCK_WRITE);+  jsonClient.write("scores", "player1.TestGame", scoresData);
   jsonClient.unlock("scores", "player1.TestGame");   jsonClient.unlock("scores", "player1.TestGame");
      
 +Note that you need to either specify the lock parameter for write() OR call lock() & unlock() without specifying a lock parameter for write().  If you both call lock() with 2 (write lock) AND specify 2 for the lock parameter for write(), then it will not update the data on the server because you'd have 2 locks interfering with each other.
 ===== Special files: commands.js and service.js ===== ===== Special files: commands.js and service.js =====
 There are a couple of additional JavaScript files you can create for your JSON database:\\ There are a couple of additional JavaScript files you can create for your JSON database:\\