Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql: select a line inserted inside the same transaction before commit

In MySql using InnoDB, in the context of one transaction, are the inserts supposed to be visible by the following selects?

Example :

 $connect = new PDO('mysql:host='.  getConfig()->get('DB_HOST').';dbname='.  getConfig()-   >get('DB_NAME'), getConfig()->get('DB_USER'), getConfig()->get('DB_PASSWORD'), array(PDO::ATTR_PERSISTENT => true));
 $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $connect->beginTransaction();

 $sql = 'INSERT INTO t_table (label) VALUES ("test") WHERE id = "1"';
 $query = $connect->prepare($sql);
 $query ->execute();

 $sql2='SELECT * FROM t_table';
 $query2=$connect->prepare($sql2);
 $query2->execute();
 $result = $query2->fetch();

 $connect->commit();

In this case, should 'test' be in $result? if not, how could I make it do so?

Precision: the column 'label' is not the primary key but has an index.

like image 605
TrtG Avatar asked Oct 07 '13 14:10

TrtG


People also ask

What does the for update do on a select statement when a transaction has been started?

SELECT FOR UPDATE is a SQL command that's useful in the context of transactional workloads. It allows you to “lock” the rows returned by a SELECT query until the entire transaction that query is part of has been committed.

Why ROLLBACK is not working in MySQL?

This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. So if you need to do transactions yourself, you must turn off the autocommit mode by AUTOCOMMIT = 0 . Refer this link for more info. Save this answer.

What is the default behavior in MySQL if a command within an ongoing transaction yields an error?

By default, MySQL starts the session for each new connection with autocommit enabled, so MySQL does a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error.

How do I turn off autocommit in MySQL?

To disable autocommit mode explicitly, use the following statement: SET autocommit=0; After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables (such as those for InnoDB or NDB ) are not made permanent immediately.


1 Answers

Yes, 'test' must be in $result.All operations in a single transaction are visible to each other.

like image 105
user2855008 Avatar answered Oct 25 '22 23:10

user2855008