You need to sign in to do that
Don't have an account?
![Ken_Koellner Ken_Koellner](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
On Insert why would you get UNABLE_TO_LOCK_ROW?
A client of one of our web service written in Apex reported an error. It turns out there was an error --
Insert failed. First exception on row 9; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusie access to this record: []
I'm trying to think of what would cause an lock problem on an insert. The method does to a start transaction and then inserts a row in Opportunity and then several in OpportunityLineItem.
I don't see how the problem could be with the row being inserted. Could it be with something locked a parent record like account? Could something have already read the Opportunity record while the OpportunityLineItem records where being written?
Check this doc its very useful here you can see all the soql exceptions
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_concepts_core_data_objects.htm
Ignacio.
I noticed that that document states that the UNABLE_TO_LOCK_ROW could be thrown on a timeout.
That means it might not actually be transactions interfering with each other, huh?
I would think that deadlock would be very hard to hit on inserts so that may be it.
I have the same issue and can't find out why. The error appears on line 14 here: I use constructor-like classes for objects I use most including the user object. It does noting else than to create sample user objects with all required fields filled with sample data and returns them in a list. When I run this test class all by itself it goes fine, but as soon as I run all tests it fails on the "insert users" with UNABLE_TO_LOCK_ROW.
Any ideas?
UNABLE_TO_LOCK_ROW exception occur when there was an exception thrown in a Before Insert trigger. There was not a deadlock condition, just some other code that encountered an exception. The debug log showed the actual issue.
To the best of my knowlege SF uses a poor-man's techinque of lock detection via time out so it doesn't have the ability to tell you what the locked record that is blocking the transaction is.
I am facing the same issue, I have scheduled a batch apex to create opportunities and system throws an error UNABLE_TO_LOCK_ROW with the account id(meaning account is locked and my opty creation code is trying to access it). May I know how did you resolve this issue? Please let me know. Thanks!
There's one technique we do use. For certain updates we put the DMLs in a loop and try three times. Virtually all the time we get the DML to stick within three tries. At the DF talk, I asked the presenter if this technique is a good idea or not. The comment was that effectively what I'm doing is extended the timeout. As you know from the comments earlier this thread, Apex uses poor man's deadlock detection via timeout. So if you just wait a bit longer, you may get your DML to stick.
A technique I have not tried would be to try to grab a write lock on whatever is causing the deadlock. The thing is, you may not be easily determine that. If you are inserting a child record and the child object as one parent that you are fairly sure is creating the issue, you could try reading FOR UPDATE the parent record prior to the DML That might help. The thing would be, you might not know whether you are grabbing the parent that is indeed causing the problem. So maybe doing a read FOR UPDATE on an Account might help avoid deadlock when inserting an Opportunity under that Account.
I will look for the DF video and will watch it. I got your point regarding if you cannot identify the parent which causes this issue. In my case, debug log showed account id after the error hence I think putting a read FOR UPDATE on the account might work.
Thanks!