简体   繁体   中英

How are SQL Transactions and JavaScript Promises related?

I recently learned about Promises , which seemed similar to SQL Transactions.

  • Transactions are all or nothing
  • Promises are sequential callbacks

    Are they different implementations of the same concept to handle asynchronous events?

Promises are a general concept dealing with asynchronousness. Transactions are a means to guarantee atomicity when updating multiple records.

They might appear together in some library but they are not related. You could see them together if you have a transactional database that has an asynchronous API.

Intent:

A promise provides a high-level interface for synchronizing asynchronous callbacks

A transaction provides a high-level interface for executing dependent SQL statements

Use Case

When an application needs a future value, it constructs the promise on demand and waits until it has returned.

When a database needs to peform a series of data manipulations, a transaction is constructed

Error Handling

If a promise raises an exception, the exception is returned and the future is suspended

If a transaction raises an exception, a rollback is executed on the pending transaction

Mutation

If there are no exceptions, the computed value is returned in the future object.

If there are no exceptions, the final result set of the transaction is committed

Side-Effects

In a transaction block, you can write queries to more than one database, but you must commit or roll back a transaction to one database before writing a query to another.

In a future, you can assign values to one or more global or non-local variables; there is no commit or rollback

Tainting

A read can occur during the execution of concurrent SQL transactions. The possible read actions include:

  • dirty read, in which a second SQL transaction reads a row before the first SQL transaction executes a COMMIT
  • non-repeatable read, in which a SQL transaction reads a row and then a second SQL transaction modifies or deletes the row and executes a COMMIT;
  • phantom, in which a SQL transaction reads rows that meet search criteria, a second SQL transaction then generates at least one row that meets the first transaction's search criteria, and then the first transaction repeats the search, resulting in a different result set.

An assignment or serialization can occur during the execution of a future without warning

State Management

Setting savepoints lets you roll back portions of a transaction. For example, if your transaction includes an insert, an update, and a delete, and you set a savepoint after the update, you can roll back the transaction to exclude the delete.

A future has no savepoints

References

cftransaction
Understanding the Available Transaction Isolation Levels
Asynchronous Programming in JavaScript with Promises
How Transactional Replication Works
Transaction Process Synchronization
Optimistic Algorithm
Concurrency Control Algorithms
Configuring Serializable Isolation
Improving concurrency with new registry variables
Operating Systems Lecture Notes: Allocation and Deadlock
SQLite File I/O Specification
Broken promises–C++0x futures
concurrent.futures — Launching parallel tasks
Javascript Is Awesome: I don't want promises
JavaScript Asynchronous Architectures: Events vs. Promises
AS3 Promises - a guide and example
Smalltalk Concurrency, Playing With Futures
promise-streams
System.Threading.TTask.Future
Promises Part 5: The Lego Problem
Callbacks vs Events
Events and Callbacks
Events vs. Callbacks – when to use what
coroutines,callbacks,message-queues - Tcl
Introduction to ES6 Promises – The Four Functions You Need To Avoid Callback Hell
All about ES6 Generators
Chaining Difference in ES6 Promisies and PEP3148 Futures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM