@@ -371,20 +371,40 @@ def schema_editor(self, *args, **kwargs):
371371 return DatabaseSchemaEditor (self , * args , ** kwargs )
372372
373373 @cached_property
374- def sql_server_version (self ):
375- with self .temporary_connection () as cursor :
376- cursor .execute ("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar)" )
377- ver = cursor .fetchone ()[0 ]
378- ver = int (ver .split ('.' )[0 ])
379- if not ver in self ._sql_server_versions :
380- raise NotSupportedError ('SQL Server v%d is not supported.' % ver )
381- return self ._sql_server_versions [ver ]
374+ def sql_server_version (self , _known_versions = {}):
375+ """
376+ Get the SQL server version
377+
378+ The _known_versions default dictionary is created on the class. This is
379+ intentional - it allows us to cache this property's value across instances.
380+ Therefore, when Django creates a new database connection using the same
381+ alias, we won't need query the server again.
382+ """
383+ if self .alias not in _known_versions :
384+ with self .temporary_connection () as cursor :
385+ cursor .execute ("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar)" )
386+ ver = cursor .fetchone ()[0 ]
387+ ver = int (ver .split ('.' )[0 ])
388+ if not ver in self ._sql_server_versions :
389+ raise NotSupportedError ('SQL Server v%d is not supported.' % ver )
390+ _known_versions [self .alias ] = self ._sql_server_versions [ver ]
391+ return _known_versions [self .alias ]
382392
383393 @cached_property
384- def to_azure_sql_db (self ):
385- with self .temporary_connection () as cursor :
386- cursor .execute ("SELECT CAST(SERVERPROPERTY('EngineEdition') AS integer)" )
387- return cursor .fetchone ()[0 ] == EDITION_AZURE_SQL_DB
394+ def to_azure_sql_db (self , _known_azures = {}):
395+ """
396+ Whether this connection is to a Microsoft Azure database server
397+
398+ The _known_azures default dictionary is created on the class. This is
399+ intentional - it allows us to cache this property's value across instances.
400+ Therefore, when Django creates a new database connection using the same
401+ alias, we won't need query the server again.
402+ """
403+ if self .alias not in _known_azures :
404+ with self .temporary_connection () as cursor :
405+ cursor .execute ("SELECT CAST(SERVERPROPERTY('EngineEdition') AS integer)" )
406+ _known_azures [self .alias ] = cursor .fetchone ()[0 ] == EDITION_AZURE_SQL_DB
407+ return _known_azures [self .alias ]
388408
389409 def _execute_foreach (self , sql , table_names = None ):
390410 cursor = self .cursor ()
0 commit comments