Click here to Skip to main content
15,893,998 members
Home / Discussions / C#
   

C#

 
General.NET remote object running locally Pin
dacris3-Apr-03 17:08
dacris3-Apr-03 17:08 
GeneralRe: .NET remote object running locally Pin
dacris4-Apr-03 2:05
dacris4-Apr-03 2:05 
Questionhow can get password and username in active directory? Pin
wangzhibin3-Apr-03 12:55
wangzhibin3-Apr-03 12:55 
Generalread binary registry key Pin
peterchen3-Apr-03 12:12
peterchen3-Apr-03 12:12 
GeneralRe: read binary registry key Pin
J. Dunlap4-Apr-03 7:08
J. Dunlap4-Apr-03 7:08 
GeneralProblems with ".Equals()" in listview code Pin
vlusardi3-Apr-03 12:08
vlusardi3-Apr-03 12:08 
GeneralCrystal Report distribution Pin
Mmithat3-Apr-03 12:05
Mmithat3-Apr-03 12:05 
GeneralConnection Gateway Pin
0siris3-Apr-03 11:49
0siris3-Apr-03 11:49 
I have been trying to develop a connection gateway which opens a fixed number of connections to a database using OLEDB in c#. This gateway keeps those XX connections opens and the idea is to have a thread/Session request a connection, use it, then return back to the queue. The request and return operations utilize a unique string called a SessionID to determine which connection to put back on the queue. Since this object will be accessed from several threads I have been trying to make it thread safe, but I have not developed far enough in thread programming to utilize this successfully. I am going to paste the code here and see if any of you can determine what is wrong with the code. Any help would be greatly appreciated.

Thank you. Cool | :cool:

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Threading;

