I want to perform 4 or 5 statements as a transaction but I need to make sure that during this complete transaction no one else inserts or deletes records from a table named SomeTable.
So how can I lock MyTable at the beggining of the transaction so that during my transaction no one else can insert or delete anything in table SomeTable?
We currently use SQL Serv 2000 with an ERP application and VB applications.
Both system work with the same database & tables but the ERP application seems to put exclusive lock on all tables it needs during processes. So our VB applications are not able to read these data and we receive TIMEOUT error.
Anybody know how a SELECT statement can generate an exclusive lock on a table ? I always thought that SELECT's take out shared locks. Is this something to do with temporary tables generated by ORDER BY's and DISTINCT ? Rogue SQL below (from Site Server).
SELECT A.i_Dsid, A.i_Aid, A.vc_Val, A.i_Val, A.dt_Val, A.img_Val FROM Object_Attributes A, ( SELECT DISTINCT L.i_Dsid FROM Object_Lookup L , Object_Attributes OA2 (index = IND_vc_Aid) WHERE ((( L.i_ObjectClass = 9999 )) AND ( OA2.vc_Val LIKE ( '999999999.9999999%' ) AND OA2.i_Aid = 99)) AND (L.i_Container_Dsid = 99) AND ( OA2.i_Dsid = L.i_Dsid )) AS B WHERE B.i_Dsid = A.i_Dsid AND A.i_Aid NOT IN( 1, 2, 3, 4, 5 ) ORDER BY A.i_Dsid, A.i_Aid
Is it possible to place an exclusive row lock when running a SELECT query by using a lock hint (or otherwise).
Basically, when a select statement is run against a table I don't won't any other users to read that row until it has been updated - at some later stage.
Any suggestions on whether this is possible would be welcome.
I'm relatively new to SQL and I've come across something that doesn't seem quite right. When an insert becomes part of an transaction I notice an exclusive KEY lock in Enterprise Manager. The table in question was using a Clustered index but I changed that, dropped the table and brought it back in but I still get the lock which keeps all others out of the table. Is this the expected behavior or is there something I am missing? Could the size of the tabe affect things? This is a very small table currently. I'm using MSSQL 7 sp3.
Hi , I have some issues with deadlock.I am getting deadlock becuase of two select on same table. The sql server log is like this , Select statements should have always shared lock.I am not getting why its doing exclusive lock and creating deadlock.
5/6/2008 12:38 spid4s Unknown Deadlock encountered €¦. Printing deadlock information 5/6/2008 12:38 spid4s Unknown Wait-for graph 5/6/2008 12:38 spid4s Unknown Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content: 5/6/2008 12:38 spid4s Unknown Node:1 5/6/2008 12:38 spid4s Unknown KEY: 9:72057594050117632 (8d036f07c58f) CleanCnt:3 Mode Flags: 0×0 5/6/2008 12:38 spid4s Unknown Grant List 3: 5/6/2008 12:38 spid4s Unknown Owner:0×12E9F160 Mode: S Flg:0×0 Ref:1 Life:00000001 SPID:68 ECID:0 XactLockInfo: 0×353D1C54 5/6/2008 12:38 spid4s Unknown SPID: 68 ECID: 0 Statement Type: SELECT Line #: 4 5/6/2008 12:38 spid4s Unknown Input Buf: Language Event: (@actDefId nvarchar(36)@stateList varchar(1)@stateList1 varchar(1)@procRelObjType smallint@procRelObjIdList varchar(36)) 5/6/2008 12:38 spid4s Unknown Requested By: 5/6/2008 12:38 spid4s Unknown ResType:LockOwner Stype:€™OR€™Xdes: 0×2FBB67F0 Mode: X SPID:112 BatchID:0 ECID:0 TaskProxy0×0792E378) Value:0×38baa20 Cost0/11888) 5/6/2008 12:38 spid4s Unknown Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content: 5/6/2008 12:38 spid4s Unknown Node:2 5/6/2008 12:38 spid4s Unknown KEY: 9:72057594049986560 (6f02e1cd37c3) CleanCnt:3 Mode:X Flags: 0×0 5/6/2008 12:38 spid4s Unknown Wait List: 5/6/2008 12:38 spid4s Unknown Owner:0×12396EE0 Mode: S Flg:0×2 Ref:1 Life:00000000 SPID:90 ECID:0 XactLockInfo: 0×0AA8178C 5/6/2008 12:38 spid4s Unknown SPID: 90 ECID: 0 Statement Type: SELECT Line #: 4 5/6/2008 12:38 spid4s Unknown Input Buf: Language Event: (@actDefId nvarchar(36)@stateList varchar(1)@stateList1 varchar(1)@procRelObjType smallint@procRelObjIdList varchar(36)) 5/6/2008 12:38 spid4s Unknown Requested By: 5/6/2008 12:38 spid4s Unknown ResType:LockOwner Stype:€™OR€™Xdes: 0×353D1C30 Mode: S SPID:68 BatchID:0 ECID:0 TaskProxy0×13B3E378) Value:0×12e9e780 Cost0/6164) 5/6/2008 12:38 spid4s Unknown Log Viewer could not read information for this log entry. Cause: Data is Null. This method or property cannot be called on Null values.. Content:
If you have any idea regarding this please let me know ASAP.
Here is the situation i am stuck with, see the example first and below explained the problem:
-- 'SESSION A
create table foo (
id integer,
pid integer,
data varchar(10)
);
begin transaction
insert into foo values ( 1, 1, 'foo' )
insert into foo values ( 2, 1, 'bar' )
insert into foo values ( 3, 1, 'bozo' )
insert into foo values ( 4, 2, 'snafu' )
insert into foo values ( 5, 2, 'rimrom' )
insert into foo values ( 6, 2, 'blark' )
insert into foo values ( 7, 3, 'smeg' )
commit transaction
create index foo_id_idx on foo ( id )
create index foo_pid_idx on foo ( pid )
begin transaction
insert into foo values ( 9, 3, 'blamo' )
-- 'SESSION B
begin transaction
select id, data from foo with ( updlock, rowlock ) where id = 5;
-- Problem:
-- Uncommitted transaction in session A, with insert into table FOO, aquires lock on index foo_pid_idx which BLOCKS select with ( updlock, rowlock ) in session B.
-- Insert should aquire only exclusive rowlock. Why does insert block select with ( updlock, rowlock )?
I want to lock a table so others cannot lock it but able to read it inside transactions.
The coding I need is something like this: set implicit_transactions on begin transaction select * from table1 with (tablock, holdlock) update table2 set field1 = 'test' commit transaction commit transaction
I have tried the coding above, it won't prevent others from locking table1.
So, I changed the tablock to tablockx to prevent others from locking table1. But this will also prevent others from reading table1. So, how can I lock table1 so others cannot lock it but still able to read it?
Howdy, all. We have a (log-type) file that's constantly being writtento. We need to grab the latest rows out of this file, insert them intoa table, and then process them. We've found out the hard way that BCPlocks the file while it's inserting, so rows can't be written to thefile while the BCP is running.Our current workaround is to make a copy of the file (using ftp), butwe're running into other problems.I'm trying to find a better way to do this. We've narrowed down acouple.1) use TAIL or something similar to grab the records since we last ran.2) use BULK INSERT, provided it doesn't lock the file.3) get suggestions off of USENET. :)Any suggestions or comments?Thanks,Michael
Hi, I'm taking an Excel spreadsheet (that could have around 30k rows) and processing it in SSIS. I essentially have a flag in one of the spreadsheet cols that indicates whether the record is already in the database or not.
I'm splitting the data using a conditional split on this column and using a OLE DB Destination (Fast Load) to perform the inserts and a OLE DB Command to fire a stored procedure to perform any updates. Both the OLE DB Destination and the stored procedure are hitting the same table and the two operations could be executing at the same time as they both appear directly after the Conditional Split, so the OLE DB Destination is set NOT to lock the table.
This seemed to work OK until recently. I've just added 2 triggers onto the table in question which I don't want to fire 30,000 times during the import. As the OLE DB Destination is set to use Fast Load, it doesn't fire the triggers - cool. In the update stored procedure it disables the trigger before performing it's update and re-enables the trigger when finished. Currently this does mean that if you only had updates, the trigger could be enables/disabled 30,000 times. That sounds kinda bad, but I don't really know if this carries a large overhead or not?
If, when importing now you have both updates and inserts the whole process locks up. From looking at activity monitor, it seems as though the INSERT gets suspended.
Do I have a fundamental problem with how I've structured the Data Flow or am I just being really stupid in Enabling/Disabling a trigger that many times, which is probably causing the problem?
Hi all,Hope there is a quick fix for this:I am inserting data from one table to another on the same DB. Theinsert is pretty simple as in:insert into datatable(field1, field2, field3)select a1, a2, a3 from temptable...This inserts about 4 millions rows in one go. And since I had the'cannot obtain lock resources' problem, several methods were suggestedby some web sites:1) one to split the insert into smaller chunks (I have no idea how Ican spit a insert to insert only n records at a time..)2)to use waitfor - which I did but did not fix the error.3)use bulk insert (in t-sql) - I dont know how to do this?As I see I am simply trying to move data from one table to another(ofcourse lots of data) in SQL Server 2000 and I dont see one simplesolution to the locking problem.any ideas on how best I can do this will save my day!thanks all.
I am using conditional split Checking to see if a record exists and if so update else insert. But this cause database dead lock any one has suggestion?
Error: The Script returned a failure result. Task SCR REIL Data failed
OnError - Task SQL Insert Error Msg Error: A deadlock was detected while trying to lock variable "System::ErrorCode, System::ErrorDescription, System::ExecutionInstanceGUID, System::StartTime, User::FEED_ID, User::t_ProcessedFiles" for read access. A lock could not be acquired after 16 attempts and timed out. Error: The expression ""EXEC [dbo].[us_sp_Insert_STG_FEED_EVENT_LOG] @FEED_ID= " + (DT_WSTR,10) @[User::FEED_ID] + ", @FEED_EVENT_LOG_TYPE_ID = 3, @STARTED_ON = '"+(DT_WSTR,30)@[System::StartTime] +"', @ENDED_ON = NULL, @message = 'Package failed. ErrorCode: "+(DT_WSTR,10)@[System::ErrorCode]+" ErrorMsg: "+@[System::ErrorDescription]+"', @FILES_PROCESSED = '" + @[User::t_ProcessedFiles] + "', @PKG_EXECUTION_ID = '" + @[System::ExecutionInstanceGUID] + "'"" on property "SqlStatementSource" cannot be evaluated. Modify the expression to be valid.
Warning: The Execution method succeeded, but the number of errors raised (4) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
And how did I get 4 errors? - I only set my script task result to failure
Hi,all: This problem almost drives me crazy, hope I can get some hints from you guyz!!! Ok, here is the situation: I wanna only one users 2 modify the data(update) from my page each time, and if at the same time, there are some other users connecting my database through .aspx page, they can only browse the data until the first users finish updating. It seems I need to implement locking the database, but I am not sure how I am gonna do that using asp.net!!! Thanx in advance!
hi, i have an application that updates some records in sql tables, and i want to do a web application that updates records in the some database-table(sql) so, my question is how can i lock the row or table so i dont have concurrency problems.tnx in advance.
Hello Friends, I am having a VB application running for the SQL SERVER DB. The VB application is installed on the multiple of PCs in the network. Now when I am trying to fetch the same from all the different PCs simultaneously, its amazingly fast. But the issue comes when I am trying to update the same table (but different rows) from the different PCs simultaneously. The time taken is directly proportaional to the number of users. I am not getting what could be the problem? Can any one suggest me the approch? Is it some related to table / row / page locking? As all the connections are trying to update on the same table. I checked the isolation level. Its default, "READ COMMITTED". Kindly suggest...
Hi,If I run an insert statement from the query analyzer and then try toopen the table from enterprise manager then it takes long time to openthe table. But this problem dissapears when i put the statement insideBegin/End Transaction statement.Any idea why this is happening?Thank in advance.Taw.
Which lock type or isolation level should I use to be sure that no onewill read or write or do anything with the table I'm using?Code block should look something like this:lock tableread value from tablechange value to new_valueupdate table set value = new_valuerelease lockWhile I'm changing the value absolutly no one should be able to readfrom the table.
How can I see which table is locked up by some particular process? I know that I can view paricular spid from 'current activity'. But is there any way I can see which table is the center of problem? I really appreciate your help..
Hi, I am working on a project which need to produce a sequential certificate number, Everytime I need a new certificate number, I need to find out what is the max number in the database and then the new certificate number just max+1. But how can I block another transaction to check what is max certi. number while this transaction is in the middle of writing the new certificate number(max+1) into database . Does ADLockOptimistic work in this case? Here is the code: My database sql 2000.
cmdTemp.CommandText="Select max(certificateNumber) from product_table where certificateNumber<> 8888888" set cert_info=Server.CreateObject("ADODB.RecordSet") cert_info.Open cmdTemp, , AdOpenKeySet, adLockOptimistic If Not cert_info.EOF then
How can i give a table wise exclusive lock in MSSQL Server ?
I got the description but, How can I apply this ? The sql : LOCK TABLE <tablename> IN EXCLUSIVE MODE is not working.
Is there any query/method to do this ? Please help ...
thanks
About Exclusive locks -------------------- Exclusive (X) locks are used for data modification operations, such as UPDATE, INSERT, or DELETE.
Other transactions cannot read or modify data locked with an Exclusive (X) lock. If a Shared (S) exists, other transactions cannot acquire an Exclusive (X) lock. --------------------
I need to update a row but keep a lock on the table (so no one else can update it) while I do run some more code. In Oracle, it always locks whatever you update until you hit commit, but sql server works opposite. How do I tell it not to commit a statement, or how would I explicitly get a lock and then release it later?
I need confirmation from you SQL Server experts out there. Please let me know if the following works. Thanks!
This stored procedure gets a value and increments by 1, but while it does this, I want to lock the table so no other processes can read the same value between the UPDATE and SELECT (of course, this may only happen in a fraction of a second, but I anticipate that we will have thousands of concurrent users). I need to manually increment this column because an identity column is not appropriate in this case.
BEGIN TRANSACTION
UPDATE forum WITH (TABLOCKX) SET forum_last_used_msg_id = forum_last_used_msg_id + 1 WHERE forum_id = @forum_id
SELECT @new_id = forum_last_used_msg_id FROM forum WHERE forum_id = @forum_id
I have multiple applications which can potentially update the same trigger simultaneously. Each application:
(1) Reads the contents of the current trigger (2) Creates a new trigger based on the current contents (3) Drops the trigger (4) Creates the new trigger
I need to insure that once one application begins step (1), then no other application can start step (1) until step (4) has completed.
Any ideas on how this can be done? Some databases have a concept of locking tables explicitly, so for them, I can do:
I have an update statement and it looks like it is holding exclusive lock on the table and does not release it until it completes. a PAGLOCK hint has been specified on the update statement and i think it is being ignored. It is a transaction database.so, other queries accessing that table has to wait for it to complete and thus causing timeouts.The Update statement is also causing high IO and CPU utilization. How do I
1) reduce the granularity level to prevent the locking 2)any ideas on optimizing the query?
Here is the query:
UPDATE Customers SET IndexStart = TMP.IndexStart, IndexPosition = TMP.IndexStart, IndexStop = TMP.IndexStop, IndexLevel = TMP.IndexLevel FROM Customers AS C WITH (PAGLOCK) INNER JOIN #tmp_IndexBCs AS TMP WITH (TABLOCKX) ON C.ID= TMP.ID
We are facing atable lock issue , while running a SSIS package We have two flows . one for insert and other for update into the same target table. The update is done using a procedure.
We have disabled the lock table option in the target( in the insert flow) The first set of records which come for updates flow fine. But when the records start coming into the insert flow, the data flow stops.
Is this a table lock issue? How do i set the commit interval at the update flow? can someone lpease help me out of this situation
Hi,I need to lock a table so that Inserts are prevented as well as deleted andupdates. At present I'm thinking this might do it:SELECT * FROM myTable WITH(UPLOCK)but then again I'm not sure whether this will cover the insert case.Thanks,Robin