georaldc
georaldc

Reputation: 1940

PHP: Is there a relationship between PDO transactions and Sessions?

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?

Upvotes: 1

Views: 194

Answers (1)

Jan Waś
Jan Waś

Reputation: 81

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.

Upvotes: 0

Related Questions