接下来,我们调用 Web 方法。因为我们将执行 T-SQL 批处理语句,所以代码如下所示:
#
# Invoking a sqlbatch to retrieve the number of connections
$soap -> on_action (sub { return '""';});
$method = SOAP::Data->name('sqlbatch')->attr({xmlns =>
'http://schemas.microsoft.com/sqlserver/2004/SOAP'});
@param = ( SOAP::Data->name(BatchCommands => 'select session_id,
net_transport, protocol_type from sys.dm_exec_connections'));
最后,我们分析 XML 响应以检索数据:
for my $node($doc->getElementsByTagName("row"))
{
print "\n";
for my $kid ($node->getChildNodes)
{
print $kid->getNodeName(); print ":: ";
for my $gkid ($kid->getChildNodes)
{
print $gkid->getNodeValue(); #print the actual values for the columns
}
print "\t";
}
print "\n";
}
运行该 Perl 脚本可以生成以下输出:
Testing SOAP::Lite client against AdventureWorks Contacts sample web service.
Calling sqlbatch
Server response...
Server response...
session_id:: 54 net_transport:: HTTP protocol_type:: SOAP
connection_id::5EC2B4E2-39A6-4FA7-BBDB-144DAED59A41
session_id:: 53 net_transport:: Shared memory protocol_type:: TSQL
connection_id:: 5AE50B7D-D919-4FBC-BA42-6069A12F4D30
session_id:: 53 net_transport:: Session protocol_type:: TSQL
connection_id::05830BE9-F12F-429D-BBAC-E4EEB2C528EF
parent_connection_id:: 5AE50B7D-D919-4FBC-BA42-6069A12F4D30
上述输出表明与 SQL Server 之间存在两个连接:一个连接使用二进制协议 TDS,并且显示为 protocol_type:: TSQL;另一个连接对应于在运行该 Perl 脚本时生成的 SOAP/HTTP 连接。
我希望将您的注意力引到 session_id 列上面。该会话标识符和与该请求关联的数据库引擎中的 spid(会话进程标识符)匹配。有两个条目的 spid 等于 53,因为一个对应于物理连接(net_transport 是共享内存),另一个对应于在同一物理连接上进行的逻辑会话。(有关多个活动结果集的详细信息,请参阅 Multiple Active Result Sets (MARS) in SQLServer 2。)该逻辑会话的 parent_connection_id 与物理连接匹配这一事实证明了这一点。对于 TDS 而言,连接和会话紧密联系在一起;换句话说,用户无法从不同的物理连接加入现有的会话。SOAP 访问使用户能够通过在请求中指定适当的会话标头来加入现有会话。在 SOAP 中使用多个会话这一主题需要专门撰文加以阐述。感兴趣的读者可以阅读 SQL Server Books Online 来获得有关如何启用和使用会话的详细信息。
