database-deadlocks

Are the rows locked in order in a SELECT … ORDER BY … FOR UPDATE statement?

时光毁灭记忆、已成空白 提交于 2019-12-04 07:27:30
This question can be considered as a follow-up on my comment on Can two concurrent but identical DELETE statements cause a deadlock? . I am wondering if the rows are locked in ascending my_status order in the following statement: SELECT 1 FROM my_table ORDER BY my_status FOR UPDATE; There is an interesting remark on https://www.postgresql.org/docs/9.5/static/sql-select.html which says: It is possible for a SELECT command running at the READ COMMITTED transaction isolation level and using ORDER BY and a locking clause to return rows out of order. This is because ORDER BY is applied first. The

“deadlock victim” in transaction, how to change the priority?

北城以北 提交于 2019-12-04 04:05:51
问题 i have logged an exception thrown by an ASP.NET-Application. Message: Transaction (Process ID 56) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction. I'm sure that the reason for it was, that i was running some selects directly in SSMS on tables that were queried in the application in the same time. So now my question is: Can i change the priority SQL-Server chooses "deadlock victims"? I would like to

Deadlock error on same table with two Update SQL statements

不问归期 提交于 2019-12-04 03:14:10
I have a C# project which writes data to a TSQL database. There are two update statements which run within a loop, eg.: for (int i = 0; i < customersProducts.Count; i++) { CustomerProducts c = customersProducts[i]; // Update product dimensions for (int j = 0; j < c.Count; j++) { Product p = c[j]; updateProductDimensions(p); } // ... some processing // Update product for (int j = 0; j < c.Count; j++) { Product p = c[j]; updateProduct(p); } } The updateProductDimensions() and updateProduct() both trigger SQL Update statements. There is some overlap in the columns that are updated: string

Database Deadlock in SELECT FOR UPDATE

回眸只為那壹抹淺笑 提交于 2019-12-03 20:18:36
I'm getting deadlock intermittently in my application. My application has 1 table e.g EMPLOYEE (ID (PK), NAME, SAL) and there are 2 sessions. Session 1: SELECT ID, NAME, SAL FROM EMPLOYEE WHERE SAL = (SELECT MIN(SAL) FROM EMPLOYEE) FOR UPDATE Let say the query return EMPLOYEE ROW having ID=2 then application does some processing like rs.updateInt(ID_SAL, 10); Session 2: (for other business logic) SELECT ID, NAME, SAL FROM EMPLOYEE WHERE ID=2 FOR UPDATE. So, in the application both sessions try to update the same row (in example row with ID=2) Such situation is expected and hence I thought

C3p0 APPARENT DEADLOCK exception

戏子无情 提交于 2019-12-03 17:07:20
问题 I keep getting this exception in my Tomcat log: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76b28200 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76b28200 -- APPARENT DEADLOCK!!! Complete Status: Managed Threads

Can two concurrent but identical DELETE statements cause a deadlock?

放肆的年华 提交于 2019-12-03 13:10:39
Assume some_table has two rows, with primary key 1 and 2 . The following sequence of statements can cause a deadlock: session 1: begin; session 2: begin; session 1: DELETE FROM my_table WHERE my_key = 1; session 2: DELETE FROM my_table WHERE my_key = 2; session 1: DELETE FROM my_table WHERE my_key = 2; session 2: DELETE FROM my_table WHERE my_key = 1; The deadlock would not have occurred if both sessions deleted in the same order. Now, coming to my question, what happens if the DELETE statement touches multiple rows? For example: session 1: begin; session 2: begin; session 1: DELETE FROM my

SQL Server Deadlock Fix: Force join order, or automatically retry?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 12:59:39
i have a stored procedure that performs a join of TableB to TableA : SELECT <--- Nested <--- TableA Loop <-- | ---TableB At the same time, in a transaction, rows are inserted into TableA , and then into TableB . This situation is occasionally causing deadlocks, as the stored procedure select grabs rows from TableB , while the insert adds rows to TableA , and then each wants the other to let go of the other table: INSERT SELECT ========= ======== Lock A Lock B Insert A Select B Want B Want A ....deadlock... Logic requires the INSERT to first add rows to A , and then to B , while i personally

How to do a safe “SELECT FOR UPDATE” with a WHERE condition over multiple tables on a DB2?

ε祈祈猫儿з 提交于 2019-12-03 11:33:54
Problem On a DB2 (version 9.5) the SQL statement SELECT o.Id FROM Table1 o, Table2 x WHERE [...] FOR UPDATE WITH RR gives me the error message SQLSTATE=42829 (The FOR UPDATE clause is not allowed because the table specified by the cursor cannot be modified). Additional info I need to specify WITH RR , because I'm running on isolation level READ_COMMITTED , but I need my query to block while there is another process running the same query. Solution so far... If I instead query like this: SELECT t.Id FROM Table t WHERE t.Id IN ( SELECT o.Id FROM Table1 o, Table2 x WHERE [...] ) FOR UPDATE WITH

Deadlock error in INSERT statement

混江龙づ霸主 提交于 2019-12-03 10:34:29
We've got a web-based application. There are time-bound database operations (INSERTs and UPDATEs) in the application which take more time to complete, hence this particular flow has been changed into a Java Thread so it will not wait (block) for the complete database operation to be completed. My problem is, if more than 1 user comes across this particular flow, I'm facing the following error thrown by PostgreSQL: org.postgresql.util.PSQLException: ERROR: deadlock detected Detail: Process 13560 waits for ShareLock on transaction 3147316424; blocked by process 13566. Process 13566 waits for

Deadlock involving foreign key constraint

大城市里の小女人 提交于 2019-12-03 08:56:30
I would like to understand better a mechanism of locking in postgres. Let's say that tree can have apples (via foreign key on apple table). It seems that when selecting a tree for update lock is obtained on an apple. However, the operation is not blocked even if someone else already holds a lock on this apple. Why is it so? p.s. Please don't suggest to remove "select for update". Scenario Transaction 1 Transaction 2 BEGIN . update apple; . . BEGIN . select tree for update; . update apple; . --halts because of the other transaction locking an apple update apple; . -- deadlock . COMMIT -