无法使用 ADO 在 MS SQL Express 服务器上开始事务
Cannot begin transaction on MS SQL Express server with ADO
我想从 C++ 更新 MS SQL 数据库中的一些行。问题是,我无法创建交易。每次我尝试执行 Ado::Connection::BeginTrans
我都会得到一个错误:
A request to establish a connection with the transaction manager was
denied.
我在本地计算机 (Win 10 Pro) 上安装了 SQL Server 2017 Express Edition 的新副本,并创建了一个名为 test
.
的新数据库
连接字符串:
Provider=SQLNCLI11;Server=SNAIL\SQLEXPRESS;Database=test;Trusted_Connection=yes;
我应该怎么做才能摆脱这个错误?
编辑:
部分源码:
#import <msado15.dll> rename_namespace("ADO2"), raw_interfaces_only, raw_native_types, named_guids, no_smart_pointers
const auto connection_string = L"Provider=sqloledb;Data Source=SNAIL\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI";
// create connection
Ref<ADO2::_Connection> m_DbConnection;
m_DbConnection->put_Mode(ADO2::adModeReadWrite);
m_DbConnection->put_ConnectionTimeout(5);
m_DbConnection->Open(connection_string, nullptr, nullptr, ADO2::adConnectUnspecified)
class InAdoTransaction {
private:
Ref<ADO2::_Connection> m_DbConnection;
bool m_Commited;
public:
InAdoTransaction(ADO2::_Connection *Connection) {
m_DbConnection = Connection;
m_Commited = false;
long transaction_level;
HRESULT er = m_DbConnection->BeginTrans(&transaction_level);
ASSERT(er == S_OK);
}
~InAdoTransaction() {
if (!m_Commited) {
Rollback();
}
}
void Rollback() {
m_DbConnection->RollbackTrans();
}
void Commit() {
m_DbConnection->CommitTrans();
m_Commited = true;
}
};
// usage
HRESULT FUNC StoreMetadata()
{
HRESULT er = S_OK;
Ref<ADO2::_Recordset> rst;
InAdoTransaction transaction(m_DbConnection);
if (FAILED(m_DbConnection->Execute(QUERY_TRUNCATE_METADATA_TABLE, nullptr, 0, OUT(rst)))) {
return E_CANNOT_STORE_METADATA;
}
...
transaction.Commit();
}
我终于找到了解决办法。整个问题出在我在表 INFORMATION_SCHEMA.TABLES
和 INFORMATION_SCHEMA.VIEWS
上创建的记录集中。此记录集的锁定类型设置为 LockType::adLockReadOnly
,这导致了问题。我已将其更改为 LockType::adLockOptimistic
,现在一切正常。
但是,提供者 returns 如此具有误导性的错误消息令人遗憾。
我想从 C++ 更新 MS SQL 数据库中的一些行。问题是,我无法创建交易。每次我尝试执行 Ado::Connection::BeginTrans
我都会得到一个错误:
A request to establish a connection with the transaction manager was denied.
我在本地计算机 (Win 10 Pro) 上安装了 SQL Server 2017 Express Edition 的新副本,并创建了一个名为 test
.
连接字符串:
Provider=SQLNCLI11;Server=SNAIL\SQLEXPRESS;Database=test;Trusted_Connection=yes;
我应该怎么做才能摆脱这个错误?
编辑:
部分源码:
#import <msado15.dll> rename_namespace("ADO2"), raw_interfaces_only, raw_native_types, named_guids, no_smart_pointers
const auto connection_string = L"Provider=sqloledb;Data Source=SNAIL\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI";
// create connection
Ref<ADO2::_Connection> m_DbConnection;
m_DbConnection->put_Mode(ADO2::adModeReadWrite);
m_DbConnection->put_ConnectionTimeout(5);
m_DbConnection->Open(connection_string, nullptr, nullptr, ADO2::adConnectUnspecified)
class InAdoTransaction {
private:
Ref<ADO2::_Connection> m_DbConnection;
bool m_Commited;
public:
InAdoTransaction(ADO2::_Connection *Connection) {
m_DbConnection = Connection;
m_Commited = false;
long transaction_level;
HRESULT er = m_DbConnection->BeginTrans(&transaction_level);
ASSERT(er == S_OK);
}
~InAdoTransaction() {
if (!m_Commited) {
Rollback();
}
}
void Rollback() {
m_DbConnection->RollbackTrans();
}
void Commit() {
m_DbConnection->CommitTrans();
m_Commited = true;
}
};
// usage
HRESULT FUNC StoreMetadata()
{
HRESULT er = S_OK;
Ref<ADO2::_Recordset> rst;
InAdoTransaction transaction(m_DbConnection);
if (FAILED(m_DbConnection->Execute(QUERY_TRUNCATE_METADATA_TABLE, nullptr, 0, OUT(rst)))) {
return E_CANNOT_STORE_METADATA;
}
...
transaction.Commit();
}
我终于找到了解决办法。整个问题出在我在表 INFORMATION_SCHEMA.TABLES
和 INFORMATION_SCHEMA.VIEWS
上创建的记录集中。此记录集的锁定类型设置为 LockType::adLockReadOnly
,这导致了问题。我已将其更改为 LockType::adLockOptimistic
,现在一切正常。
但是,提供者 returns 如此具有误导性的错误消息令人遗憾。