473,651 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading and a database connections.

jt
The program works like this:
There is a form with a button.
When the form is loaded, a separate thread is started which is
retreiving/updating data in the database every x seconds.
When clicked on the button, data is retrieved from the database.

This looks to work fine. However, sometimes after clicking on the butten to
retrieve the data i got an error message (on the separate thread):
"The connection is already Open"

After changing the query that is executed when the button is clicked to a
more extensive query, the error is popping up every time.

The database is an Access database.
Each tread has it's own connection.
Each tread opens and closes its own connection.

Who can help me?
Jan 2 '06 #1
10 4567
Check the following:
1. Are you closing your connections in the Finally block?
2. If you are disposing some objects in the Finally block, check if
they exist or not; If Not Object Is Nothing Then Object.Dispose
3. Throw all your exceptions-it should not happen that an error happens
in some function and the program moves ahead.
Update commands would always apply a lock on the table, however,
select commands should not, so use WITH NOLOCK in all select queries.

Sankalp

Jan 2 '06 #2
we had the same problem of being that
"The Connection is already open"

Even we r very new to .Net but we could manage to get the solution.

Actually we created a common class for all the methods and in that
constructor only
we hav defined the open connection for the database which led to many
problems.

the solution is that u hav to create a new configuration file (ie.
..config file).
and also create the unique class which is only meant for the opening of
the connection.

and open the connection were ever u need and after the transaction
,then immediately
close the connection.this is a great help when we use datareaders

try this even for urs if it works then fine.

Jan 3 '06 #3
jt
Hello, thank you for your replay, however, this is not the answer i was
looking for.
the error should not occur, so i would like to know why i have this problem.
Of course, putting it all in a try and catch routine is a work around, but
that is not what i want.
Any more suggestions?

"Sankalp" wrote:
Check the following:
1. Are you closing your connections in the Finally block?
2. If you are disposing some objects in the Finally block, check if
they exist or not; If Not Object Is Nothing Then Object.Dispose
3. Throw all your exceptions-it should not happen that an error happens
in some function and the program moves ahead.
Update commands would always apply a lock on the table, however,
select commands should not, so use WITH NOLOCK in all select queries.

Sankalp

Jan 3 '06 #4
jt
Hello, thank you for your replay.
What you did is right, but because i'm using 2 threads, this is not answer
to the problem.

"Runni" wrote:
we had the same problem of being that
"The Connection is already open"

Even we r very new to .Net but we could manage to get the solution.

Actually we created a common class for all the methods and in that
constructor only
we hav defined the open connection for the database which led to many
problems.

the solution is that u hav to create a new configuration file (ie.
..config file).
and also create the unique class which is only meant for the opening of
the connection.

and open the connection were ever u need and after the transaction
,then immediately
close the connection.this is a great help when we use datareaders

try this even for urs if it works then fine.

Jan 3 '06 #5
You got it all wrong-I asked you to close all your connections in the
finally block and not suppress your errors by wrongly using a
Try-Catch.:-D

"Connection already open" - the only thing that can be inferred from
this your connections are not getting closed.

Adding a data layer or using DataAdapter and 'rightly' using Finally
will also resolve your problem of making sure all your connections get
closed.

Jan 3 '06 #6
jt
Hello, the connections are getting closed, the problem is in the treading.
I would exepct that eacht tread has it's own connection. But i looks like
this is not the case, and the 2 treads use the same connenction.

Routine 1:
1: open connection1
2: execute query
3: close connection1

Routine 2:
1: open connection2
2: execute query
3: close connection2

Repeating these routines on one thread does not result in an error.
Repeating each routine on one threads will eventualy result in an error.
Why is the connection of routine 1 interfearing with the connection of
routine 2?
"Sankalp" wrote:
You got it all wrong-I asked you to close all your connections in the
finally block and not suppress your errors by wrongly using a
Try-Catch.:-D

"Connection already open" - the only thing that can be inferred from
this your connections are not getting closed.

Adding a data layer or using DataAdapter and 'rightly' using Finally
will also resolve your problem of making sure all your connections get
closed.

Jan 3 '06 #7
I"m new to DataBase access and have a question. During an Update, you say
the database is locked. If I try to access it with a query while it's
locked, won't it wait until the database is unlocked to execute the query or
do I have to put the query in a loop until it's executed? Thanks for any
help you can give me to understand this.
--
Dennis in Houston
"Sankalp" wrote:
Check the following:
1. Are you closing your connections in the Finally block?
2. If you are disposing some objects in the Finally block, check if
they exist or not; If Not Object Is Nothing Then Object.Dispose
3. Throw all your exceptions-it should not happen that an error happens
in some function and the program moves ahead.
Update commands would always apply a lock on the table, however,
select commands should not, so use WITH NOLOCK in all select queries.

Sankalp

