Alexander Ponomarenko
Alexander Ponomarenko

Reputation: 559

Transaction between controller actions in Play

Is there a way to start "transaction" between several controller actions that would keep model changes, but not save them unless all actions are completed?

Lets say I have a following user flow: step1 -> step2 -> step3 -> step4 On each step user fills some data and each step has a "Cancel" button that needs to cancel the wizard as well as all changes from previous steps.

Since we don't want every step to pass all filled data to next steps in order to save to database only on last step - each step creates or changes one or more model instance and stores them in database. How can we revert changes from all previous steps when "Cancel" clicked?

Ideally we would want to call some "TransactionSupport.startTransaction" in step1 and "TransactionSupport.endTransaction" on step4. But taking into account that each step request could go to a different Play server - how would we store this transaction between servers?

Upvotes: 1

Views: 282

Answers (1)

Pere Villega
Pere Villega

Reputation: 16439

As per documentation, and knowing that Play is stateless, this doesn't seem possible. In fact if you disable the default Transaction manager of Play via annotations with:

@play.db.jpa.NoTransaction

Then Play won't obtain a DB connection for that request.

To solve your issue, a possible workaround is to store the information of the steps in a temporal table and only commit the data to the "working" tables at the end.

Link the information to the user id, wizard id and session id, so you can detect stale data.

Upvotes: 1

Related Questions