Caveat When Handling Persistent Class Instances
The instances property and the allInstances, firstInstance, and lastInstance methods of the Class class employ a database method that retrieves references to instances of a class (and optionally its subclasses) from the physical database.
You can use the allInstances, firstInstance, and lastInstance methods and the instances property in a production system if you have implemented a mechanism that ensures that there are no uncommitted creates or deletes of instances at the time of execution of the method or during use of the instances property.
The collection of instances from the database may not be consistent with updates made by existing uncommitted transactions that are still in a client node cache. For example, if overflow has occurred, the collection may contain instances that have been created, and deleted instances may be missing.
In addition, this type of access has no form of concurrency control that can guarantee a consistent view or "snapshot of instances" for the operation as a whole in a multiuser environment.
In delta database mode, the instances property and the allInstances, firstInstance, and lastInstance methods of the Class class are executed on both the root and delta database but the merged result set may not be the same as the result set obtained outside of delta mode.
The differences in behavior when using class extent methods in delta mode are as follows.
-
Any class extent method can return a reference to an object deleted in delta mode; for example, if the first instance of a class is deleted in delta mode, a reference to this object is still returned by the Class class firstInstance method.
-
The Class class countPersistentInstances method and calls to Class.instances.size include instances that were deleted in delta mode.
For these reasons, unless you have implemented a mechanism to block updates, as stated in the note earlier in this topic, the instances property and the allInstances, firstInstance, and lastInstance methods are not recommended for production use in a Jade application. Such use is appropriate only as a development diagnostic or testing aid.
To find the number of non-exclusive persistent instances of a class, use the countPersistentInstances method of the Class class in preference to the size method on the instances pseudo collection, as shown in the following code fragments.
Customer.instances.size;
Customer.countPersistentInstances; // significantly faster