Dear all:
I develop a web application in VS2005. In order to reuse data access functionality and domain knowledge in other projects, I seperate data access functionality and domain knowledge into two different liraries name DataFarm and EPMLibrary.
DataFarm is used to deal with all database access jobs. And EPMLibrary is used to take care all project management logics and entities.
In order to hide data access details into EPMLibrary, I define a class in EPMLibrary like this
public class Configuration
{
[ThreadStatic]
public static DataFarm myFarm;static Configuration()
{
myFarm = new DataFarm();
}
}
When I use my EPMLibrary in my web project, I add database connection in Application_Start and it works fine until now. But when I use some of my EPMLibrary objects, a null reference exception occurs usually but not regularly when the object needs operate with database.
Could any one teach me how to solve this problem and why it happens?
Great idea, however I have a few questions:
When you use the database you need to open a connection, use it and return it to the pool, each time you open a connection you create an object in the DataFarm(), without seen your code, why are you using static and [ThreadStatic]? Are you calling it from one place or many places in the app?
Can you post your code to check what are you doing in the connection and how you keep the connection?
Cheers
Al
First:
Beacuse the code is too long. I explain my design here.
The DataFarm class contains and maintains a collection of database connections, and it provides a set of execution commands for developers to do database operations.
Int32 DoCommand(Command cmd);
Int32 DoCommand(Command cmd, String connectionName);
Int32 DoCommand(IEnumerable cmdSet);
Int32 DoCommand(IEnumerable cmdSet, String connectionName);
.....
I list part of my function signature here, there actually exist a lot of overloaded function for DoCommand, and a set of DoSelect. The type "Command" is defined by myself to provide equivalent functionality with SqlCommand and OleDbCommand, and can be extended to support other database type.
The open, close and transaction are maintained in these functions.
Second:
Beacuse I want that all users use the same copy of DataFarm instance, so I use [ThreadStatic] to ensure there is only one copy in my web application.
You said:
Beacuse I want that all users use the same copy of DataFarm instance, so I use [ThreadStatic] to ensure there is only one copy in my web application.
Then if that's the function that opens a connectionl, the problem will be the each user override each other, please remove the static part and give it a try.
Hope this helps
Al
0 comments:
Post a Comment