Is a DB transaction executed when it is committed? cache_get() is not working as expected when called in a request initiated by drupal_http_request() on the same server.  Is it true that the cache_set() immediately before drupal_http_request() doesn't actually take place in the database until the first database transaction is committed?

Given the sequence:


cache_set($cid, $data)
cache_get($cid) // returns data
external request using drupal_http_request()

cache_get($cid) // same $cid, returns FALSE

cache_get($cid) // of course, returns data


To make the cached item available to the request initiated by drupal_http_request() I wrapped the cache_set call in a function like this, but it had no effect.



function cache_set_wrapper($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
  $txn = db_transaction();
  return cache_set($cid, $data, $bin, $expire);
}


Why is there a discrepancy in the cache between the initial request on the second request?
Copied from my comments:

That depends on where and how you are calling cache_set(). A transaction only exists if someone explicitly started it. If you're in a node, user or other entity insert/update hook for example, then yes, then you are in a transaction.

If there is an active transaction that it will only get committed at the end. Nested transactions are supported but only to rollback to a certain step. Does the external request need to happen immediately? Doing external requests from a entity crud hook is always a bit problematic for performance (imagine a bulk resave or import of nodes) too. Consider moving the HTTP request to a shutdown function or a queue.