remove svn:executable from .cs files
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleConnectionPool.cs
1 //
2 // OracleConnectionPool.cs 
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors: 
11 //    Hubert FONGARNAND <informatique.internet@fiducial.fr>
12 //   
13 // (C) Copyright 2005 Hubert FONGARNAND
14 //
15 //
16 // Licensed under the MIT/X11 License.
17 //
18
19 using System;
20 using System.Collections;
21 using System.Collections.Specialized;
22 using System.ComponentModel;
23 using System.Data;
24 using System.Data.OracleClient.Oci;
25 using System.Drawing.Design;
26 using System.EnterpriseServices;
27 using System.Text;
28 using System.Threading;
29
30 namespace System.Data.OracleClient 
31 {
32         internal class OracleConnectionPool 
33         {
34                 ArrayList list = new ArrayList (); // list of connections
35                 OracleConnectionInfo info;
36                 OracleConnectionPoolManager manager;
37                 bool initialized;
38                 int activeConnections = 0;
39                 int PoolMinSize;
40                 int PoolMaxSize;
41                 
42                 
43                 public OracleConnectionPool (OracleConnectionPoolManager manager, OracleConnectionInfo info, int minPoolSize, int maxPoolSize) 
44                 {
45                         this.info = info;
46                         this.manager = manager;
47                         initialized = false;
48                         PoolMinSize = minPoolSize;
49                         PoolMaxSize = maxPoolSize;
50                 }
51                 
52                 public OciGlue GetConnection () 
53                 {
54                         OciGlue connection = null;
55                         lock (list) {
56                                 if (!initialized) {
57                                         
58                                         for (int n = 0; n < PoolMinSize; n++)
59                                                 list.Add (CreateConnection ());
60                                         initialized = true;
61                                 }
62                                 do {
63                                         if (list.Count > 0) {
64                                                 // There are available connections in the pool
65                                                 connection = (OciGlue)list [list.Count - 1];
66                                                 list.RemoveAt (list.Count -1);
67                                                 if (!connection.Connected){
68                                                         connection = null;
69                                                         continue;
70                                                 }
71                                         }
72                                         
73                                         if (connection == null && activeConnections < PoolMaxSize) {
74                                                 connection = CreateConnection ();
75                                         }
76                                         // Pas de connection disponible on attends que quelqu'un en libere une
77                                         if (connection == null) {
78                                                 if (Monitor.Wait (list, 6000) == false)
79                                                         throw new InvalidOperationException ("Timeout expired.  The timeout expired waiting for a connection in the pool probably due to max connections reached.");
80                                         }
81                                 } while (connection == null);
82                         }
83                         return connection;
84                 }
85                 
86                 public void ReleaseConnection (OciGlue connection) 
87                 {
88                         lock (list) {
89                                 list.Add (connection);
90                                 Monitor.Pulse (list);
91                         }
92                 }
93                 
94                 OciGlue CreateConnection () 
95                 {
96                         activeConnections++;
97                         return manager.CreateConnection (info);
98                 }
99         }
100 }
101