shmish111
shmish111

Reputation: 3787

Testing database transactions

How can I write and run automated tests that check that my database transaction strategy is removing race conditions? At the moment all I do is test it in development by putting a breakpoint in the code and sending two requests, I can then see in slow motion what happens. This is not something I can automate though, it's not even testing really, just part of development.

Upvotes: 1

Views: 71

Answers (2)

usr
usr

Reputation: 171178

Perform a load test with a realistic work-load. Unfortunately, this is not easy to do. Race conditions are hard to discover on any platform. I know of no systematic way to find such bugs.

Sometimes you can exclude the possibility of inconsistencies by construction. For example:

  • A transaction running under SERIALIZABLE behaves as if it was the only transaction in the system. Therefore, there are never data races.
  • A read-only transaction under SNAPSHOT behaves the same way. Total data consistency.
  • A UNIQUE INDEX will never violate its integrity guarantees.

As you can see you can sometimes make your code safe by construction so that there is minimal need to test.

Upvotes: 1

Boris Pavlović
Boris Pavlović

Reputation: 64622

Your test can spawn threads and run two or more threads making the same request isolated by the transaction.

Upvotes: 1

Related Questions