Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP requests in transactions?

I have a model which sends a HTTP request to an external web service on creation in order to find out some information to add before it is saved.

Currently I'm doing this in a before_create callback. I recently learned that before/after callbacks happen within database transactions.

Am I opening myself up to any issues such as limiting DB throughput by doing this? Would it be better to commit the record before sending the http request and then update the record when it returns?

like image 951
David Tuite Avatar asked Mar 26 '12 10:03

David Tuite


1 Answers

As long a s you keep a transaction open, all the locks it acquired are active. If you have a call to an external source that may stall you for a long period of time, be sure not to to have any unrelated locks in the same transaction.

In other words: don't put anything else into the same transaction.

If you don't mind the new row being visible before you look up the additional information, you might just commit and later update the row.

Or you fetch the information from the external web service before you even start the transaction. That would be cleanest / fastest solution for the database.

PostgreSQL lock types.
How to view locks.

like image 88
Erwin Brandstetter Avatar answered Sep 21 '22 19:09

Erwin Brandstetter