简体   繁体   中英

MySQL transactions implicit commit

I'm starting out with MySQL trnsactions and I have a doubt:

In the documentation it says:

Beginning a transaction causes any pending transaction to be committed. See Section 13.3.3, “Statements That Cause an Implicit Commit”, for more information.

I have more or less 5 users on the same web application ( It is a local application for testing ) and all of them share the same MySQL user to interact with the database.

My question is: If I use transactions in the code and two of them start a transaction ( because of inserting, updating or something ) Could it be that the transactions interfere with each other?

I see in the statements that cause an implicit commit Includes starting a transaction. Being a local application It's fast and hard to tell if there is something wrong going on there, every query turns out as expected but I still have the doubt.

With MySQl by default each connection has autocommit turned on. That is, each connection will commit each query immediately. For an InnoDb table each transaction is therefore atomic - it completes entirely and without interference.

For updates that require several operations you can use a transaction by using a START TRANSACTION query. Any outstanding transactions will be committed, but this won't be a problem because mostly they will have been committed anyway.

All the updates performed until a COMMIT query is received are guaranteed to be completed entirely and without interference or, in the case of a ROLLBACK , none are applied.

Other transations from other connections see a consistent view of the database while this is going on.

This property is ACID compliance (Atomicity, Consistency, Isolation, Durability) You should be fine with an InnoDB table.

Other table types may implement different levels of ACID compliance. If you have a need to use one you should check it carefully.

This is a much simplified veiw of transaction handling. There is more detail on the MySQL web site here and you can read about ACID compliance here

The implicit commit occurs within a session .

So for instance you start a transaction, do some updates and then forget to close the transaction and start a new one. Then the first transaction will implicitely committed.

However, other connections to the database will not be affected by that; they have their own transactions.

You say that 5 users use the same db user. That is okay. But in order to have them perform separate operations they should not use the same connection/session.

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