namespace FritoLay.Data.Gateway
{
///
/// Summary description for ConnectionGateway.
///

public class ConnectionGateway : System.ComponentModel.Component
{
// Maximum Number of Database Connections
const int MAX_CONNECTIONS = 50;
// Maximum Number of Iterations in Sleep thread.
const int MAX_LOOPS = 60;

// Database Connection String
protected string connectionString = "";

// List of assigned connections with unique SessionID as Key
private System.Collections.SortedList connections = new System.Collections.SortedList(50);
// Queue of ready to use connections
private System.Collections.Queue connectionQueue = new System.Collections.Queue(50);
// Thread safe version of the assigned connections
private System.Collections.SortedList synchConnections = null;
// Thread safe version of the ready to use connections
private System.Collections.Queue synchQueue = null;

// Mutex to control threaded access to the ready to use connection queue
private System.Threading.Mutex mutex;

System.TimeSpan tSpan = new System.TimeSpan(0, 0, 0, 0, 100);

///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public ConnectionGateway(System.ComponentModel.IContainer container)
{
///
/// Required for Windows.Forms Class Composition Designer support
///

container.Add(this);
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.synchConnections = System.Collections.SortedList.Synchronized(this.connections);
this.synchQueue = System.Collections.Queue.Synchronized(this.connectionQueue);
this.mutex = new System.Threading.Mutex();
}

public ConnectionGateway()
{
///
/// Required for Windows.Forms Class Composition Designer support
///

InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
this.synchConnections = System.Collections.SortedList.Synchronized(this.connections);
this.synchQueue = System.Collections.Queue.Synchronized(this.connectionQueue);
this.mutex = new System.Threading.Mutex();
}

#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

public string ConnectionString
{
get { return this.connectionString; }
set { this.connectionString = value; }
}

public void Initialize()
{
if(this.connectionString != "")
{
for(int i = 0; i < ConnectionGateway.MAX_CONNECTIONS; i++)
{
System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(this.connectionString);
cnn.Open();
this.connectionQueue.Enqueue(cnn);
}
}
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public System.Data.OleDb.OleDbConnection GetConnection(string SessionID)
{
lock(this.connections)
{
try
{
//if(this.connectionQueue.Count == 0)
//{
//System.Threading.ThreadStart ts = new System.Threading.ThreadStart(this.Sleep);
//System.Threading.Thread thread = new System.Threading.Thread(ts);
//thread.Name = "Sleeper";
//thread.Start();
//thread.Join();

//}
int counter = 0;
while(this.connections.Count == ConnectionGateway.MAX_CONNECTIONS)
{
Monitor.Wait(this.connections, tSpan);
Monitor.Pulse(this.connections);

if(counter > ConnectionGateway.MAX_LOOPS)
break;
counter++;
}

if(this.connectionQueue.Count == 0)
{
//this.mutex.ReleaseMutex();
throw new FritoLay.Data.Gateway.TimeOutException("No Available Connections");
}

System.Data.OleDb.OleDbConnection conn = (System.Data.OleDb.OleDbConnection)this.synchQueue.Dequeue();

this.synchConnections.Add(SessionID, conn);

return conn;
}
catch(System.Exception ex)
{
throw ex;
}
finally
{
}
}
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void ReturnConnection(string SessionID)
{
lock(this.connections)
{
try
{
System.Data.OleDb.OleDbConnection conn = (System.Data.OleDb.OleDbConnection)this.synchConnections.GetByIndex(this.connections.IndexOfKey(SessionID));

if(conn.State == System.Data.ConnectionState.Closed)
{
conn.ConnectionString = this.connectionString;
conn.Open();
}
if(conn.ConnectionString != this.connectionString)
{
conn.Close();
conn.ConnectionString = this.connectionString;
conn.Open();
}

this.synchQueue.Enqueue(conn);
this.synchConnections.Remove(SessionID);
}
catch(System.Exception ex)
{
throw ex;
}
finally
{
}
}
}

private void Sleep()
{
try
{
this.mutex.WaitOne();

int counter = 0;
while(counter <= ConnectionGateway.MAX_LOOPS)
{
Thread.Sleep(this.tSpan);
//Monitor.Wait(this.connectionQueue, tSpan);

if(this.connectionQueue.Count > 0)
{
Thread.CurrentThread.Abort();
break;
}

counter++;
}
}
catch(System.Exception ex)
{
}
finally
{
this.mutex.ReleaseMutex();
}

}

public void Terminate()
{
if(this.connectionQueue.Count > 0)
{
for(int i = 0; i < this.connectionQueue.Count; i++)
{
System.Data.OleDb.OleDbConnection conn = (System.Data.OleDb.OleDbConnection)this.connectionQueue.Dequeue();
conn.Close();
conn = null;
}
}
this.connectionQueue.Clear();

if(this.connections.Count > 0)
{
System.Collections.IEnumerator ie = this.connections.GetEnumerator();
while(ie.MoveNext())
{
System.Collections.DictionaryEntry de = (System.Collections.DictionaryEntry)ie.Current;
System.Data.OleDb.OleDbConnection conn = (System.Data.OleDb.OleDbConnection)de.Value;
conn.Close();
conn = null;
}
}
}
}
}
QuestionTreeView and right click select? Pin
se99ts3-Apr-03 10:17
se99ts3-Apr-03 10:17 
AnswerRe: TreeView and right click select? Pin
mikasa3-Apr-03 11:02
mikasa3-Apr-03 11:02 
AnswerRe: TreeView and right click select? Pin
Leon van Wyk6-Apr-03 11:22
professionalLeon van Wyk6-Apr-03 11:22 
GeneralCalling C# function within javascript Pin
gekoscan3-Apr-03 8:26
gekoscan3-Apr-03 8:26 
GeneralRe: Calling C# function within javascript Pin
David Stone3-Apr-03 9:33
sitebuilderDavid Stone3-Apr-03 9:33 
GeneralProgramming Crystal Report that as Web Service... Pin
frontad3-Apr-03 8:21
frontad3-Apr-03 8:21 
GeneralMS Officeto XML Pin
theJazzyBrain3-Apr-03 8:04
theJazzyBrain3-Apr-03 8:04 
GeneralRe: MS Officeto XML Pin
Feng Qin4-Apr-03 2:13
Feng Qin4-Apr-03 2:13 
GeneralRe: MS Officeto XML Pin
theJazzyBrain6-Apr-03 21:56
theJazzyBrain6-Apr-03 21:56 
GeneralRe: MS Officeto XML Pin
Feng Qin7-Apr-03 18:11
Feng Qin7-Apr-03 18:11 
QuestionHow do you apply attributes to parameters? Pin
Kevin McFarlane3-Apr-03 6:30
Kevin McFarlane3-Apr-03 6:30 
AnswerRe: How do you apply attributes to parameters? Pin
David Stone3-Apr-03 7:20
sitebuilderDavid Stone3-Apr-03 7:20 
GeneralRe: How do you apply attributes to parameters? Pin
Kevin McFarlane3-Apr-03 10:23
Kevin McFarlane3-Apr-03 10:23 
GeneralRe: How do you apply attributes to parameters? Pin
David Stone3-Apr-03 13:40
sitebuilderDavid Stone3-Apr-03 13:40 
GeneralC# / XML attributes and values Pin
econner3-Apr-03 2:44
econner3-Apr-03 2:44 
GeneralRe: C# / XML attributes and values Pin
Oleksandr Kucherenko3-Apr-03 4:28
Oleksandr Kucherenko3-Apr-03 4:28 
GeneralRe: C# / XML attributes and values Pin
econner3-Apr-03 6:54
econner3-Apr-03 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.