Author: danmorg
Date: 2005-03-06 06:38:45 -0500 (Sun, 06 Mar 2005)
New Revision: 41480

Added:
   
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPool.cs
   
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPoolManager.cs
Modified:
   trunk/mcs/class/System.Data.OracleClient/ChangeLog
   trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient.dll.sources
   
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnection.cs
   trunk/mcs/class/System.Data.OracleClient/Test/TestOracleClient.cs
Log:
2005-03-06  Daniel Morgan <[EMAIL PROTECTED]>

        - Applied patch by Hubert FONGARNAND 
          <[EMAIL PROTECTED]>
        - Had to re-format according to mono style 
          and remove Console.WriteLines
        - Made sure maxPoolSize and minPoolSize can be specified 
          via the connection string, and that pooling can be disabled
        - if the next opened connection surpasses the max pool size, it waits
          for a specified time in milliseconds, if it did not 
          get another connection from the connection pool,
          throw an InvalidOperationException for timeout expired
          
        * System.Data.OracleClient.dll.sources: add 
        OracleConnectionPool.cs to the build
        
        * System.Data.OracleClient/OracleConnection.cs: modified 
          - add connection pooling to OracleClient
          - GetNlsInfo () was not using the passed handle, it was always
          a session handle
        
        * System.Data.OracleClient/OracleConnectionPoolManager.cs
        * System.Data.OracleClient/OracleConnectionPool.cs: added
        new file for connection pooling in OracleClient
        
        * System.Data.OracleClient/Test/TestOracleClient.cs: added tests
        for OracleClient connection pooling

Modified: trunk/mcs/class/System.Data.OracleClient/ChangeLog
===================================================================
--- trunk/mcs/class/System.Data.OracleClient/ChangeLog  2005-03-06 07:08:30 UTC 
(rev 41479)
+++ trunk/mcs/class/System.Data.OracleClient/ChangeLog  2005-03-06 11:38:45 UTC 
(rev 41480)
@@ -1,3 +1,31 @@
+2005-03-06  Daniel Morgan <[EMAIL PROTECTED]>
+
+       - Applied patch by Hubert FONGARNAND 
+         <[EMAIL PROTECTED]>
+       - Had to re-format according to mono style 
+         and remove Console.WriteLines
+       - Made sure maxPoolSize and minPoolSize can be specified 
+         via the connection string, and that pooling can be disabled
+       - if the next opened connection surpasses the max pool size, it waits
+         for a specified time in milliseconds, if it did not 
+         get another connection from the connection pool,
+         throw an InvalidOperationException for timeout expired
+         
+       * System.Data.OracleClient.dll.sources: add 
+       OracleConnectionPool.cs to the build
+       
+       * System.Data.OracleClient/OracleConnection.cs: modified 
+         - add connection pooling to OracleClient
+         - GetNlsInfo () was not using the passed handle, it was always
+         a session handle
+       
+       * System.Data.OracleClient/OracleConnectionPoolManager.cs
+       * System.Data.OracleClient/OracleConnectionPool.cs: added
+       new file for connection pooling in OracleClient
+       
+       * System.Data.OracleClient/Test/TestOracleClient.cs: added tests
+       for OracleClient connection pooling
+       
 2005-02-25  Daniel Morgan <[EMAIL PROTECTED]>
 
        * System.Data.OracleClient/OracleParameter.cs: modified 
@@ -548,12 +576,12 @@
 
 2003-02-12  Daniel Morgan <[EMAIL PROTECTED]>
 
-       * README.TXT
-    * System.Data.OracleClient/OciGlueLib_BCC_win32.make
-    * System.Data.OracleClient/OciGlueLib_GCC_linux.make
-    * System.Data.OracleClient/OciGlueLib_MSVC_win32.make
-    * System.Data.OracleClient/ociglue.c
-    * System.Data.OracleClient/ociglue.h
+       * README.TXT
+    * System.Data.OracleClient/OciGlueLib_BCC_win32.make
+    * System.Data.OracleClient/OciGlueLib_GCC_linux.make
+    * System.Data.OracleClient/OciGlueLib_MSVC_win32.make
+    * System.Data.OracleClient/ociglue.c
+    * System.Data.OracleClient/ociglue.h
     * Test/Test.csproj
     * Test/Test.csproj.user: removed files because System.Data.OracleClient
     no longer uses a glue library

