OdbcConnection in Multithreading

  • Thread starter Thread starter Miguel
  • Start date Start date
M

Miguel

Hey, Hi all friends!!!


I've got an application which executes an stored procedure
using an OdbcCommand object, but it has to executes
several stored procedures at the same time, so i decided
use multithreading, so that the application creates a
thread for each stored procedure and start it in this way

class ClassWithMethod
{
public void MyMethod()
{
//here stored procedure executes
}
}


class Test
{
static public Main (string[] args)
{
ClassWithMethod obj = new ClassWithMethod();
Thread myThread = new Thread (new ThreadStart
(obj.MyMethod));
myThread.Start();
}
}


But in the application a new thread is created each time
an event happens, and each time a thread is created the
application is creating a new OdbcConnection (ODBC 'cause
it uses a Sybase 10 database) and it is closing the
connection in the finally block of the method MyMethod
(describe before) but, to close the connection the
application is verifying that the connection it is gonna
close be an open connection, if it is not an open
connection it throws an exception, and my problem is that
this exception i mentioned is throwing in some cases, i.e.
just in some cases, i.e. in some thread which the
connection is trying to be closed, the application detects
that this connection of this thread is closed.


So, anyone can undarstand why is happennig this???
or is there a better or more efficcient way to manage
connections in multithreading????

Any help i will thank!!!


Regards!!!!
 
Miguel said:
Hey, Hi all friends!!!


I've got an application which executes an stored procedure
using an OdbcCommand object, but it has to executes
several stored procedures at the same time, so i decided
use multithreading, so that the application creates a
thread for each stored procedure and start it in this way

class ClassWithMethod
{
public void MyMethod()
{
//here stored procedure executes
}
}


class Test
{
static public Main (string[] args)
{
ClassWithMethod obj = new ClassWithMethod();
Thread myThread = new Thread (new ThreadStart
(obj.MyMethod));
myThread.Start();
}
}


But in the application a new thread is created each time
an event happens, and each time a thread is created the
application is creating a new OdbcConnection (ODBC 'cause
it uses a Sybase 10 database) and it is closing the
connection in the finally block of the method MyMethod
(describe before) but, to close the connection the
application is verifying that the connection it is gonna
close be an open connection, if it is not an open
connection it throws an exception, and my problem is that
this exception i mentioned is throwing in some cases, i.e.
just in some cases, i.e. in some thread which the
connection is trying to be closed, the application detects
that this connection of this thread is closed.


So, anyone can undarstand why is happennig this???
or is there a better or more efficcient way to manage
connections in multithreading????

Hard to tell without seeing the code. But I suspect that the connection
isn't open because it failed to open, probably because you had too many
connections open.

You should open the connection _outside_ your try block to avoid this.

con.Open()
try
{
. . .

}
finally
{
con.Close()
}

David
 
mmm, yes i'm opening the connection outside the try block
and outside the method, and the posibility you say about
too many connections, i dont think so becuase im trying
with only 2 thrads, i.e. its creating just 2 connections
according to my code.

ThanX David!!!
 
Back
Top