# How to avoid errors on rapid queries?

**URL:** <https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157>\
**Category:** Dgraph Clients\
**Tags:** untagged, dgraph-js\
**Created:** [May 14, 2018, 3:38am UTC](https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157 "2018-05-14T03:38:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [May 14, 2018, 3:38am UTC](https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157/1 "2018-05-14T03:38:57Z")

</div>

**Moved from GitHub [dgraph-js/36](https://github.com/dgraph-io/dgraph-js/issues/36)**

_Posted by_ [good-idea](https://github.com/good-idea):

I’m setting up some integration tests, and coming across the “Error: Transaction has been aborted. Please retry” error. There’s a function that resets the data I am mutating before and after each test. It works about 80% of the time, but otherwise is throwing the error.

If I put a short async ‘sleep’ delay in the beforeEach/afterEach functions, it works as expected. But, this doesn’t leave me confident that my code can handle requests which might fail because they are submitted in rapid succession. Simply retrying the query once more doesn’t feel good either.

Am I missing something?

Here’s a simplified version of the test.

```auto
// @flow
/* eslint-disable no-undef */
import { request } from '../../../ __tests__ /utils'
import { sampleUser } from '../../../database/stubs/users'
import { updateUser, getUser } from '../UserModel'

const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const resetUserData = async () => {
	await updateUser(sampleUser.uid, { input: sampleUser })
}

describe('[updateViewerProfile]', () => {
	beforeAll(async () => {
		await resetUserData()
		// await sleep(100) // <-- uncommenting these two gets it working
	})
	afterAll(async () => {
		// await sleep(100) // <--
		await resetUserData()
	})

	it('should update existing data', async () => {
		const newBioLine = 'Test bio line'

		// Note: this is an actual GraphQL mutation that goes through my resolvers.
		// This is not querying dgraph directly.
		const mutation = /* GraphQL */ `
		mutation updateProfile($bioLine: String) {
			updateViewerProfile(input: {bioLine: $bioLine }) {
				username
				bioLine
			}
		}
		`
		const variables = {
			bioLine: newBioLine,
		}
		const context = { viewer: joseph }
		const result = await request(mutation, { variables, context })
		expect(result.data.updateViewerProfile.bioLine).toBe(newBioLine)
	})
})

```

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [May 14, 2018, 5:48am UTC](https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157/2 "2018-05-14T05:48:14Z")

</div>

[gpahal](https://github.com/gpahal) _commented_ :

An aborted error is returned if other transactions concurrently modify the same data that was  
modified in this transaction. It is up to the user to retry transactions when they fail. This is more of a dgraph issue rather than the client’s. You can create a new issue in the main dgraph repo.

Similar issues in dgraph:

- [[question] insert seems not fast enough · Issue #2018 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/2018)
- [About transaction abort error while persist edges](http://discuss.hypermode.com/t/about-transaction-abort-error-while-persist-edges/2213)
- [Add IgnoreDataConflict option · Issue #2221 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/2221)

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [May 16, 2018, 5:49pm UTC](https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157/3 "2018-05-16T17:49:05Z")

</div>

[good-idea](https://github.com/good-idea) _commented_ :

Got it, thanks @gpahal

Also, for anyone coming here concerning `dgraph-js`, I was simply forgetting to `await` a `txn.commit()` in one of the functions used (but not shown) above. 🤦🏼‍♂️

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 11, 2020, 4:16am UTC](https://discuss.dgraph.io/t/how-to-avoid-errors-on-rapid-queries/8157/4 "2020-07-11T04:16:28Z")

</div>


