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

Bug #13890: DbTarget log transaction #14020

Merged
merged 8 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Yii Framework 2 Change Log
- Bug #13790: Fixed error in `\yii\widgets\MaskedInput` JavaScript by raising version required (samdark)
- Bug #13807: Fixed `yii\db\QueryBuilder` to inherit subquery params when building a `INSERT INTO ... SELECT` query (sergeymakinen)
- Bug #13848: `yii\di\Instance::ensure()` wasn't throwing an exception when `$type` is specified and `$reference` object isn't instance of `$type` (c-jonua)
- Bug #13890: DbTarget log transaction bug (shirase)
- Bug #13901: Fixed passing unused parameter to `formatMessage()` call in `\yii\validators\IpValidator` (Kolyunya)
- Bug #13961: Fixed `unserialize()` error during RBAC rule retrieving from PostgreSQL DBMS (vsguts)
- Bug #14012: `yii\db\pgsql\Schema::findViewNames()` was skipping materialized views (insolita)
Expand Down
6 changes: 6 additions & 0 deletions framework/log/DbTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function init()
*/
public function export()
{
if ($this->db->getTransaction()) {
$this->db = clone $this->db;
$this->db->pdo = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late reply, but does this work in a master-slave setup? The situation is a bit more complex there:

if ($this->_master) {
if ($this->pdo === $this->_master->pdo) {
$this->pdo = null;
}
$this->_master->close();
$this->_master = null;
}
if ($this->pdo !== null) {
Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__);
$this->pdo = null;
$this->_schema = null;
$this->_transaction = null;
}
if ($this->_slave) {
$this->_slave->close();
$this->_slave = null;
}

also transaction property of the DB is not reset so for each call to export another connection is opened even though there is no transaction on current connection anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

master-slave should work well. I see no reason for it not to.

Transaction property — yes. Should be reset. Good catch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #14121

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. It could be slave transaction...

$this->db->open();
}

$tableName = $this->db->quoteTableName($this->logTable);
$sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]])
VALUES (:level, :category, :log_time, :prefix, :message)";
Expand Down
25 changes: 25 additions & 0 deletions tests/framework/log/DbTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,29 @@ public function testTimestamp()
$loggedTime = $query->createCommand(self::getConnection())->queryScalar();
static::assertEquals($time, $loggedTime);
}

public function testTransactionRollBack()
{
$db = self::getConnection();
$logger = Yii::getLogger();

$tx = $db->beginTransaction();

$messsageData = [
'test',
Logger::LEVEL_WARNING,
'test',
time(),
[]
];

$logger->messages[] = $messsageData;
$logger->flush(true);

$tx->rollBack();

$query = (new Query())->select('COUNT(*)')->from(self::$logTable)->where(['category' => 'test', 'message' => 'test']);
$count = $query->createCommand($db)->queryScalar();
static::assertEquals(1, $count);
}
}