Modified: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnection.cs
===================================================================
--- 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnection.cs
       2005-03-06 07:08:30 UTC (rev 41479)
+++ 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnection.cs
       2005-03-06 11:38:45 UTC (rev 41480)
@@ -8,11 +8,13 @@
 // Namespace: System.Data.OracleClient
 //
 // Authors: 
-//    Daniel Morgan <[EMAIL PROTECTED]>
+//    Daniel Morgan <[EMAIL PROTECTED]>
 //    Tim Coleman <[EMAIL PROTECTED]>
+//    Hubert FONGARNAND <[EMAIL PROTECTED]>
 //
-// Copyright (C) Daniel Morgan, 2002
+// Copyright (C) Daniel Morgan, 2002, 2005
 // Copyright (C) Tim Coleman, 2003
+// Copyright (C) Hubert FONGARNAND, 2005
 //
 // Original source code for setting ConnectionString 
 // by Tim Coleman <[EMAIL PROTECTED]>
@@ -36,9 +38,10 @@
 {
        internal struct OracleConnectionInfo 
        {
-               public string Username;
-               public string Password;
-               public string Database;
+               internal string Username;
+               internal string Password;
+               internal string Database;
+               internal string ConnectionString;
        }
 
        [DefaultEvent ("InfoMessage")]
@@ -52,6 +55,11 @@
                OracleTransaction transaction = null;
                string connectionString = "";
                OracleDataReader dataReader = null;
+               bool pooling = true;
+               static OracleConnectionPoolManager pools = new 
OracleConnectionPoolManager ();
+               OracleConnectionPool pool;
+               int minPoolSize = 0;
+               int maxPoolSize = 100;
 
                #endregion // Fields
 
@@ -60,7 +68,6 @@
                public OracleConnection () 
                {
                        state = ConnectionState.Closed;
-                       oci = new OciGlue ();
                }
 
                public OracleConnection (string connectionString) 
@@ -146,10 +153,7 @@
                        IntPtr sh = oci.ServiceContext;
                        IntPtr eh = oci.ErrorHandle;
 
-                       OciCalls.OCIServerVersion (sh, 
-                               eh, 
-                               ref buffer,  bufflen,
-                               OciHandleType.Service);
+                       OciCalls.OCIServerVersion (sh, eh, ref buffer,  
bufflen, OciHandleType.Service);
                        
                        // Get length of returned string
                        int     rsize = 0;
@@ -275,7 +279,7 @@
                {
                        byte[] buffer = new Byte[bufflen];
 
-                       int st = OciCalls.OCINlsGetInfo (Session, ErrorHandle, 
+                       int st = OciCalls.OCINlsGetInfo (handle, ErrorHandle, 
                                ref buffer, bufflen, (ushort) item);
 
                        // Get length of returned string
@@ -291,7 +295,14 @@
 
                public void Open () 
                {
-                       oci.CreateConnection (conInfo);
+                       if (!pooling) { 
+                               oci = new OciGlue ();
+                               oci.CreateConnection (conInfo);
+                       }
+                       else {
+                               pool = pools.GetConnectionPool (conInfo, 
minPoolSize, maxPoolSize);
+                               oci = pool.GetConnection ();
+                       }
                        state = ConnectionState.Open;
                        CreateStateChange (ConnectionState.Closed, 
ConnectionState.Open);
                }
@@ -325,7 +336,11 @@
                        if (transaction != null)
                                transaction.Rollback ();
 
-                       oci.Disconnect ();
+                       if (!pooling)
+                               oci.Disconnect ();
+                       else if (pool != null)
+                               pool.ReleaseConnection (oci);
+
                        state = ConnectionState.Closed;
                        CreateStateChange (ConnectionState.Open, 
ConnectionState.Closed);
                }
@@ -386,6 +401,8 @@
                        }
 
                        SetProperties (parameters);
+
+                       conInfo.ConnectionString = connectionString;
                }
 
                private void SetProperties (NameValueCollection parameters) 
