Article ID: 240317 - View products that this article applies to.
This article was previously published under Q240317
Because the Microsoft Jet database engine has a read cache and lazy writes, you can get duplicate values in your custom counter field if two applications add records in less time than it takes for the cache to refresh and the lazy-write mechanism to flush to disk. This article presents a method that takes these factors into account.
The Microsoft Jet database engine provides a Counter (AutoIncrement) field. Starting with Jet 4.0, you can specify a starting value and an increment value. This solves most of the need for using a custom counter that users of earlier versions of Jet had. However, if your counter is a text value or does not increase in a fixed amount, the steps outlined in this article may benefit your application.
Microsoft Jet has a read-cache that is updated every PageTimeout milliseconds (default is 5000ms = 5 seconds). It also has a lazy-write mechanism that operates on a separate thread to main processing and thus writes changes to disk asynchronously. These two mechanisms help boost performance, but in certain situations that require high concurrency, they may create problems.
The Microsoft Jet 4.0 OLEDB provider and the Microsoft Jet and Replication Objects type library provide two methods to ensure that your application has current data:
JetEngine object's RefreshCache MethodThere is a separate read-cache for each connection object. Calling the RefreshCache method of the JetEngine object, as exposed by the Microsoft Jet and Replication Objects type library, immediately refreshes the read-cache for the Connection objects passed as a method argument. The read-cache for all other Connection objects in the application are not affected.
"Jet OLEDB:Transaction Commit Mode" Connection PropertyIn Microsoft Jet 2.x and earlier, all writes were immediately committed. With Win32 and multi-threading, Microsoft Jet introduced a lazy-write mechanism. Setting this property to a value of 1 causes all transaction commits to be written immediately to disk for that Connection object. All other Connection objects are unaffected.
These methods are preferable to modifying registry values to get the same effect because you can precisely control where you need this value and they are more deterministic in operation. Global registry programs will adversely affect engine performance in other areas and in other applications.
EXAMPLEThe following example provides a function for generating custom counter numbers and handling the concurrency and locking issues that result from the process. It involves the use of a second table to store the next available key value. This is used for performance reasons and also to avoid adversely affecting users who would just need to read data.
The main function is NextKeyValue. It accepts three arguments:
When you set the initial value in the table, this is the first value returned by the function.
The error handling is designed to handle locking problems opening the table. If the locks time-out, the function returns -1 as the next key value. If any other error occurs, the function raises a run-time error that the main application will need to trap.
Because most people leave their registry settings untouched, Microsoft Jet will usually have a 100ms delay between lock-retries. If all instances of Jet have the same delay, this could result in a race situation and cause your application to time-out more than is necessary. The NextKeyValue function sets the lock retry to a random interval between 60 and 150 milliseconds to reduce the chance of a race condition occurring. The test application is responsible for using the RANDOM statement to seed the random number generator.
The test application adds 100 records to the table. It does not implement any but the most basic error handling to handle lock timeouts between the program and others like it.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
For more information about Jet and ADO, please see:
Migrating from DAO to ADO and Using ADO with the Jet ProviderFor information on implementing a mutli-user custom counter using Microsoft Data Access Objects (DAO):
(http://support.microsoft.com/kb/191253/EN-US/ )How To Implement Multi-user Custom Counters in DAO 3.5
Article ID: 240317 - Last Review: November 23, 2006 - Revision: 3.4