Using Database Slots to Dynamically Enable/Disable Graph or Cache Extensions in Acumatica
In Acumatica development, you may encounter scenarios where you need to dynamically enable or disable features based on a custom setup screen. This approach can be particularly useful when you have system configurations or toggles that control the activation of specific extensions. One way to achieve this efficiently is through the use of database slots, which help avoid performance issues and deadlocks.
Common Mistake: Instantiating a Graph in the IsActive Method
A common mistake is to instantiate a graph directly inside the IsActive method. This can lead to deadlocks and system inefficiencies. For example:
In this bad example, creating a graph instance inside the IsActive method introduces the risk of deadlocks, especially when the system tries to create multiple graph instances in complex scenarios. To avoid this, we should instead cache the necessary data using database slots.
Correct Approach: Using Database Slots
Instead of creating graph instances in the IsActive method, you can use database slots to cache the necessary configuration data and access it dynamically. Database slots allow you to cache data on the server and retrieve it without repeatedly querying the database.
What Are Database Slots?
● GetSlot: Retrieves cached data for a specific key. If the data isn't already cached, it fetches the data, stores it in memory, and returns it.
● ResetSlots: Clears the cached data when changes occur, ensuring that the cache is refreshed with up-to-date information.
Example: Enabling/Disabling Features Based on Custom Setup Screen
In this example, we show how you can use a database slot to enable or disable graph and cache extensions based on a custom setup screen.
Step 1: Setup DAC
Here’s some version of a DAC representing a custom setup screen:
In this DAC, EnableCustomFeature controls whether a custom feature is enabled or disabled.
Step 2: Slot for Caching Setup Data
Now, we define a slot class to cache this data:
This class uses the slot mechanism to cache whether the feature is enabled. The data is loaded once and cached for future requests, avoiding repeated database queries.
Step 3: Dynamic Enabling/Disabling of Graph and Cache Extensions
Here’s how you can use this cached data to enable or disable a graph extension dynamically:
This code dynamically controls the graph extension. If the feature is enabled in the custom setup screen, the graph extension becomes active.
Similarly, you can control cache extensions in the same manner:
Here, the cache extension will only be applied when the feature is enabled.
Detailed Explanation of Slot Methods
● PXDatabase.GetSlot<T>(key, tables): Retrieves the cached data object associated with the given key. If the cache is not initialized, it fetches data from the database, caches it under the key, and returns it. The list of tables (e.g., typeof(MyCustomSetup)) ensures that the cache is invalidated when any table in the list is modified.
● PXDatabase.ResetSlots(): Clears all cached slots, forcing the system to retrieve fresh data when the slot is next accessed. This is essential after database updates to ensure that the cache remains current.
Conclusion
By using database slots, you can efficiently enable or disable features based on custom setup screen configurations without creating new graph instances in the IsActive method. This approach avoids deadlocks, improves performance, and ensures data consistency across the system.
To summarize:
● Use PXDatabase.GetSlot to cache and retrieve configuration data.
● Use PXDatabase.ResetSlots to refresh the cache after database changes.
● Dynamically control graph and cache extensions based on cached data to toggle features efficiently.