[RFC] Trait for transactional operations

Hi app devs,

In https://github.com/nextcloud/mail/pull/6030#discussion_r806939201 we realized that it’s a repetitive pattern to wrap a series of db operations in a transaction so that they are done atomically.

So I wonder if we should add a helper to OCP so all apps can benefit from this.

I’ve opened a PR for discussion at https://github.com/nextcloud/server/pull/31205.

The usage would look like this

class MySerivce {
	use Transactional;
	public function doSomethingBig(): int {
		return $this->atomic(function() {
			$user = $this->userMapper->creatUser();
			$this->profileMapper->createProfile($user);
			return $this->mailAccountsMapper->provisionUser($user);
		}, $this->db);
	}
}

^ this business logic would either be done completely or rolled back. So this trait will be best used in the service layer.

Ref spring - Where should "@Transactional" be placed Service Layer or DAO - Stack Overflow
Ref java - Where does the @Transactional annotation belong? - Stack Overflow

Cheers,
Christoph

1 Like

This will be shipped with Nextcloud 24.

Documentation will be updated via https://github.com/nextcloud/documentation/pull/8048.

This topic was automatically closed after 9 days. New replies are no longer allowed.