Execute Non-Database Operations Within a Transaction
Learn how to execute non-database operations within a transaction.
We'll cover the following
Expanding transactions for non-database operations
With this knowledge, there is an opportunity to expand transactions to include non-database operations. Imagine that our application uses an external search engine such as Elasticsearch. Whenever we change the database, we have to update our search engine. However, it’s essential to keep the database and the search engine in sync.
- if the database changes fail, we don’t want to update the search engine.
- if the search engine update fails, we want to roll back the changes to the database.
Our MusicDB
application has a MusicDB.SearchEngine
module that handles search engine updates via its update
function to explore this scenario. This is just a placeholder module—our sample application doesn’t include a real search engine, so the module’s functions simulate the behavior.
To update the search engine along with the changes to the database, we call the appropriate functions from within the transaction by doing the following:
Get hands-on with 1400+ tech skills courses.