Publish date: 2003-04-23
Connection Pooling (C#, VB.Net)
using System;
using System.Data;
using System.Data.OleDb;
namespace CSharp
{
/// <summary>
/// This sample demonstrates how to organize the pool of connections using OLE DB (IBProvider)
/// </summary>
class ADONetSample_ConnectionPooling
{
[STAThread]
static void Main(string[] args)
{
try
{
String connString;
// Specification in the connection string:
// Please note: Pooling is implicit, you automatically get it unless you disable it.
// Therefore, "true" is the default for the pooling keyword (pooling=true).
// Connection Reset: False
// Connection Lifetime: 5
// Enlist: true
// Min Pool Size: 1
// Max Pool Size: 50
connString = "provider=LCPI.IBProvider;" +
"data source=e:\\database\\employee.gdb;" +
"user id=sysdba;password=masterkey;" +
"Trusted_Connection=yes;" +
"connection reset=false;" +
"connection lifetime=5;" +
"enlist=true;" +
"min pool size=1;" +
"max pool size=50";
OleDbConnection myConnection1 = new OleDbConnection (connString);
OleDbConnection myConnection2 = new OleDbConnection (connString);
OleDbConnection myConnection3 = new OleDbConnection (connString);
// Open two connections.
Console.WriteLine ("Open two connections.");
myConnection1.Open();
myConnection2.Open();
// Now there are two connections in the pool that matches the connection string.
// Return the both connections to the pool.
Console.WriteLine ("Return both of the connections to the pool.");
myConnection1.Close();
myConnection2.Close();
// Get a connection out of the pool.
Console.WriteLine ("Open a connection from the pool.");
myConnection1.Open();
// Get a second connection out of the pool.
Console.WriteLine ("Open a second connection from the pool.");
myConnection2.Open();
// Open a third connection.
Console.WriteLine ("Open a third connection.");
myConnection3.Open();
// Return the all connections to the pool.
Console.WriteLine ("Return all three connections to the pool.");
myConnection1.Close();
myConnection2.Close();
myConnection3.Close();
}
catch(OleDbException myOLEDBException)
{
//ole db exception
//error collection
Console.WriteLine("----------------------------------------");
for (int i = 0; i<=myOLEDBException.Errors.Count-1;i++)
{
Console.WriteLine("Message " + (i + 1) + ": " + myOLEDBException.Errors[i].Message);
Console.WriteLine("Native: " + myOLEDBException.Errors[i].NativeError.ToString());
Console.WriteLine("Source: " + myOLEDBException.Errors[i].Source);
Console.WriteLine("SQL: " + myOLEDBException.Errors[i].SQLState);
Console.WriteLine("----------------------------------------");
}
}
catch (Exception myException)
{
//other exception
Console.WriteLine("----------------------------------------");
Console.WriteLine(myException.Message);
}
Console.WriteLine("All Done. Press Enter to close");
Console.ReadLine();
}
}
}
Imports System.Data
Imports System.Data.OleDb
'This Sample dempnstrate how organize Pool of the connections using OLE DB IBProvider
Module ADONetSample_ConnectionPooling
Sub Main()
Try
Dim connString As String
' Specification in the connection string:
' Please note: Pooling is implicit, you automatically get it unless you disable it.
' Therefore, "true" is the default for the pooling keyword (pooling=true).
' Connection Reset: False
' Connection Lifetime: 5
' Enlist: true
' Min Pool Size: 1
' Max Pool Size: 50
connString = "provider=LCPI.IBProvider;" & _
"data source=e:\database\employee.gdb;" & _
"user id=sysdba;password=masterkey;" & _
"Trusted_Connection=yes;" & _
"connection reset=false;" & _
"connection lifetime=5;" & _
"enlist=true;" & _
"min pool size=1;" & _
"max pool size=50"
Dim myConnection1 As OleDbConnection = New OleDbConnection(connString)
Dim myConnection2 As OleDbConnection = New OleDbConnection(connString)
Dim myConnection3 As OleDbConnection = New OleDbConnection(connString)
' Open two connections.
Console.WriteLine("Open two connections.")
myConnection1.Open()
myConnection2.Open()
' Now there are two connections in the pool that matches the connection string.
' Return the both connections to the pool.
Console.WriteLine("Return both of the connections to the pool.")
myConnection1.Close()
myConnection2.Close()
' Get a connection out of the pool.
Console.WriteLine("Open a connection from the pool.")
myConnection1.Open()
' Get a second connection out of the pool.
Console.WriteLine("Open a second connection from the pool.")
myConnection2.Open()
' Open a third connection.
Console.WriteLine("Open a third connection.")
myConnection3.Open()
' Return the all connections to the pool.
Console.WriteLine("Return all three connections to the pool.")
myConnection1.Close()
myConnection2.Close()
myConnection3.Close()
'Ole Db Error Collection
Catch myOLEDBException As OleDbException
Dim str As String, i As Integer
For i = 0 To myOLEDBException.Errors.Count - 1
Console.WriteLine("----------------------------------------")
Console.WriteLine("Message " & i.ToString + 1 & ": " + myOLEDBException.Errors(i).Message, True)
Console.WriteLine("Native: " + myOLEDBException.Errors(i).NativeError.ToString())
Console.WriteLine("Source: " + myOLEDBException.Errors(i).Source)
Console.WriteLine("SQL: " + myOLEDBException.Errors(i).SQLState)
Console.WriteLine("----------------------------------------")
Next i
'Other Error
Catch e As Exception
Console.WriteLine("----------------------------------------")
Console.WriteLine(e.Message)
End Try
Console.WriteLine("All Done. Press Enter to close")
Console.ReadLine()
End Sub
End Module