locking table issues (Again)
locking table issues (Again)
Hello,
I have 2 queries, one TMyQuery and one TMyScript
MyQuery.SQL.Text := 'LOCK TABLES history WRITE;'
MyScript.SQL.Add('INSERT history SET.... blah blah');
MyScript.SQL.Add('SET @last_id=LAST_INSERT_ID();';
MyScript.SQL.Add('INSERT users SET blah blah blah';
MyQuery.Execute;
MyScript.Execute;
MyQuery.SQL.Text := 'UNLOCK TABLES;';
this code returns me "TABLE 'users' was not locked with LOCK TABLES';
why I have to lock a table that I dont want to lock? Any ideas?
Thanks
I have 2 queries, one TMyQuery and one TMyScript
MyQuery.SQL.Text := 'LOCK TABLES history WRITE;'
MyScript.SQL.Add('INSERT history SET.... blah blah');
MyScript.SQL.Add('SET @last_id=LAST_INSERT_ID();';
MyScript.SQL.Add('INSERT users SET blah blah blah';
MyQuery.Execute;
MyScript.Execute;
MyQuery.SQL.Text := 'UNLOCK TABLES;';
this code returns me "TABLE 'users' was not locked with LOCK TABLES';
why I have to lock a table that I dont want to lock? Any ideas?
Thanks
but I have another question,
if I have an application with one connection to mysql and I have 2 background threads adding records (INSERT) how can I prevent that I will have collisions between LAST_INSERT_ID of 2 threads? LOCK TABLE works also per connection, correct?
The solution is to have 2 connections?
if I have an application with one connection to mysql and I have 2 background threads adding records (INSERT) how can I prevent that I will have collisions between LAST_INSERT_ID of 2 threads? LOCK TABLE works also per connection, correct?
The solution is to have 2 connections?
-
- Posts: 451
- Joined: Wed 19 Jan 2005 09:59
> I will have collisions between LAST_INSERT_ID of 2 threads?
You should always use one MyConnection per thread, because using one MyConnection in different threads may cause errors. We mean that thread-safe components are components that can work in multithread application. You can read about it in corresponding topic of MyDAC FAQ.
> LOCK TABLE works also per connection, correct?
LOCK TABLE works for whole database. Other sessions will "know" that the table is locked. Read MySQL Reference for detailed information.
> The solution is to have 2 connections?
Yes.
You should always use one MyConnection per thread, because using one MyConnection in different threads may cause errors. We mean that thread-safe components are components that can work in multithread application. You can read about it in corresponding topic of MyDAC FAQ.
> LOCK TABLE works also per connection, correct?
LOCK TABLE works for whole database. Other sessions will "know" that the table is locked. Read MySQL Reference for detailed information.
> The solution is to have 2 connections?
Yes.