Multi threaded app database access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a multithreaded app which now needs database storage.

I am in the process of designing my Data Access Layer but and was wondering
what issues I should look for for in regards to a multi threaded app.

My app will be need to initiate access to the database from several places
in the Business logic. I'd appreaciate any advice/suggesstions on best
practices to avoid problems such as accessing the same method in the data
access layer form different parts of the business logic at the same time etc,

Thanks In Advance
Andy
 
I have a multithreaded app which now needs database storage.
I am in the process of designing my Data Access Layer but and was wondering
what issues I should look for for in regards to a multi threaded app.

Performance, resources, scalability. Look at connection pools and thread pool
My app will be need to initiate access to the database from several places
in the Business logic. I'd appreaciate any advice/suggesstions on best
practices to avoid problems such as accessing the same method in the data
access layer form different parts of the business logic at the same time etc,

It's not very good solution to block table from the business logic
perspective.
DBs have concept of ACID see there
http://www.service-architecture.com/database/articles/acid_properties.html or
in MSDN that allow to specyfy which level of isolation do u need to apply for
your tables. Scrutinize DB manuals to get more info about it

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi,

I am in the process of designing my Data Access Layer but and was
wondering
what issues I should look for for in regards to a multi threaded app.

A web app with DB access faces the very same issues, you could read some
articles about how to implement DB access in the web.
My app will be need to initiate access to the database from several places
in the Business logic. I'd appreaciate any advice/suggesstions on best
practices to avoid problems such as accessing the same method in the data
access layer form different parts of the business logic at the same time
etc,

I would keep the DB access class as a stateless class, each method receive a
ICommand that just execute using the selected way (
ExecuteReader/NonQuery/Scalar ). Also each method will create its own
connection.
 
Hi Ignacio,

Wouldn't I have to lock the methods in the database access class while in
use to stop them being called from elsewhere in the business logic?

Regards
Macca
 
Hi,

Macca said:
Hi Ignacio,

Wouldn't I have to lock the methods in the database access class while in
use to stop them being called from elsewhere in the business logic?

No, unless the DB class is using shared resources, which you should not.

Here is an example, this class deal with the DB comm, it does receive a
SqlCommand (which is built by the BO ).

As you can see none method use any shared resources, and a connection is
created and closed inside the method.

public class DataProvider
{
static string connString;
public static string ConnString
{
get{ return connString;}
set
{
connString = value;
}
}


static public object ExecuteScalar(SqlCommand command)
{
object returnvalue = null;
if ( connString == "")
throw new Exception("No connection string defined");
SqlConnection conn = new SqlConnection( connString);
try
{
conn.Open();
command.Connection = conn;
returnvalue = command.ExecuteScalar();

}
catch( Exception e)
{
throw new Exception("Error ocurred in ExecuteScalar: commandtext: " +
command.CommandText, e);
}
finally
{
conn.Close();
}
return returnvalue;

}
static public SqlDataReader ExecuteReader(SqlCommand command)
{}
 
Your tsql procs may need special attention. I think a reasonable design is
to have all updates in sprocs and have selects in client code and/or in
sprocs. Most of the sql will automatically handle sync within the context
of the sql statement transaction. Sometimes, for special procs like a "test
and set" you may need to ensure correct atomic behavior with other tsql
symantics. But that is all on the sql end. Your DAL could probably be a
static class with all static methods, so you should not need any special
synchronization in general. Naturally, this may not always be true and you
may need some shared object(s) that will need to be sync'd with a lock, etc.
It all depends on what your doing.

--
William Stacey [MVP]

| Hi Ignacio,
|
| Wouldn't I have to lock the methods in the database access class while in
| use to stop them being called from elsewhere in the business logic?
|
| Regards
| Macca
|
| "Ignacio Machin ( .NET/ C# MVP )" wrote:
|
| > Hi,
| >
| > | >
| > > I am in the process of designing my Data Access Layer but and was
| > > wondering
| > > what issues I should look for for in regards to a multi threaded app.
| >
| > A web app with DB access faces the very same issues, you could read
some
| > articles about how to implement DB access in the web.
| >
| > > My app will be need to initiate access to the database from several
places
| > > in the Business logic. I'd appreaciate any advice/suggesstions on best
| > > practices to avoid problems such as accessing the same method in the
data
| > > access layer form different parts of the business logic at the same
time
| > > etc,
| >
| > I would keep the DB access class as a stateless class, each method
receive a
| > ICommand that just execute using the selected way (
| > ExecuteReader/NonQuery/Scalar ). Also each method will create its own
| > connection.
| >
| >
| >
| > --
| > Ignacio Machin,
| > ignacio.machin AT dot.state.fl.us
| > Florida Department Of Transportation
| >
| >
| >
 
Back
Top