Microsoft数据访问块和静态方法 [英] Microsoft Data Access Block and static methods

查看:63
本文介绍了Microsoft数据访问块和静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有 -


请原谅交叉发布......我不确定相应的新闻组

是什么意思。


我有一个问题,我不太确定该怎么问。据我所知,我的b $ b有完全错误的verbaige,但这里什么都没有...


我目前正在使用MS数据访问块用于桌面应用程序

我正在写作。最近,我不得不添加一个Web服务的调用,它在

本身调用一个mssql数据库。我曾经想过我会在客户端应用程序中使用

MS DAL,但后来发现自己很困惑

的东西:Web服务如何,连接到它的多个人

在任何给定时间,使用静态方法获取它的数据?


过去使用ASP.NET应用程序的经验,如果我有一个静态

变量由一个用户A声明并设置,用户B浏览到该页面

将看到由A.设置的新值。不是数据库

调用可能吗?如果用户A在用户B呼叫它的同时呼叫我的网络服务,

B可能无法获得A'的结果,反之亦然?


我认为这可能归结为我对静态变量如何工作的想法:我

查看MSDN文档并看到SqlCommand具有以下内容:

"这种类型的任何公共静态(在Visual Basic中共享)成员对于多线程操作都是安全的。任何实例成员都不是

保证是线程安全的。


我认为它完全相反 - 如果有多个线程

正在击中一个静态成员,这些结果可能会得到

返回给错误的用户,而在一个实例成员中,每个方法

被叫是一个单独的逻辑片段,所以没有结果被误导。


任何人都可以帮我解决这个问题吗?我不是编程世界的新手,

但是由于某些原因我发现这真的令人困惑。

提前谢谢!

Clint

Hey all -

Excuse the cross-post ... I''m not sure what the appropriate newsgroup
would be for this question.

I have a question that I''m not quite sure how to ask. For all I know, I
have the verbaige completely wrong, but here goes nothing ...

I''m currently using the MS Data Access Block for a desktop application
I''m writing. Recently, I had to add a call to a web service, which in
itself calls a mssql database. I had figured that I would just use the
MS DAL as I was in the client app, but then found myself confused about
something: How can a web service, with multiple people connecting to it
at any given time, use a static method to get it''s data?

In past experiences with ASP.NET applications, if I had a static
variable declared and set by one User A, User B browsing to that page
would see the new value set by A. Isn''t the same possible for database
calls? If User A calls my web service at the same time User B calls it,
isn''t it possible that B might get A''s result, or vice versa?

I think it might come down to my idea of how static variables work: I
look at the MSDN docs and see that SqlCommand has the following:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe."

I would figure it to be completely opposite - that if multiple threads
are hitting a static member, it''d be possible those results could get
returned to the wrong user, whereas in an instance member, each method
being called is a separate logical piece, so there''s no results being
misguided.

Can anyone help me out with this? I''m not new to the programming world,
but for some reason I''m finding this really confusing.
Thanks in advance!
Clint

推荐答案

DAB中的大多数方法都是无状态的。这些方法仅使用

传入参数和局部变量。您遇到的问题

ASP.NET开发使用静态变量来存储信息。

将其视为只有一个插槽。可以存储一张

数据,这样每个用户都可以看到单个插槽中的数据。


DAB中有一些静态字段(用于SqlParameter)缓存,如果

我记得),但是这些字段都有锁,以防止

