User login

Beware of transactions in Drupal 7

Drupal 7's database layer makes use of transactions in several places. Be careful when you expect to see the results of node_save() outside of the scope of the current request. If someone accesses your website data before the transaction has been committed, the results of queries executed within are not visible. Either all queries succeed or nothing happens at all in a database transactions.

Why could that be a problem? We recently had the situation of someone initiating communication with another Drupal site in a hook_node_insert() implementation. The other side's page callback itself did a HTTP request for getting information about the node being saved and for that request the node was not in the database. In this case, the solution was to add a form submit handler to be executed after the original node form handler, which respects the concept of the transaction. It actually makes no sense to inform other sites about the publication while the transaction might still fail.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.