The comments below are my thoughts on threading and shared members. I think I have a pretty good understanding of shared members and thread safety. I am looking for feedback from the community as to whether or not my thoughts are valid.
Basically, to be thread safe means that the data being acted upon will only be effected by the current unit of work, i.e. the current thread. All other threads will be blocked from accessing the particular bit of code/memory until the current thread is done with it.
I also beleive that I read somewhere that data access adapers are not thread safe. I also read the following in the reference manual: "Use a DataAccessAdapter object solely per thread, and per connection. A DataAccessAdapter object contains 1 active connection and no thread-access scheduling code. This means that you need to create a new DataAccessAdapter object if you want to utilize in another thread a new connection and a new transaction or want to open a new connection."
After reading the statement above, I think it means that you **DO NOT **want to write code like this:
public class MyDB
private mDB as DataAccessAdapter
public sub new
mDB = New DataAccessAdapter(true)
end sub
public property Connection as DataAccessAdapter
get
return mDB
set
mDB = value
end property
end class
public class MyClientForm
dim myData as new MyDB
dim ent as new EmployeeEntity
ent.FirstName = Bob
myData.Connection.SaveEntity(ent)
end class
So what does this have to do with shared members? Well, it's my understanting that every .net type has a native interface. This is the basic definition of the class itself. Shared members are automatically available in memory without having an to create a specific instance of the type. I beleive that this is acheivable due to the native interface of the type.
One question is, since you will never have an instance specific object alive in a shared member, can you always assume that objects accessed in a shared member are thread safe and that shared members are thread safe?
For example, if I write a shared method like so:
shared sub SaveEntity(ent as IEntity2)
dim db as new DataAccessAdapter(false)
db.SaveEntity(ent)
end sub
and this method is hosted in a remote, server activated, single call object. Can client A and client B can call the saveEntity method safely at the same time without worring about mixing up the data?
Another thing that I find baffling about shared members is this. If I dont need to store statefule information in the BAL, and all members are service based, can all the members be shared?
Sometimes, object creation can take a long time, (i.e. it can take length of time to create an instance of an object like a connection), therefore you might want to put these objects into a thread pool, because after all, why waste time creating them, when you can have a pool of them ready to do your bidding. That being said, what are the implications of pooling an object with only shared members?
So the real basis for the question is, I cannot figure out what the best practices are for using shared members or not to. I have also been asked by my co-workers as to why we should or should not use shared members. Any insight into this would be appreciated.