Jan 4 '06 #8
In SQL Server 2000, if you execute a query affecting a row, then that
particular row will be locked so that no other query can access the
same row till the transaction completes. The other rows are available
to update queries. This is row-level locking. Similarly, if the update
query will be affecting multiple rows, SQL Server applies a lock
depending upon the memory available. If these rows are present in
contiguous pages, then SQL Server might lock those pages, extents or
even the entire table. If pages are locked, then the rest of the rows
in other data pages can be accessed. If the entire table is locked,
then the other simultaneous queries will have to wait until the
transaction is completed. So, as you can see SQL Server chooses the
type of locking for each transaction-it all depends on the memory
availability. If an update query is being executed, the select query
will have to wait till the update transaction completes.

However, SQL does give us some kind of control over the type of
locking. You can still specify a ROWLOCK in an update query. Similary
a select query applies a read lock on the tables. However, you can
specify not to let this happen by using WITH NOLOCK in your select
queries.

This is not the end of story-there is something called Transaction
Isolation Levels. Refer to the SQL Server Books online and you can read
in detail about them.

There is a Microsoft fix to the problem when SQL Server applies a lock
when an Update query is fired and a Select query is fired at the same
time. As per this fix, you experience this problem when you run a
SELECT statement that uses the TOP clause and the READPAST locking
hint.

Refer to this: http://support.microsoft.com/?kbid=867746

HTH.
Sankalp

Jan 4 '06 #9
Let me have a look at the code you have written in the thread event-the
event handler which gets fired by your threads, the way you have
declared your connection object, they way you open database
connections.
As you said, each thread will be starting its own connection. Think of
them as separate pieces of code-when you create a NEW connection
object, and the other thread also creates a NEW connection object, the
previous thread has got nothing to do with it. They are separate
objects created in the managed pool and its your softwares
responsibility to return them back to the connection pool when you dont
need them. This is where you HAVE to close connections in a FINALLY
block after checking whether your application was able to open them in
the first place or not.
The ideal way to open connections is to use connection pooling as that
will increase the performance of your application greatly and more so
when you have a multithreaded application. The .NET framework
automatically creates a connection pool for you for each unique
connection string. But this is not the point being discussed here.

Sankalp

Jan 4 '06 #10

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

Similar topics

14
1786
by: comp.lang.php | last post by:
I've heard numerous and varied commentaries here and on other fora regarding PHP and the concept of threads. Coming from a Java background I understand how threads benefit to prevent collisions, all the while carefully written to avoid "race conditions" while a web application is being utilized in a multi-user environment. However, are there examples in PHP alone where this same technology is possible? I am faced with having to deal...
2
2062
by: F. GEIGER | last post by:
In my wxPython-app a part of it gathers data, when a button is pressed, and stores it into a db. The GUI part should display the stuff being stored in the db. When both parts work on the same connection, I get "SQL statements in progress errors". Seems ok to me, you can't do that. So, next step: Both parts get a separate connection. Now I get "Database locked" errors. Hmm, yes, it's GUI stuff after all, being all in the same
2
2450
by: Sophia Cao | last post by:
Hello, I am seeking a python solution for my project. I am trying to implement an architecture where there is a server who receives incoming messages from several clients, then those messages need to be written to a MySQL database. To avoid too many connections to the database, I plan to save the incoming messages into a queue, while use a seperate process/thread to constantly monitoring the queue and storing the messages to the...
0
1038
by: Mike Caputo | last post by:
I'm connecting to a DB on a dual-processor 2.8 Ghz server with 2.5 gig RAM. So I've got plenty of muscle, and I'd like to do double-time on one update command. The command calls a stored procedure that receives two variables. I've got the threading class set up, but I keep running into a problem with the connection. My threading class copies the command and creates a new connection for it using the main connection's connect string, but...
11
2146
by: # Cyrille37 # | last post by:
Hello all, I come to you to getting help for managing multi threading and database connection. My project use Xml-Rpc to receive messages, so each call come from a different thread. Incoming calls are executing SQL on a MysqlConnection. MysqlConnection does not like when concurents calls appends. For a fast and dirty solution, I've put a Monitor() at messages arrived.
3
238
by: arun.hallan | last post by:
I've read up on threading but it's confusing me somewhat. The functionality i need is as so: I have my main method which calls two other methods in two different classes. Each of these methods involves waiting for database operation to occur, so would be better firing these two methods off at the same time, i.e. in two threads.
1
1163
by: Philip Zigoris | last post by:
Hi all, I have written a socket based service in python and under fairly heavy traffic it performs really well. But i have encountered the following problem: when the system runs out of file descriptors, it seems to stop switching control between threads. Here is some more detail: The system has n+2 threads, where n is usually around 10. This was
1
426
by: Robert.R.Emmel | last post by:
Hello, I am using the threading module and the Queue module in python to to send out shipment tracking URL requests. Is there a way to timeout a thread within a Queue? I think the way I have it now the thread will wait until something is returned and will basically wait forever for that something.
1
5682
by: Pradip | last post by:
Hello every body. I am new to this forum and also in Python. Read many things about multi threading in python. But still having problem. I am using Django Framework with Python having PostgreSQL as backend database with Linux OS. My applications are long running. I am using threading. The problem I am facing is that the connections that are being created for database(postgres) update are not getting closed even though my threads had...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.