Service Authoring
This section recommends the best practices for the database connections to remain available in certain circumstances, but only if the services follow some general guidelines. These best practices include:
Performing query operations before model changes
Minimizing long-running work done after model changes
Performing Long Running Work When No Connection is in Use
Once a connection is retrieved for performing model operations, it cannot be released until the service terminates. Performing query operations before model operations, allows a connection to be retrieved for querying and then later be released while other work is being completed. If there is long-running work that impacts the model, try to perform that work first when there is no connection in use.
varthing = Things["SomeThing"];

// Instead of
thing.AddPropertyDefinition();// connection acquired
varhistory = thing.QueryPropertyHistory();
vardata = makeSomeExternalCalls(history);// connection held here
varvalue = calculateValue(data);// and here
thing.SetPropertyValue(value);
// Do this
varhistory = thing.QueryPropertyHistory();// connection acquired & released
vardata = makeSomeExternalCalls(history);// no connection
varvalue = calculateValue(data);// nor here
thing.AddPropertyDefinition();// connection acquired
thing.SetPropertyValue(value);
Use Asynchronous Services
If it is not possible to perform a long-running task after the model hasbeen changed, using asynchronous services can ease the connection competition.Asynchronous services may not be viable in some cases, as they cannot returna value directly, but they can be helpful for efficient resource usage.
varthing = Things["SomeThing"];

thing.AddPropertyDefinition();// connection acquired
asyncDoHttpRequestAndUpdateProperty(property);// request handled in another thread
// connection released (before async call completes)
What are Model Operations?
We refer to Model Operations' instead of ‘database operations’ because not allactions that deal with the database are performed synchronously and immediatelyusea connection.
Model operations are those that affect the system entities themselves, asopposed to their data. Examples include:
Property definition changes: Adding, removing, changing base-type, etc
Configuration changes
A number of database operations are performed asynchronously and are potentiallyarranged in groups for efficiency. These are generally data operations:
Updating a property value (even if persistent)
Adding an entry to a DataTable, Stream, or ValueStream
Was this helpful?