Is a transaction even running on SELECT?

I read in docs :

... because transactions begin when the cursor executes the query, but ends when COMMIT or ROLLBACK are executed using the Connection Object.

import MySQLdb

db = MySQLdb.connect(user="root", db="test")
c = db.cursor()
c.execute("SELECT * FROM books")
print c.fetchall()

I suspect that MySQLdb starts a transaction even on queries that do not change data (e.g. SELECT), because it is difficult to know if the query only reads the data and does not write them.

  • It's true?
  • If so, does that mean what should I do cursor.commit()after each query to make sure the table is not locked?
  • Other questions I don't know about?

thank

+5
source share
2 answers

, SELECT , .

, - :

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
SELECT * FROM books ;
COMMIT ;

:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;

, , , COMMIT. .

SELECT * FROM books ;, , a SQL statement, COMMIT ; "". , COMMIT

+3
+1

All Articles