并发线程搞砸了,以及数据(信息
$ b关于存储过程需要什么SqlParameters的$ b是全局的

对于每个用户。


有意义吗?


至于MSDN文档,他们说静态成员方法

被写成线程安全的,所以他们必须使用

适当的锁。 MS假设该框架将用于

多线程环境中,并且不能让静态方法不是b
线程安全。


话虽如此,实例成员并不安全,因为对象的单个

实例通常不会被多个

线程使用。例如,在ASP.NET中,您通常为每个用户请求创建一个

的实例 - 一个TextBox控件永远不会看到多个线程的
。由于锁会增加一些性能开销(以及很多开发和测试开销的b $ b),这个决定会产生很大的意义,即使它最初看起来有点违反直觉。


-

Scott
http://www.OdeToCode.com/blogs/scott/

2004年12月9日13:48:02 -0800,Clint < CJ ******* @ gmail.com>写道:
Most of the methods in the DAB are stateless. The methods use only the
incoming parameters and local variables. The problem you faced in
ASP.NET development was using a static variable to store information.
Think of it as having only one "slot" available to store a piece of
data so every user sees the data from the single slot.

There are a few static fields in the DAB (for SqlParameter caching, if
I remember), but these fields are guarded with locks to prevent
concurrent threads from screwing it up, and the data (information
about what SqlParameters are required by a stored procedure) is global
for every user.

Making any sense?

As for the MSDN docs, they are saying that the static member methods
are written to be thread safe, so they have to use locks where
appropriate. MS assumes the framework is going to be used in a
multithreaded environment and couldn''t let static methods not be
thread safe.

That being said, instance members are not safe because a single
instance of an object is typically not going to be used by multiple
threads. In ASP.NET for instance, you typically create an instance of
a class for each user request - a TextBox control will never see
multiple threads. Since locks add some performance overhead (and a lot
of development and testing overhead), the decision makes a great deal
of sense, even if it looks counterintuitive at first.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On 9 Dec 2004 13:48:02 -0800, "Clint" <cj*******@gmail.com> wrote:
嘿所有 -

请原谅...我不确定适当的新闻组
是什么意思这个问题。

我有一个问题,我不太确定该怎么问。据我所知,我确实完全错了,但是这里什么都没有......

我正在使用MS数据访问块用于桌面应用程序
我在写。最近,我不得不添加对Web服务的调用,该服务本身调用mssql数据库。我曾经想过我会在客户端应用程序中使用
MS DAL,但后来发现自己对于某些事情感到困惑:如何通过多人连接到网络服务在任何给定的时间,使用静态方法获取它的数据?

在过去的ASP.NET应用程序体验中,如果我有一个静态的变量声明并由一个设置用户A,用户B浏览到该页面
将看到由A.设置的新值。对于数据库
调用是否可能相同?如果用户A在用户B呼叫它的同时呼叫我的网络服务,那么B可能无法获得A'的结果,反之亦然?

我认为它可能归结为我对静态变量如何工作的看法:我看看MSDN文档并看到SqlCommand具有以下内容:

任何公共静态(在Visual Basic中共享)这种类型的成员对于多线程操作是安全的。任何实例成员都不保证是线程安全的。

我认为它完全相反 - 如果多个线程
击中静态成员,它'这些结果可能会被返回给错误的用户,而在一个实例成员中,被调用的每个方法都是一个单独的逻辑部分,所以没有结果是
被误导了。

任何人都可以帮我解决这个问题吗?我不是编程世界的新手,
但由于某种原因,我发现这真令人困惑。
提前致谢!
Clint
Hey all -

Excuse the cross-post ... I''m not sure what the appropriate newsgroup
would be for this question.

I have a question that I''m not quite sure how to ask. For all I know, I
have the verbaige completely wrong, but here goes nothing ...

I''m currently using the MS Data Access Block for a desktop application
I''m writing. Recently, I had to add a call to a web service, which in
itself calls a mssql database. I had figured that I would just use the
MS DAL as I was in the client app, but then found myself confused about
something: How can a web service, with multiple people connecting to it
at any given time, use a static method to get it''s data?

In past experiences with ASP.NET applications, if I had a static
variable declared and set by one User A, User B browsing to that page
would see the new value set by A. Isn''t the same possible for database
calls? If User A calls my web service at the same time User B calls it,
isn''t it possible that B might get A''s result, or vice versa?

I think it might come down to my idea of how static variables work: I
look at the MSDN docs and see that SqlCommand has the following:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe."

I would figure it to be completely opposite - that if multiple threads
are hitting a static member, it''d be possible those results could get
returned to the wrong user, whereas in an instance member, each method
being called is a separate logical piece, so there''s no results being
misguided.

Can anyone help me out with this? I''m not new to the programming world,
but for some reason I''m finding this really confusing.
Thanks in advance!
Clint






Clint,


几天前我有同样的问题 - 我写了一个快速的neato

样本。基本上我有一个静态方法的类,做了一个

console.writeline它所在的线程ID。然后我解雇了2个线程和

称为同一个类反复地让我惊讶的是静态方法

归还了我......不同的线程ID。非常热情,我写了另一个

样本,其中我在静态中放了5分钟的睡眠 - 好吧如果非常

相同的实例被共享,任何一个线程都会有等待5分钟

才能使用ONE COMMON实例 - ...和我的怀疑

...它没有!!!!这让我相信---


甚至在每个线程上都创建了静态方法 - 但是在线程中

是一个且只有一个实例。每个线程都获得它自己的私有副本

静态方法。


所以有 - 静态方法是线程安全的,这就是为什么着名的线 - "
Clint,

I had the very same question a few days back - and I wrote up a quick neato
sample. Basically I had a class with a static method, doing a
console.writeline of the thread id it was in. I then fired off 2 threads and
called the same class repeatedly and to my surprise the static method
returned me ... different thread ids. Very enthused, I wrote up another
sample in which I put a 5 minute sleep in the static - and well if the very
same instance was being shared either thread would have to wait 5 minutes
before being able to use the ONE COMMON instance - ... and to my suspection
... it didn''t !!!! Which led me to believe that ---

Even static methods are created on each thread - but within the thread there
is one and only one instance. Every thread gets it''s own private copy of a
static method.

So there - static methods are threadsafe which is why the famous line -- "
"此类型的任何公共静态(在Visual Basic中为Shared)成员对于多线程操作都是安全的。任何实例成员都不能保证是线程安全的。


"


实例成员的意思是什么?那意味着,如果你有

模块级静态方法,那么两个线程实际上将共享相同的

值,你需要互斥锁或其他一些方法来锁定访问那些 -

或者你可能会得到意想不到的值。但方法 - 很酷!模块

级变量并不酷。


所以继续使用DAB而不用担心静态问题:)


- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
http://blogs.apress.com/authors .php?author = Sahil Malik


" Clint" < CJ ******* @ gmail.com>在消息中写道

新闻:11 ********************* @ z14g2000cwz.googlegro ups.com ...嘿所有 -
我有一个问题,我是''我不太清楚怎么问。据我所知,我确实完全错了,但是这里什么都没有......

我正在使用MS数据访问块用于桌面应用程序
我在写。最近,我不得不添加对Web服务的调用,该服务本身调用mssql数据库。我曾经想过我会在客户端应用程序中使用
MS DAL,但后来发现自己对于某些事情感到困惑:如何通过多人连接到网络服务在任何给定的时间,使用静态方法获取它的数据?

在过去的ASP.NET应用程序体验中,如果我有一个静态的变量声明并由一个设置用户A,用户B浏览到该页面
将看到由A.设置的新值。对于数据库
调用是否可能相同?如果用户A在用户B呼叫它的同时呼叫我的网络服务,那么B可能无法获得A'的结果,反之亦然?

我认为它可能归结为我对静态变量如何工作的看法:我看看MSDN文档并看到SqlCommand具有以下内容:

任何公共静态(在Visual Basic中共享)这种类型的成员对于多线程操作是安全的。任何实例成员都不保证是线程安全的。

我认为它完全相反 - 如果多个线程
击中静态成员,它'这些结果可能会被返回给错误的用户,而在一个实例成员中,被调用的每个方法都是一个单独的逻辑部分,所以没有结果是
被误导了。

任何人都可以帮我解决这个问题吗?我不是编程世界的新手,
但由于某种原因,我发现这真令人困惑。
提前致谢!
Clint
"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe."
"

What do they mean by instance members? Well what that means is, if you have
module level static methods, then two threads will actually share the same
value, and you need mutexes or some other method to lock access to those -
or you might get unexpected values in them. But methods - are cool !! module
level variables are not cool.

So go ahead use the DAB without worrying about statics :)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
http://blogs.apress.com/authors.php?author=Sahil Malik


"Clint" <cj*******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... Hey all -

Excuse the cross-post ... I''m not sure what the appropriate newsgroup
would be for this question.

I have a question that I''m not quite sure how to ask. For all I know, I
have the verbaige completely wrong, but here goes nothing ...

I''m currently using the MS Data Access Block for a desktop application
I''m writing. Recently, I had to add a call to a web service, which in
itself calls a mssql database. I had figured that I would just use the
MS DAL as I was in the client app, but then found myself confused about
something: How can a web service, with multiple people connecting to it
at any given time, use a static method to get it''s data?

