473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

multi threaded database access...

hey, I have 2 threads, th and th2, both of them run a method.

each of these 2 methods requires database access.

sometimes I get an error, that database requires an open connection, and
that the current connection state is "connecting".

is this because both threads are trying to use the same database???

they use the same public static data access class. would this be why? isnt
there some way I can avoid this?

thanks.
May 7 '07 #1
7 4798
You cannot do this with a public static database access thread. You need to
have each thread open its own database connection, do the work, then
immediately close the connection and allow it to return to the ADO.NET
connection pool.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rogelio" wrote:
hey, I have 2 threads, th and th2, both of them run a method.

each of these 2 methods requires database access.

sometimes I get an error, that database requires an open connection, and
that the current connection state is "connecting".

is this because both threads are trying to use the same database???

they use the same public static data access class. would this be why? isnt
there some way I can avoid this?

thanks.
May 7 '07 #2
so the best solution would be to make 2 static database access classes? then
I can have each thread use their own public static class.

I tried making the methods in the class public instead of public static, and
making 2 instances of the class and I still get the error.

"Peter Bromberg [C# MVP]" wrote:
You cannot do this with a public static database access thread. You need to
have each thread open its own database connection, do the work, then
immediately close the connection and allow it to return to the ADO.NET
connection pool.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rogelio" wrote:
hey, I have 2 threads, th and th2, both of them run a method.

each of these 2 methods requires database access.

sometimes I get an error, that database requires an open connection, and
that the current connection state is "connecting".

is this because both threads are trying to use the same database???

they use the same public static data access class. would this be why? isnt
there some way I can avoid this?

thanks.
May 7 '07 #3
I get the feeling you are using the same Connection instance between the
two threads. You don't want to do this. Rather, you want to create the
instance of the Connection when you need it, use it, and then close it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rogelio" <Ro*****@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
so the best solution would be to make 2 static database access classes?
then
I can have each thread use their own public static class.

I tried making the methods in the class public instead of public static,
and
making 2 instances of the class and I still get the error.

"Peter Bromberg [C# MVP]" wrote:
>You cannot do this with a public static database access thread. You need
to
have each thread open its own database connection, do the work, then
immediately close the connection and allow it to return to the ADO.NET
connection pool.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Rogelio" wrote:
hey, I have 2 threads, th and th2, both of them run a method.

each of these 2 methods requires database access.

sometimes I get an error, that database requires an open connection,
and
that the current connection state is "connecting".

is this because both threads are trying to use the same database???

they use the same public static data access class. would this be why?
isnt
there some way I can avoid this?

thanks.

May 7 '07 #4

"Rogelio" <Ro*****@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
so the best solution would be to make 2 static database access classes?
then
I can have each thread use their own public static class.

I tried making the methods in the class public instead of public static,
and
making 2 instances of the class and I still get the error.
Making a static method to do what? You simply use the ADO.Net Using
Statement to open the database connection, do the database access, and let
the Using statement close the connection or you close the connection with a
try/catch/finally.

You do all of this with inline code and in your case for each thread that
needs that SQL access.

You don't need some static method to do this nor is it warranted, as being
shown in the examples of how to open a database connection, access the
database, and close the database when finished, each time the code is
executed.

You don't have to use the ADO.Net USING statement but it is nice.

http://www.sql-server-performance.co...ansactions.asp
May 7 '07 #5
I fixed it. I had made a private static SqlConnection at the top of the
class, then in each method open and closing it like this:

class1
{
SqlConnection SqlCon = new SqlConnectio();

public static void DoDBStuff()
{
SqlCon = new SqlConnectio(ConString);

SqlCon.Open()
sql command stuff
SqlCon.Close()
}
}//end class

this was using the same SqlConnection instance every time.

I simply had to change my code in each method, to make a new instance for
each method. like this

class1
{
//no definitions here

public static void DoDBStuff()
{
SqlConnection SqlCon = new SqlConnectio(ConString);
SqlCon.Open();
sql command stuff
SqlCon.Close();
}
}//end class

thanks guys.

"Mr. Arnold" wrote:
>
"Rogelio" <Ro*****@discussions.microsoft.comwrote in message
news:6E**********************************@microsof t.com...
so the best solution would be to make 2 static database access classes?
then
I can have each thread use their own public static class.

I tried making the methods in the class public instead of public static,
and
making 2 instances of the class and I still get the error.

Making a static method to do what? You simply use the ADO.Net Using
Statement to open the database connection, do the database access, and let
the Using statement close the connection or you close the connection with a
try/catch/finally.

You do all of this with inline code and in your case for each thread that
needs that SQL access.

You don't need some static method to do this nor is it warranted, as being
shown in the examples of how to open a database connection, access the
database, and close the database when finished, each time the code is
executed.

You don't have to use the ADO.Net USING statement but it is nice.

http://www.sql-server-performance.co...ansactions.asp

May 7 '07 #6

"Rogelio" <Ro*****@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
>I fixed it. I had made a private static SqlConnection at the top of the
class, then in each method open and closing it like this:

class1
{
SqlConnection SqlCon = new SqlConnectio();

public static void DoDBStuff()
{
SqlCon = new SqlConnectio(ConString);

SqlCon.Open()
sql command stuff
SqlCon.Close()
}
}//end class

this was using the same SqlConnection instance every time.

I simply had to change my code in each method, to make a new instance for
each method. like this

class1
{
//no definitions here

public static void DoDBStuff()
{
SqlConnection SqlCon = new SqlConnectio(ConString);
SqlCon.Open();
sql command stuff
SqlCon.Close();
}
}//end class

thanks guys.
You ever read about a try/catch block?

May 8 '07 #7
that wasnt my actual source code. it was a representation of my code. of
course I have try catch's. why you so rude about it anyways.
You ever read about a try/catch block?
May 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
0
by: David | last post by:
I've installed the DBD::Proxy and RPC::PLServer modules on Windows 2000 accessing SQL Server 2000 (through an ODBC system DSN). I can interface to the database from one or more Linux boxes just...
0
by: Ganbold | last post by:
Hi, I'm new to multi-threaded programming and reading the book "Programming with POSIX Threads" and trying to understand concepts and coding. What I'm trying to do is to rewrite mysql client...
5
by: Rob Durant | last post by:
Hi, I have a multi-threaded application (have also tried as service - same behaviour) that runs fine on XP, but not on 2003. Symptoms are: Threads are started normally, locks acquired and...
5
by: Duncan Mole | last post by:
Hi all, Does anyone know of a way to achieve multi-theaded platform invokes? I have been taking quite a performance hit using the syncronised attribute: . Cheers ---
9
by: Seenu | last post by:
Is it safe to create a static function in a multi threaded application ? As an example, I have a function that logs errors ,exceptions and some informational data public static void Log(....) {...
9
by: Stu | last post by:
Hi, I have a web service that does database access and calculations However it keeps falling over becuase its not multi threaded and each call to it is not in its own space!! I dont want to use...
5
by: Macca | last post by:
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...
0
by: JeffJones176 | last post by:
Hello All: I am very new to multi-threaded programming and have a need (I think) to write an application that uses multi-threaded pages. My scenario follows: My business has multiple...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
As far as I know, the C Standard has no mention of multi-threaded programming; it has no mention of how to achieve multi-threaded programming, nor does it mention whether the language or its...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.