1
votes

I am currently working on a project to uses Yii and stumbled across something that made me scratch my head. I started a db transaction using Yii (which just calls PDO::beginTransaction) and did some database stuff and at the end, store a flash message for the user and do a redirect. I forgot though to commit the transaction so nothing got stored in my database, but what caught my attention is that my flash message also did not appear. Doing a commit or rollback makes the flash message appear just fine.

Basically, I noticed that I could not store any session related data and have it stick after a redirect if I started a transaction and didn't commit/rollback. I normally don't leave transactions hanging so I never noticed this behavior before.

So is there a relationship between the 2 that would prevent Sessions from working properly?

1
Starting a transaction and not committing is a no-brainer: it get's rolled back implicitly when the [database] connection is closed. The interesting bit, if it really applies, is that a rolled back transaction allows the flash message to be displayed .. Is the flash message also persisted in the database or is the display a secondary effect? - user2864740
If it is indeed the case (that the flash works, but your data is not persisted after a rollback) then I would imagine that either 1) there is a silent error when Yii tries to being a transaction for itself because a transaction is already open or 2) Yii creates a new nested transaction that is not committed because the outer transaction is rolled back implicitly when the [database] connection is closed. (Having no existing transaction - via commit or a rollback - on the connection would allow Yii to "do it's thing" on the shared connection at the end of the request.) - user2864740
Now that you mentioned it, I just remembered that I was storing sessions (which would include my flash messages) in my database. I still don't quite understand why calling rollback makes the session values stick. Wouldn't that be the same as having pdo rollback on its own at the end of the script's execution? - georaldc
Imagine: begin tran {a}; begin tran {b}; commit {for a, issued by b}; [implicit] rollback - the inner commit generally "doesn't stick" so a commit was issued for {b} when the transaction for {a} should already have been closed (which would explain why a rollback still worked). Since the {a} transaction wasn't closed, {b} is an inner transaction and not a secondary sequential transaction. That is my hunch of what is happening, anyway. - user2864740
Hmm. Guess I'll dig around more tomorrow and try to find out what's going on. Thanks for trying to help me out - georaldc

1 Answers

0
votes

Session is written to the database at the end of the request. If you make an explicit rollback, it still gets written to the db outside of the transaction. If you don't, the rollback happens implicitly AFTER the session saving queries are run.