Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. The Developer Center continues to support Commerce 6.13.0 through 7.4.1.Visit new site

Service-layer database transactions

Service-layer database transactions

Database transaction behavior is defined on a per-method basis in the service.xml Spring configuration file. Service classes with methods that need to run as transactions inherit the parent bean named txProxyTemplate in service.xml. txProxyTemplate has a property called transactionAttributes that defines the nature of transactions for methods matching particular name patterns. For example, if you include the following property in a service that inherits txProxyTemplate, all service methods whose names begin with update will be run in a transaction.

         core/ep-core/src/main/resources/spring/service/service.xml
				<prop key="update*">PROPAGATION_REQUIRED</prop>

      

The following transaction attributes are in use:

  • PROPAGATION_REQUIRED - Supports a transaction if one already exists. If there is no transaction a new one is started.
  • PROPAGATION_NEVER - Does not execute as a transaction and throws an exception if one already exists.
  • PROPAGATION_SUPPORTS - A new transaction will not be started to run the method, however if a transaction is in progress it will propagate to include the call to this method as well.

The following attributes are also available:

  • PROPAGATION_MANDATORY - Throws an exception if there is no active transaction
  • PROPAGATION_NOT_SUPPORTED - Executes non-transactionally and suppends any existing transaction
  • PROPAGATION_REQUIRES_NEW - Always starts a new transaction. If an active transaction exists, it is suspended.
  • PROPAGATION_NESTED - Runs as a nested transaction if one exists, or starts a new one.

Note that all methods that access the database must have names that match one of the transaction attribute properties defined in txProxyTemplate or an exception will be thrown at runtime.