Skip to content

Commit 6a932e3

Browse files
committed
Merge pull request #690 from adrienbrault/dbal-717-2.4
Cherry pick to 2.4: [DBAL-717] Fix bug in MasterSlaveConnection with keepSlave option and switch back after transaction.
2 parents f15c482 + 3cf9885 commit 6a932e3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/Doctrine/DBAL/Connections/MasterSlaveConnection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ public function connect($connectionName = null)
163163
}
164164

165165
if ($this->connections[$connectionName]) {
166-
if ($forceMasterAsSlave) {
167-
$this->connections['slave'] = $this->_conn = $this->connections['master'];
168-
} else {
169-
$this->_conn = $this->connections[$connectionName];
166+
$this->_conn = $this->connections[$connectionName];
167+
168+
if ($forceMasterAsSlave && ! $this->keepSlave) {
169+
$this->connections['slave'] = $this->_conn;
170170
}
171+
171172
return false;
172173
}
173174

tests/Doctrine/Tests/DBAL/Functional/MasterSlaveConnectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,23 @@ public function testKeepSlaveBeginTransactionStaysOnMaster()
102102
$conn->connect('slave');
103103
$this->assertFalse($conn->isConnectedToMaster());
104104
}
105+
106+
/**
107+
* @group DBAL-335
108+
*/
109+
public function testKeepSlaveInsertStaysOnMaster()
110+
{
111+
$conn = $this->createMasterSlaveConnection($keepSlave = true);
112+
$conn->connect('slave');
113+
114+
$conn->insert('master_slave_table', array('test_int' => 30));
115+
116+
$this->assertTrue($conn->isConnectedToMaster());
117+
118+
$conn->connect();
119+
$this->assertTrue($conn->isConnectedToMaster());
120+
121+
$conn->connect('slave');
122+
$this->assertFalse($conn->isConnectedToMaster());
123+
}
105124
}

0 commit comments

Comments
 (0)