Skip to content

Commit eb86a4b

Browse files
committed
add profile to transaction
add ClientSession::getId()
1 parent 434db7d commit eb86a4b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/ClientSession.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ public static function prepareOptions(&$options){
9292
}
9393
}
9494

95+
/**
96+
* Returns the logical session ID as string for this session, which may be used to identify this session's operations on the server.
97+
* @see https://www.php.net/manual/en/mongodb-driver-session.getlogicalsessionid.php
98+
* @return string
99+
*/
100+
public function getId(){
101+
return $this->mongoSession->getLogicalSessionId()->id->jsonSerialize()['$binary'];
102+
}
103+
95104
/**
96105
* Start a new session in a connection.
97106
* @param Connection $db

src/Transaction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function start($transactionOptions = []){
5757
if($this->clientSession->mongoSession->isInTransaction())
5858
throw new Exception('Nested transaction not supported');
5959
$this->clientSession->db->trigger(Connection::EVENT_START_TRANSACTION);
60+
if($this->mongoSession->db->enableLogging)
61+
Yii::beginProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
6062
$this->clientSession->mongoSession->startTransaction($transactionOptions);
6163
Yii::debug('MongoDB transaction started.', __METHOD__);
6264
}
@@ -68,6 +70,8 @@ public function start($transactionOptions = []){
6870
public function commit(){
6971
Yii::debug('Committing mongodb transaction ...', __METHOD__);
7072
$this->clientSession->mongoSession->commitTransaction();
73+
if($this->mongoSession->db->enableLogging)
74+
Yii::endProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
7175
Yii::debug('Commit mongodb transaction.', __METHOD__);
7276
$this->clientSession->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
7377
}
@@ -79,6 +83,8 @@ public function commit(){
7983
public function rollBack(){
8084
Yii::debug('Rolling back mongodb transaction ...', __METHOD__);
8185
$this->clientSession->mongoSession->abortTransaction();
86+
if($this->mongoSession->db->enableLogging)
87+
Yii::endProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
8288
Yii::debug('Roll back mongodb transaction.', __METHOD__);
8389
$this->clientSession->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
8490
}

0 commit comments

Comments
 (0)