-
Notifications
You must be signed in to change notification settings - Fork 209
PHPC-1231: Add Session::isInTransaction to indicate whether a transaction is active #882
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--TEST-- | ||
MongoDB\Driver\Session::isInTransaction() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test would do well to cover both the "started" and "in progress" transaction states. At the moment, I believe it only covers "started", which is before the first command is sent. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our test was basically that test already, but I added two sections that actually have an operation in the transaction too. |
||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
<?php skip_if_not_libmongoc_crypto() ?> | ||
<?php skip_if_server_version('<', '4.0'); ?> | ||
<?php skip_if_not_replica_set(); ?> | ||
<?php skip_if_not_clean(DATABASE_NAME, COLLECTION_NAME); ?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
$manager = new MongoDB\Driver\Manager(URI); | ||
|
||
/* Create collections as that can't be (automatically) done in a transaction */ | ||
$cmd = new \MongoDB\Driver\Command([ | ||
'create' => COLLECTION_NAME, | ||
]); | ||
$manager->executeCommand(DATABASE_NAME, $cmd); | ||
|
||
/* Start a session */ | ||
$session = $manager->startSession(); | ||
|
||
/* Empty transaction, and aborted empty transaction */ | ||
var_dump($session->isInTransaction()); | ||
$session->startTransaction(); | ||
var_dump($session->isInTransaction()); | ||
$session->abortTransaction(); | ||
var_dump($session->isInTransaction()); | ||
|
||
/* Empty transaction, and committed empty transaction */ | ||
var_dump($session->isInTransaction()); | ||
$session->startTransaction(); | ||
var_dump($session->isInTransaction()); | ||
$session->commitTransaction(); | ||
var_dump($session->isInTransaction()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that transactions are started lazily during the first command, how does this interact when the only operation is an abort or commit? I assume the driver is still sending something in this case and the server just registers it as a no-op? Perhaps a question for @ShaneHarvey. |
||
|
||
/* Aborted transaction with one operation */ | ||
var_dump($session->isInTransaction()); | ||
$session->startTransaction(); | ||
$bw = new \MongoDB\Driver\BulkWrite(); | ||
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] ); | ||
$manager->executeBulkWrite(NS, $bw, ['session' => $session]); | ||
var_dump($session->isInTransaction()); | ||
$session->abortTransaction(); | ||
var_dump($session->isInTransaction()); | ||
|
||
/* Committed transaction with one operation */ | ||
var_dump($session->isInTransaction()); | ||
$session->startTransaction(); | ||
$bw = new \MongoDB\Driver\BulkWrite(); | ||
$bw->insert( [ '_id' => 0, 'msg' => 'Initial Value' ] ); | ||
$manager->executeBulkWrite(NS, $bw, ['session' => $session]); | ||
var_dump($session->isInTransaction()); | ||
$session->commitTransaction(); | ||
var_dump($session->isInTransaction()); | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
bool(false) | ||
bool(true) | ||
bool(false) | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method name looks strange to me, but I think it's preferable to the more verbose option of
isTransactionInProgress()
or the like.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change it to
inTransaction
? Don't like that much either...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to suggest
inTransaction
because it's not consistent with prefixing boolean-returning methods with "is". Totally OK with sticking withisInTransaction()
.