In past experiences with ASP.NET applications, if I had a static
variable declared and set by one User A, User B browsing to that page
would see the new value set by A. Isn''t the same possible for database
calls? If User A calls my web service at the same time User B calls it,
isn''t it possible that B might get A''s result, or vice versa?

I think it might come down to my idea of how static variables work: I
look at the MSDN docs and see that SqlCommand has the following:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations. Any instance members are not
guaranteed to be thread safe."

I would figure it to be completely opposite - that if multiple threads
are hitting a static member, it''d be possible those results could get
returned to the wrong user, whereas in an instance member, each method
being called is a separate logical piece, so there''s no results being
misguided.

Can anyone help me out with this? I''m not new to the programming world,
but for some reason I''m finding this really confusing.
Thanks in advance!
Clint



谢谢Sahil!


如果你不介意的话,我还会在这些问题上再提几个问题

已经回答了,只是为了确保我理解实际上是什么。

on。
Thanks Sahil!

If you don''t mind, I just have a few more questions on top of those
already answered, just to make sure I understand whats actually going
on.
甚至在每个线程上都创建了静态方法 - 但是在$ b内$ b线程只有一个实例。每个线程都获得它自己的私有副本
的静态方法。


让我们假装我有一个使用MDI表格的客户端应用程序

- 调用一个MDI子配置文件,另一个搜索。

那就是说,我有一个实用工具类,

class Foo

{

public static DataSet StaticDatabaseQueryMethod()

{

//做一些返回数据集的事情

}

}


在MDIChild配置文件中,一个计时器产生一个调用

Foo.StaticDatabaseQueryMethod()的线程,该线程将返回用户的配置文件。在

的同时,用户实际上正在查看Search MDI Child表单,

和搜索产生它自己的线程并执行搜索 - 再次

- 使用Foo.StaticDatabaseQueryMethod()。


如果我理解正确,我有三个线程在运行(主GUI,

配置文件查询和搜索查询)。因为这些是单独的

线程,这两个查询都可以访问静态Foo成员,并且

不会有任何数据被返回到错误形式的问题

(即,Profile的返回集转到Search,反之亦然)?

实例成员的含义是什么?那么这意味着,如果
你有模块级静态方法,那么两个线程实际上将共享
相同的值,你需要互斥锁或其他一些方法来锁定对
的访问权限 - 或者你可能会得到意想不到的值。但方法 - 很酷!
模块级变量并不酷。
Even static methods are created on each thread - but within the thread there is one and only one instance. Every thread gets it''s own private copy of a static method.
Let''s pretend for a second that I have a client app that uses MDI forms
- call one MDI Child Profile, the other Search.
That said, I have a utility class,
class Foo
{
public static DataSet StaticDatabaseQueryMethod()
{
// do something that returns a DataSet
}
}

In MDIChild Profile, a timer spawns a thread that calls
Foo.StaticDatabaseQueryMethod() that will return the user''s profile. At
the same time, the user is really looking at the Search MDI Child form,
and search that spawns it''s own thread and performs the search - again
- using Foo.StaticDatabaseQueryMethod().

If I''m understanding right, I have three threads running (Main GUI,
Profile query, and Search query). Because these are on separate
threads, the two queries can both access the static Foo member, and
will not have any issues with data being returned to the wrong form
(ie, Profile''s return set goes to Search, and vice versa)?
What do they mean by instance members? Well what that means is, if you have module level static methods, then two threads will actually share the same value, and you need mutexes or some other method to lock access to those - or you might get unexpected values in them. But methods - are cool !! module level variables are not cool.




换句话说,使用static for methods是可以的,但是对于字段是静态的

在课堂上不是吗?


另一个例子,对不起:)

class Foo

{

private static string fooField; //这是一个坏主意,并且

在访问时需要锁定(...)

私有静态字符串fooMethod(){} //这样做可以,并且

只要它被单独的线程调用就会返回正确的值

}


再次感谢!

克林特



In other words, using static for methods is ok, but static for fields
within a class isn''t?

Another example, sorry :)
class Foo
{
private static string fooField; // this is a bad idea, and will
require lock(...) when accessing
private static string fooMethod() {} // this is OK to do, and will
return correct values so long as it''s called by separate threads
}

Thanks again!
Clint


这篇关于Microsoft数据访问块和静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