3

I'm using Oracle.ManagedDataAccess Nuget package version 18.3.0. I tried many things. I tried to dispose everything I can think of, even oracle parameters objects. And wrapped everything inside a using block but to no avail. The only thing that actually worked for me is that the commented line OracleConnection.ClearPool(oracle);. Is this a bug, or some configuration related issue, or am I misunderstand things here? Also, I tried to remove the reference of Oracle.ManagedDataAccess and replaced it with a reference to System.Data.OracleClient and that actually worked for me. It automatically closed the connection, so no connection left with "In-Active" status. The code below I moved it into a simple, single button, Windows Forms application to make 100% sure nothing is interfering and the problem still occurring.

using (var oracle = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=SomePortHere))(CONNECT_DATA=(SERVER=SHARED)(SERVICE_NAME=anotherHost)))", new OracleCredential(userName,password)))
                {
                    oracle.Open();
                    using (var command = new OracleCommand())
                    {
                        var query = "SELECT x from y where z=:param1";
                        command.Connection = oracle;
                        command.CommandText = query;
                        command.CommandType = System.Data.CommandType.Text;
                        var param1 = new OracleParameter(":param1", xyz);
                        command.Parameters.Add(param1);
                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                //read the data from the reader
                            }
                        }
                        param1.Dispose();
                    }
                    //If this line is commented, there will be a connection left open, with InActive status
                    //OracleConnection.ClearPool(oracle);
                }
                password.Dispose();
                return myData;

And this is an image to show the opened connection through Toad.
Oracle Connection issue Of course, for each click to that button, the code above will execute and a new session will remain open, until what you see in the image. The name "TheTesterOfAllTests.exe" is the Windows Forms app.
Is this a configuration problem? Is there any way to solve this issue other than using ClearPool method? Because it affects the performance of the app.
P.S. The application that is originally using the code above is a WCF Service that is consumed by a Web application.
P.S. 2 There is some kind of memory leakage, with each click to that button the memory usage increases

4

1 回答 1

0

事实证明,问题出在 oracle 内部创建连接的方式上,因为对于每个新创建OracleConnection的对象,都会将一个新的连接添加到连接池中。我计算91了连接池中的连接条目。OracleConnection解决方案是为每个请求“每个请求范围”使用一个实例。我通过使用IDatabase<TConnection>带有一个方法的简单通用接口来实现这一点TConnection GetConnection<TConnection>(),当然对于将在同一个请求实例上调用的每个方法,都会发生一对打开/关闭调用,因此我们不会一直保持连接打开.
关于内存泄漏,我仍然无法100%确认这一点,但是当我使用Oracle.DataAccess.Client库而不是Oracle.ManagedDataAccess内存使用量时,内存使用量急剧减少。所以,我切换回Oracle.DataAccess.Client.

PS 如果有关于这两个问题的新信息,我会更新这个答案,非常欢迎贡献,也许我误解了Oracle如何处理数据库连接的一些事情。

于 2019-01-29T18:26:26.243 回答