Microsoft SQL Server 2008:写入服务器失败

Posted

技术标签:

【中文标题】Microsoft SQL Server 2008:写入服务器失败【英文标题】:Microsoft SQL Server 2008: Write to the server failed 【发布时间】:2017-07-19 06:23:25 【问题描述】:

我有一个 Django 应用程序,它带有一个执行数据库事务的工作进程。在后台,我有一个 Azure SQL 数据库附加到我的应用程序。工作人员代码如下所示:

class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
        django.setup()
    consumer = Consumer()

    logging.info('Booting drive consumer')

    while True:
        messages = consumer.poll()

        for message in messages:
          ...
          DriveEvent.objects.create(event_id=response['Id'], drive_id=drive_payload['drive_id'])
          ...

大约 20 分钟后,create db object 调用开始失败并出现以下错误:

[drive-consumer-1]2017-07-19T03:19:08.545128400Z Traceback (most recent call last):
[drive-consumer-1]2017-07-19T03:19:08.545133000Z   File "/code/miles/management/commands/drive_consumer_worker.py", line 28, in handle
[drive-consumer-1]2017-07-19T03:19:08.545137900Z     drive_consumer.consume(json.loads(record.value))
[drive-consumer-1]2017-07-19T03:19:08.545142500Z   File "/code/miles/workers/drive_consumer.py", line 35, in consume
[drive-consumer-1]2017-07-19T03:19:08.545152200Z     self._on_create(message['calendar_id'], drive_payload, message['access_token'])
[drive-consumer-1]2017-07-19T03:19:08.545156900Z   File "/code/miles/workers/drive_consumer.py", line 46, in _on_create
[drive-consumer-1]2017-07-19T03:19:08.545161500Z     DriveEvent.objects.create(event_id=response['Id'], drive_id=drive_payload['drive_id'])
[drive-consumer-1]2017-07-19T03:19:08.545166500Z   File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
[drive-consumer-1]2017-07-19T03:19:08.545171300Z     return getattr(self.get_queryset(), name)(*args, **kwargs)
[drive-consumer-1]2017-07-19T03:19:08.545175900Z   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 399, in create
[drive-consumer-1]2017-07-19T03:19:08.545180700Z     obj.save(force_insert=True, using=self.db)
[drive-consumer-1]2017-07-19T03:19:08.545185400Z   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
[drive-consumer-1]2017-07-19T03:19:08.545190200Z     force_update=force_update, update_fields=update_fields)
[drive-consumer-1]2017-07-19T03:19:08.545194800Z   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 821, in save_base
[drive-consumer-1]2017-07-19T03:19:08.545211400Z     with transaction.atomic(using=using, savepoint=False):
[drive-consumer-1]2017-07-19T03:19:08.545215800Z   File "/usr/local/lib/python2.7/site-packages/django/db/transaction.py", line 184, in __enter__
[drive-consumer-1]2017-07-19T03:19:08.545220400Z     connection.set_autocommit(False, force_begin_transaction_with_broken_autocommit=True)
[drive-consumer-1]2017-07-19T03:19:08.545224900Z   File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 391, in set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545229600Z     self._set_autocommit(autocommit)
[drive-consumer-1]2017-07-19T03:19:08.545234000Z   File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 453, in _set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545238600Z     self.connection.autocommit = autocommit
[drive-consumer-1]2017-07-19T03:19:08.545243100Z   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
[drive-consumer-1]2017-07-19T03:19:08.545247700Z     six.reraise(dj_exc_type, dj_exc_value, traceback)
[drive-consumer-1]2017-07-19T03:19:08.545252200Z   File "/usr/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 453, in _set_autocommit
[drive-consumer-1]2017-07-19T03:19:08.545256800Z     self.connection.autocommit = autocommit
[drive-consumer-1]2017-07-19T03:19:08.545262600Z Error: ('08S01', '[08S01] [FreeTDS][SQL Server]Write to the server failed (20006) (SQLSetConnectAttr)')

数据库初始化(settings.py)看起来像这样:

DATABASES = 
        'default': 
            'ENGINE': 'sql_server.pyodbc',
            'NAME': os.environ.get('DB_NAME'),
            'HOST': os.environ.get('DB_HOST'),
            'PORT': '1433',
            'USER': os.environ.get('DB_USER'),
            'PASSWORD': os.environ.get('DB_PASSWORD'),
            'OPTIONS': 
                'host_is_server': True,
                'driver_supports_utf8': True,
                'extra_params': 'tds_version=7.2;'
            
        
    

【问题讨论】:

【参考方案1】:

根据我的经验,7.2 版本不支持 Azure SQL 数据库似乎是由tds_version 引起的。请参考我对其他 SO 线程 pymssql: Connection to the database only works sometimes 的回答更改为 7.3

可能,GitHub 上的第三方项目michiya/django-pyodbc-azure 可能会帮助您获取更多详细信息。

希望对你有帮助。

【讨论】:

我无法使用 TDS 7.3 版连接到 Azure SQL:('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)') @SakshamGupta 请在您的OPTIONS 中添加一个值为ODBC Driver 13 for SQL Server 的选项driver,然后重试。 谢谢,这似乎有效。我必须使用此处的说明设置 ODBC Driver v13.1:docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/…

以上是关于Microsoft SQL Server 2008:写入服务器失败的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft SQL Server 2008 R2 安装遇到的问题

Microsoft SQL Server 2008 R2 安装遇到的问题

Microsoft SQL Server 2008 日志分析

还原数据库时出现 Microsoft SQL Server 2008 错误

Microsoft SQL Server 2008 主键的含义

Microsoft SQL Server 2008:写入服务器失败