@@ -395,6 +412,24 @@
                                value = parameters[name];
 
                                switch (name) {
+                               case "UNICODE":
+                                       break;
+                               case "ENLIST":
+                                       break;
+                               case "CONNECTION LIFETIME":
+                                       // TODO:
+                                       break;
+                               case "INTEGRATED SECURITY":
+                                       throw new NotImplementedException ();
+                               case "PERSIST SECURITY INFO":
+                                       // TODO:
+                                       break;
+                               case "MIN POOL SIZE":
+                                       minPoolSize = int.Parse (value);
+                                       break;
+                               case "MAX POOL SIZE":
+                                       maxPoolSize = int.Parse (value);
+                                       break;
                                case "DATA SOURCE" :
                                case "SERVER" :
                                        conInfo.Database = value;
@@ -407,6 +442,20 @@
                                case "USER ID" :
                                        conInfo.Username = value;
                                        break;
+                               case "POOLING" :
+                                       switch (value.ToUpper ()) {
+                                       case "YES":
+                                       case "TRUE":
+                                               pooling = true;
+                                               break;
+                                       case "NO":
+                                       case "FALSE":
+                                               pooling = false;
+                                               break;
+                                       default:
+                                               throw new 
ArgumentException("Connection parameter not supported: '" + name + "'");
+                                       }
+                                       break;
                                default:
                                        throw new ArgumentException("Connection 
parameter not supported: '" + name + "'");
                                }

Added: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPool.cs
===================================================================
--- 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPool.cs
   2005-03-06 07:08:30 UTC (rev 41479)
+++ 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPool.cs
   2005-03-06 11:38:45 UTC (rev 41480)
@@ -0,0 +1,101 @@
+//
+// OracleConnectionPool.cs 
+//
+// Part of the Mono class libraries at
+// mcs/class/System.Data.OracleClient/System.Data.OracleClient
+//
+// Assembly: System.Data.OracleClient.dll
+// Namespace: System.Data.OracleClient
+//
+// Authors: 
+//    Hubert FONGARNAND <[EMAIL PROTECTED]>
+//   
+// (C) Copyright 2005 Hubert FONGARNAND
+//
+//
+// Licensed under the MIT/X11 License.
+//
+
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+using System.ComponentModel;
+using System.Data;
+using System.Data.OracleClient.Oci;
+using System.Drawing.Design;
+using System.EnterpriseServices;
+using System.Text;
+using System.Threading;
+
+namespace System.Data.OracleClient 
+{
+       internal class OracleConnectionPool 
+       {
+               ArrayList list = new ArrayList (); // list of connections
+               OracleConnectionInfo info;
+               OracleConnectionPoolManager manager;
+               bool initialized;
+               int activeConnections = 0;
+               int PoolMinSize;
+               int PoolMaxSize;
+               
+               
+               public OracleConnectionPool (OracleConnectionPoolManager 
manager, OracleConnectionInfo info, int minPoolSize, int maxPoolSize) 
+               {
+                       this.info = info;
+                       this.manager = manager;
+                       initialized = false;
+                       PoolMinSize = minPoolSize;
+                       PoolMaxSize = maxPoolSize;
+               }
+               
+               public OciGlue GetConnection () 
+               {
+                       OciGlue connection = null;
+                       lock (list) {
+                               if (!initialized) {
+                                       
+                                       for (int n = 0; n < PoolMinSize; n++)
+                                               list.Add (CreateConnection ());
+                                       initialized = true;
+                               }
+                               do {
+                                       if (list.Count > 0) {
+                                               // There are available 
connections in the pool
+                                               connection = (OciGlue)list 
[list.Count - 1];
+                                               list.RemoveAt (list.Count -1);
+                                               if (!connection.Connected){
+                                                       connection = null;
+                                                       continue;
+                                               }
+                                       }
+                                       
+                                       if (connection == null && 
activeConnections < PoolMaxSize) {
+                                               connection = CreateConnection 
();
+                                       }
+                                       // Pas de connection disponible on 
attends que quelqu'un en libere une
+                                       if (connection == null) {
+                                               if (Monitor.Wait (list, 6000) 
== false)
+                                                       throw new 
InvalidOperationException ("Timeout expired.  The timeout expired waiting for a 
connection in the pool probably due to max connections reached.");
+                                       }
+                               } while (connection == null);
+                       }
+                       return connection;
+               }
+               
+               public void ReleaseConnection (OciGlue connection) 
+               {
+                       lock (list) {
+                               list.Add (connection);
+                               Monitor.Pulse (list);
+                       }
+               }
+               
+               OciGlue CreateConnection () 
+               {
+                       activeConnections++;
+                       return manager.CreateConnection (info);
+               }
+       }
+}
+


Property changes on: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPool.cs
___________________________________________________________________
Name: svn:executable
   + *

Added: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPoolManager.cs
===================================================================
--- 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPoolManager.cs
    2005-03-06 07:08:30 UTC (rev 41479)
+++ 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPoolManager.cs
    2005-03-06 11:38:45 UTC (rev 41480)
@@ -0,0 +1,62 @@
+//
+// OracleConnectionPoolManager.cs 
+//
+// Part of the Mono class libraries at
+// mcs/class/System.Data.OracleClient/System.Data.OracleClient
+//
+// Assembly: System.Data.OracleClient.dll
+// Namespace: System.Data.OracleClient
+//
+// Authors: 
+//    Hubert FONGARNAND <[EMAIL PROTECTED]>
+//   
+// (C) Copyright Hubert FONGARNAND, 2005
+//
+//
+// Licensed under the MIT/X11 License.
+//
+
+using System;
+using System.Collections;
+using System.Collections.Specialized;
+using System.ComponentModel;
+using System.Data;
+using System.Data.OracleClient.Oci;
+using System.Drawing.Design;
+using System.EnterpriseServices;
+using System.Text;
+using System.Threading;
+
+namespace System.Data.OracleClient 
+{
+       internal class OracleConnectionPoolManager 
+       {
+               Hashtable pools = new Hashtable();
+               
+               public OracleConnectionPoolManager () 
+               {
+               }
+               
+               public OracleConnectionPool GetConnectionPool 
(OracleConnectionInfo info, int minPoolSize, int maxPoolSize) 
+               {
+                       lock (pools) {
+                               
+                               OracleConnectionPool pool = 
(OracleConnectionPool) pools [info.ConnectionString];
+                               if (pool == null) {
+                                       pool = new OracleConnectionPool (this, 
info, minPoolSize, maxPoolSize);
+                                       pools [info.ConnectionString] = pool;
+                               }
+                               return pool;
+                       }
+               }
+               
+               public virtual OciGlue CreateConnection (OracleConnectionInfo 
info) 
+               {
+                       OciGlue oci;
+                       oci = new OciGlue ();
+                       oci.CreateConnection (info);
+                       return oci;
+               }
+       }
+}
+


Property changes on: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleConnectionPoolManager.cs
___________________________________________________________________
Name: svn:executable
   + *

Modified: 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient.dll.sources
===================================================================
--- 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient.dll.sources   
    2005-03-06 07:08:30 UTC (rev 41479)
+++ 
trunk/mcs/class/System.Data.OracleClient/System.Data.OracleClient.dll.sources   
    2005-03-06 11:38:45 UTC (rev 41480)
@@ -38,6 +38,8 @@
 System.Data.OracleClient/OracleBoolean.cs
 System.Data.OracleClient/OracleCommand.cs
 System.Data.OracleClient/OracleConnection.cs
+System.Data.OracleClient/OracleConnectionPool.cs
+System.Data.OracleClient/OracleConnectionPoolManager.cs
 System.Data.OracleClient/OracleDataAdapter.cs
 System.Data.OracleClient/OracleDataReader.cs
 System.Data.OracleClient/OracleDateTime.cs

Modified: trunk/mcs/class/System.Data.OracleClient/Test/TestOracleClient.cs
===================================================================
--- trunk/mcs/class/System.Data.OracleClient/Test/TestOracleClient.cs   
2005-03-06 07:08:30 UTC (rev 41479)
+++ trunk/mcs/class/System.Data.OracleClient/Test/TestOracleClient.cs   
2005-03-06 11:38:45 UTC (rev 41480)
@@ -26,11 +26,16 @@
 using System.Data;
 using System.Data.OracleClient;
 using System.Text;
+using System.Threading;
 
 namespace Test.OracleClient
 {
        public class OracleTest
        {
+               private static Thread t = null;
+               private static string conStr;
+               public static readonly int MAX_CONNECTIONS = 30; // max 
connections default to 100, but I will set to 30.
+
                public OracleTest() 
                {
 
@@ -937,6 +942,93 @@
                        Console.WriteLine("StateChange OriginalState:" + 
e.OriginalState.ToString ());
                }
 
+               public static void ConnectionPoolingTest1 () {
+                       Console.WriteLine("Start Connection Pooling Test 1...");
+                       OracleConnection[] connections = null;
+                       int maxCon = MAX_CONNECTIONS + 1; // add 1 more over 
the max connections to cause it to wait for the next available connection
+                       int i = 0;
+
+                       try {
+                               connections = new OracleConnection[maxCon];     
                
+               
+                               for (i = 0; i < maxCon; i++) {
+                                       Console.WriteLine("   Open connection: 
{0}", i);
+                                       connections[i] = new 
OracleConnection(conStr);
+                                       connections[i].Open ();
+                               }
+                       } catch (InvalidOperationException e) {
+                               Console.WriteLine("Expected exception 
InvalidOperationException caught.");
+                               Console.WriteLine(e);
+                       }
+
+                       for (i = 0; i < maxCon; i++) {
+                               if (connections[i] != null) {
+                                       Console.WriteLine("   Close connection: 
{0}", i);
+                                       if (connections[i].State == 
ConnectionState.Open)
+                                               connections[i].Close ();
+                                       connections[i].Dispose ();
+                                       connections[i] = null;
+                               }
+                       }
+
+                       connections = null;
+
+                       Console.WriteLine("Done Connection Pooling Test 1.");
+               }
+
+               public static void ConnectionPoolingTest2 () {
+                       Console.WriteLine("Start Connection Pooling Test 2...");
+                       OracleConnection[] connections = null;
+                       int maxCon = MAX_CONNECTIONS;
+                       int i = 0;
+
+                       connections = new OracleConnection[maxCon];             
        
+               
+                       for (i = 0; i < maxCon; i++) {
+                               Console.WriteLine("   Open connection: {0}", i);
+                               connections[i] = new OracleConnection(conStr);
+                               connections[i].Open ();
+                       }
+               
+                       Console.WriteLine("Start another thread...");
+                       t = new Thread(new ThreadStart(AnotherThreadProc));
+                       t.Start ();
+
+                       Console.WriteLine("Sleep...");
+                       Thread.Sleep(100);
+
+                       Console.WriteLine("Closing...");
+                       for (i = 0; i < maxCon; i++) {
+                               if (connections[i] != null) {
+                                       Console.WriteLine("   Close connection: 
{0}", i);
+                                       if (connections[i].State == 
ConnectionState.Open)
+                                               connections[i].Close ();
+                                       connections[i].Dispose ();
+                                       connections[i] = null;
+                               }
+                       }
+
+                       connections = null;
+               }
+
+               private static void AnotherThreadProc () {
+                       Console.WriteLine("Open connection via another 
thread...");
+                       OracleConnection[] connections = null;
+                       int maxCon = MAX_CONNECTIONS; 
+                       int i = 0;
+
+                       connections = new OracleConnection[maxCon];             
        
+               
+                       for (i = 0; i < maxCon; i++) {
+                               Console.WriteLine("   Open connection: {0}", i);
+                               connections[i] = new OracleConnection(conStr);
+                               connections[i].Open ();
+                       }
+
+                       Console.WriteLine("Done Connection Pooling Test 2.");
+                       System.Environment.Exit (0);
+               }
+
                [STAThread]
                static void Main(string[] args) 
                {       
@@ -951,6 +1043,8 @@
                                "Password={2}",
                                args[0], args[1], args[2]);
 
+                       conStr = connectionString;
+
                        OracleConnection con1 = new OracleConnection();
 
                        ShowConnectionProperties (con1);
@@ -1029,7 +1123,11 @@
                        Console.WriteLine("Closing...");
                        con1.Close ();
                        Console.WriteLine("Closed.");
-                       
+
+                       conStr = conStr + ";pooling=true;min pool size=4;max 
pool size=" + MAX_CONNECTIONS.ToString ();
+                       ConnectionPoolingTest1 ();
+                       ConnectionPoolingTest2 ();
+
                        Console.WriteLine("Done.");
                }
        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to