Pool Connection UniDirect

Pool Connection UniDirect

Postby erik78 » Thu 14 Feb 2008 10:28

Hello everyone,

I have some questions with pool connection in UniDirect .NET.

I am a software engineer of Spain, and my company buy UniDirect Component recently.

I use it for connect _Oracle and SQLServer databases, and I can't understand how to use the pool connection.

In documents, I can read how to active the pool (in connection string, specifing Max Pool, Min Pool, Connection LifeTime,...). But, I tested using threads, and, in the second thread execution, produces an Exception ("type Internal Connection fatal"). I don't know the why of this.

Another questions, I wanted to know the number of active connection in one specific moment. How can I do this?

I paste the code of test program in c#:

using System;
using System.Text;
using System.Threading;

using CoreLab.UniDirect;

namespace BBDDPool_SQL
{
class Program
{
public static UniConnection vConnectionString;
static Thread thOpen1, thOpen2;


static void Main(string[] args)
{
try
{
//create connection
vConnectionString=new UniConnection("Provider=SQL
Server;server=...;initial catalog=...;user
id=...;password=...;Pooling=true;Max Pool Size=4;Min Pool
Size=1;Connection Lifetime=130;");

//open first connection
vConnectionString.Open();
Console.WriteLine("open connection");

//create and run threads
thOpen1 = new Thread(new ThreadStart(OpenConnection));
thOpen2 = new Thread(new ThreadStart(OpenConnection));
thOpen1.Start();
thOpen2.Start();


Console.ReadLine();
}
catch (Exception ex)
{

}
}

public static void OpenConnection()
{
try
{
vConnectionString.Open();
UniCommand oCommand =
vConnectionString.CreateCommand();

oCommand.CommandText = "select * from ...";

UniDataReader oRecordset = oCommand.ExecuteReader();
Console.WriteLine("open connection");
}
catch (Exception ex)
{

}

}
}
}

Thanks a lot.

ERIK.
erik78
 
Posts: 23
Joined: Thu 14 Feb 2008 10:11

Postby Alexey.mdr » Fri 15 Feb 2008 17:42

UniConnection is not thread safe. You cannot use one connection in several threads.
You need to create a new connection within a new thread, or use synchronisation objects.

But actually connection pooling has nothing to do with multithreading.
Connection goes to the pool only if it is closed.
Next time you want to open a connection, it will be taken from the pool and the system will save time on the connection creating.
Alexey.mdr
 
Posts: 729
Joined: Thu 13 Dec 2007 10:24


Return to dotConnect Universal