Note:

  1. This article only discusses the difference between Redis and MySQL transactions, and does not represent the unified no-SQL and relational SQL;
  2. In MySQL only useInnodbOnly the database or table of the database engine supports transactions;

Transaction command

MySQL:

  • BEGIN: Explicitly starts a transaction;
  • COMMIT: Commits the transaction, making all changes to the database permanent;
  • ROLLBACK: end the user’s transaction and undo all uncommitted changes that are being made.

Redis:

  • MULTI: marks the start of a transaction;
  • EXEC: a queue of commands to perform a transaction;
  • DISCARD: Terminates a transaction and clears the Commands queue;

The Redis transaction commands are similar to BEGAIN,COMMIT,ROLLBACK. But clearly, they are fundamentally different.


The default state

MySQL:

  • MySQL starts a transaction by default, and the default setting is auto COMMIT, that is, every time a SQL is successfully executed, a transaction is committed immediately. So you can’t Rollback.

Redis:

  • Redis does not enable transactions by default, meaning that the command executes immediately without queuing. Rollback not supported (see: Redis Pit: Understanding Redis transactions)

use

MySQL: Contains two types

  1. Use BEGIN, ROLLBACK, COMMIT to explicitly start and control a new Transaction.
  2. Run the SET AUTOCOMMIT=0 command to disable automatic commit for the current session and control the transaction enabled by default.

Redis:

  1. Open and control a Transaction explicitly with MULTI, EXEC, DISCARD (note: there is no emphasis on “new” as transactions are not opened by default).

Realize the principle of

It is easy to understand that the fundamental reason for the difference between Redis and MySQL transactions is the implementation of different ways.

MySQL:

  • MySQL implements transactions based onThe UNDO/REDO log.
  • The UNDO logrecordBefore the changeState,ROLLBACKBased on UNDO log;
  • REDO logrecordThe modifiedThe status of theCOMMITBased on REDO log implementation;
  • In MySQL, whether transactions are enabled or not, SQL is executed immediately and results are returned. Only * *The transaction openAfter * *Status after executionIt’s just recorded inREDO log, the implementation ofCOMMITAfter that, the data will beWritten to disk.
int insertSelective = serviceOrderMapper.insertSelective(s);
Copy the code

So, insertSelective will be assigned immediately (whether the transaction is opened or not, only the result or not written to disk) :

InsertSelective = number of rows affected;Copy the code

Redis:

  • Redis implements transactions based onCOMMANDS the queue.
  • If the transaction is not enabled, the command is executed immediately and returns the result, which is written directly to disk.
  • If the transaction is opened,commandInstead of being executed immediately, it is queued and returnedQueue status(depending on the client implementation (for example, spring-data-redis) itself). callEXCEWill performCOMMANDS the queue.
boolean a = redisTemplate.opsForZSet().add("generalService",orderId,System.currentTimeMillis());
Copy the code

The above code,

  • If there is no open transactions, operations are executed immediately, will be immediately a assignment (true | false).
  • If open transaction, the operation is not immediately, will return NULL values, and a type is a Boolean, so will throw an exception: Java. Lang. NullPointerException