Database commits and rollbacks for controller commands
Throughout the execution of a controller command, data is often created or updated. In many cases, the database must be updated with the new information at the end of the transaction.
The solution controller marks the beginning of the transaction before calling the business logic facade. When the execution of the controller command is complete, the controller command returns a view name to the controller. The controller is responsible for marking the end of the transaction. The actual point at which the transaction ends (before or after invoking the view) is dependent upon the type of view used.
For Web applications, the solution controller determines the view type by looking up the view name in the Struts configuration files:
- If the view command to be used is a forward or a direct view command, then the view command executes within the context of the current transaction. In this case, the database commit or rollback does not occur until the view command completes.
- If the response is a redirect, then the view command operates outside of the scope of the current transaction and the database commit or rollback occurs before the view command is called.
In the cases where the view command executes under the same transaction scope as the controller command, an error in the view command execution causes a rollback of the entire transaction. This may or may not be the desired outcome, depending upon your business logic.