return $this->getConnectionRef( DB_REPLICA, $groups );
}
- /**
- * Begins an atomic section and returns a database connection to the master DB, for updating.
- *
- * @since 1.29
- *
- * @param string $fname
- *
- * @return Database
- */
- public function beginAtomicSection( $fname ) {
- $db = $this->getWriteConnection();
- $db->startAtomic( $fname );
-
- return $db;
- }
-
- /**
- * @since 1.29
- *
- * @param IDatabase $db
- * @param string $fname
- */
- public function commitAtomicSection( IDatabase $db, $fname ) {
- $db->endAtomic( $fname );
- $this->releaseConnection( $db );
- }
-
- /**
- * @since 1.29
- *
- * @param IDatabase $db
- * @param string $fname
- */
- public function rollbackAtomicSection( IDatabase $db, $fname ) {
- // FIXME: there does not seem to be a clean way to roll back an atomic section?!
- $db->rollback( $fname, 'flush' );
- $this->releaseConnection( $db );
- }
-
}
/**
* Forces all future calls to getReadConnection() to return a write connection.
* Use this before performing read operations that are critical for a future update.
- * Calling beginAtomicSection() implies a call to prepareForUpdates().
*
* @since 1.29
*/
return parent::getWriteConnectionRef();
}
- /**
- * Begins an atomic section and returns a database connection to the master DB, for updating.
- *
- * @since 1.29
- *
- * @note: This causes all future calls to getReadConnection() to return a connection
- * to the master DB, even after commitAtomicSection() or rollbackAtomicSection() have
- * been called.
- *
- * @param string $fname
- *
- * @return Database
- */
- public function beginAtomicSection( $fname ) {
- // Once we have written to master, do not read from replica.
- $this->prepareForUpdates();
-
- return parent::beginAtomicSection( $fname );
- }
-
}
$manager = new SessionConsistentConnectionManager( $lb );
$manager->releaseConnection( $database );
}
-
- public function testBeginAtomicSection() {
- $database = $this->getIDatabaseMock();
- $lb = $this->getLoadBalancerMock();
-
- $lb->expects( $this->exactly( 2 ) )
- ->method( 'getConnection' )
- ->with( DB_MASTER )
- ->will( $this->returnValue( $database ) );
-
- $database->expects( $this->once() )
- ->method( 'startAtomic' )
- ->will( $this->returnValue( null ) );
-
- $manager = new SessionConsistentConnectionManager( $lb );
- $manager->beginAtomicSection( 'TEST' );
-
- // Should also ask for a DB_MASTER connection.
- // This is asserted by the $lb mock.
- $manager->getReadConnection();
- }
-
- public function testCommitAtomicSection() {
- $database = $this->getIDatabaseMock();
- $lb = $this->getLoadBalancerMock();
-
- $lb->expects( $this->once() )
- ->method( 'reuseConnection' )
- ->with( $database )
- ->will( $this->returnValue( null ) );
-
- $database->expects( $this->once() )
- ->method( 'endAtomic' )
- ->will( $this->returnValue( null ) );
-
- $manager = new SessionConsistentConnectionManager( $lb );
- $manager->commitAtomicSection( $database, 'TEST' );
- }
-
- public function testRollbackAtomicSection() {
- $database = $this->getIDatabaseMock();
- $lb = $this->getLoadBalancerMock();
-
- $lb->expects( $this->once() )
- ->method( 'reuseConnection' )
- ->with( $database )
- ->will( $this->returnValue( null ) );
-
- $database->expects( $this->once() )
- ->method( 'rollback' )
- ->will( $this->returnValue( null ) );
-
- $manager = new SessionConsistentConnectionManager( $lb );
- $manager->rollbackAtomicSection( $database, 'TEST' );
- }
-
}