Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested transactions as a single transaction. #9

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

pitsolu
Copy link
Contributor

@pitsolu pitsolu commented Dec 6, 2023

In case you call transactions from different places in your code under one other transaction, those end up being nested.

The idea is that there should ever only be one transaction ever - from a db point of view. In the code they might appear
to be multiple transactions, but that is not the case.

Example:

Record::transaction(function(){

	$role = new Role([

		"name"=>"test_role"
	]);
	$role->save();

	// throw new Exception("Fail 1!");
	Record::transaction(function(){

		$role = new Role([

			"name"=>"test_role1"
		]);
		$role->save();

		// throw new Exception("Fail 2!");

		$user = new User([

			"username"=>"yadmin",
			"password"=>sha1("p@55w0rd$$"),
			"role_id"=>$role->id
		]);
		$user->save();
	});

	$user = new User([

		"username"=>"xadmin",
		"password"=>sha1("p@55w0rd$$"),
		"role_id"=>$role->id
	]);
	$user->save();
});

@nicksagona
Copy link
Collaborator

Hey - this looks good. I just pushed up some small changes to your branch. The main thing I noticed was that the exception from the rollback method wasn't bubbling up to the main calling script. So I made the rollback call return the exception so that it can be thrown from the transaction call. This way it bubbles up to the main calling script. Let me know your thoughts.

try {
    Record::transaction(function(){
        $role = new Roles([
            'role' => 'Admin'
        ]);
        $role->save();

        //throw new Exception('Error 1');
        Record::transaction(function(){
            $role = new Roles([
                'role' => 'Editor'
            ]);
            $role->save();

            //throw new Exception('Error 2');
            $user = new Users([
                'username' => 'testuser2',
                'password' => 'password2',
                'email'    => '[email protected]'
            ]);
            $user->save();
        });

        $user = new Users([
            'username' => 'testuser1',
            'password' => 'password1',
            'email'    => '[email protected]'
        ]);
        $user->save();
    });
} catch (\Exception $e) {
    echo $e->getMessage();
}

@pitsolu
Copy link
Contributor Author

pitsolu commented Dec 6, 2023

Excellent!

@nicksagona nicksagona merged commit be66877 into popphp:master Dec 6, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants