Jump to content

Transactions and table locking


RobMs

Recommended Posts

Hey all,

 

I know PDO transactions are ACID. However does this mean that others cannot read/write to the database during a transaction? Or should I use table locking with transactions for this?

 

What I need to do is: be able to rollback if one of the transactions does not execute correctly and also prevent others reading/writing to the database until the transaction is commited. So should I use transactions and table locking? or will transactions do on its own?

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/197336-transactions-and-table-locking/
Share on other sites

PDO transactions are ACID compliant, if and only if the database engine you're using is ACID compliant (in MySQL's case it's InnoDB as of now).

If it's ACID, it takes care of all the locking by itself, so you do not need to do anything apart from starting a transaction and commiting/rolling back it.

Great thanks!

 

Is it possible to rollback a statement in a function?

I seem to be having difficulties, I purposely made the statement erroneous, but it still writes the first statements (Im using InnoDB so that is not the problem) I can roll back errors in the non function statement (the function is a new addition).

 

example

 

try:

transaction begin

sql statement

sql statement

sql statement

function(thisfunctionrunssqlstatement)

 

commit transaction

 

catch:

exception

rollback transaction

 

 

Any help much appreciated, thanks

I have never actually used PDO (I stick to ext/mysqli), so I am not sure, but there should be no problems with that (as long as you do it within one connection).

Remember that there are some SQL statements, that do implicit commit:

http://dev.mysql.com/doc/refman/5.1/en/implicit-commit.html

Thanks again for a reply, but I tried that and it still didnt work. Here is example code:

try {
	$db1 = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE.'', DB_USERNAME, DB_PASSWORD);
	$db1->beginTransaction();

	[sql STATEMENT]
	[sql STATEMENT]
	[sql STATEMENT]
	[sql STATEMENT]

	$variable = "SOME TEXT";
	action($db1, $variable);				
	$db1->commit();			
	$db1 = null;	
}
catch(PDOException $ex)
{
	$db1->rollBack();
	$db1 = null;
	echo $ex->getMessage();
}

/*function in another file which is included at the beginning*/

function action(PDO $db1, $variable){
	$sql = $db1->exec("SOME STATEMENT BEING DONE WRONG ON PURPOSE using $variable");		
}

 

I tried using global $db1 too in the function, still doesnt work.

To test it I am using an insert statement that tries to insert an id into the database where the id conflicts with an existing primary key. But of course it doesn't rollback previous statements, it doesnt seem to catch it as an error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.