Oracle.ManagedDataAccess,Version=4.121.1.0,Culture=neutral,PublicKeyToken=89b483f429c47342
这是连接字符串:
User id=usr;Password=pwd;Data Source=database1;Validate connection=True;Connection timeout=8;Pooling=false
最重要的是,有时ODP会引发异常:
Oracle.ManagedDataAccess.Client.OracleException (0xFFFFFC18): Connection request timed out in OracleInternal.ConnectionPool.PoolManager`3.CreateNewPR(Int32 reqCount,Boolean bForPoolPopulation,ConnectionString csWithDiffOrNewPwd,String instanceName) in OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd,Boolean bGetForApp,String affinityInstanceName,Boolean bForceMatch) in OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword,Boolean bForceMatch) in OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs,PM conPM,ConnectionString pmCS,SecureString securedPassword,SecureString securedProxyPassword) in Oracle.ManagedDataAccess.Client.OracleConnection.Open() in MySyncApp.DBRepository.GetChangedDataDB(DateTime startPeriod) in D:\MySyncApp\MySyncApp\DB.cs:line 23 in MySyncApp.Program.<>c__DisplayClass30.<>c__DisplayClass32.<Synchronize>b__2f(ID id) in D:\MySyncApp\MySyncApp\Program.cs:line 441
但是在这个异常之后,当我查看Oracle中的会话时,我发现实际上连接正在存在并且只是标记为非活动!因此,此类连接将继续挂在服务器端,逐渐耗尽可用会话的数量.
我的代码没有什么特别的,只是
public List<DataObj> GetChangedDataDB(DateTime startPeriod) { List<DataObj> list = new List<DataObj>(); using (OracleConnection conn = new OracleConnection(this._connstr)) { conn.Open(); using (OracleCommand comm = new OracleCommand("select data from table(usr.syncpackage.GetChanged(:pStart))",conn)) { comm.CommandTimeout = 10; comm.Parameters.Add(":pStart",startPeriod); using (OracleDataReader reader = comm.ExecuteReader()) { // ..omitted } } } return list; }
此代码在Parallel.ForEach循环中运行,用于同时从大量数据库中提取数据.甚至可能是到同一数据库的三个并行连接(从模式的不同部分提取数据,例如,从三个不同的企业部门提取).
甲骨文是
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 – 64bit
Production
同步过程本身以10秒的间隔在定时器上触发.如果已经有运行任务,则下一个任务正在停止:
public static void Synchronize(object obj) { // ... omitted log.Info("ITERATION_COMMON_START"); if (Program.State == "Running") { log.Info("ITERATION_COMMON_END_BY_EXISTING"); return; } lock (Program.StateLock) { Program.State = "Running"; } Parallel.ForEach(Global.config.dbs,new ParallelOptions { MaxDegreeOfParallelism = -1 },(l) => { Console.WriteLine("Started synchronization for {0}",l.key); DBRepository db = new DBRepository(l.connectionString); Parallel.ForEach(l.departments,(department) => { DateTime ChangesFromTS = GetPrevIoUsIterationTS; List<DataObj> cdata = db.GetChangedDataDB(ChangesFromTS); // ... doing the work here } } // Finishing work GC.Collect(); lock (Program.StateLock) { Program.State = ""; } }
这是定期调用同步任务的计时器:
Program.getModifiedDataTimer = new Timer(Program.Synchronize,null,(int)Global.config.syncModifiedInterval * 1000,(int)Global.config.syncModifiedInterval * 1000);
Global.config.syncModifiedInterval以秒为单位
当我打开池时,ODP的行为方式与此相同.它创建了比具有相同异常的连接字符串中的Max pool size指令所允许的更多连接.
请告诉我你对这些东西的想法和经验.
UPDATE
以下是引发异常时的Oracle跟踪:
(PUB) (ERR) OracleConnection.Open() (txnid=n/a) Oracle.ManagedDataAccess.Client.OracleException (0xFFFFFC18): Connection request timed out in OracleInternal.ConnectionPool.PoolManager`3.CreateNewPR(Int32 reqCount,SecureString securedProxyPassword) in Oracle.ManagedDataAccess.Client.OracleConnection.Open()
更新#2
似乎这种连接因滞后连接而显示,就像发送oracle连接的请求被发送但其响应被忽略.或者,发送到服务器/从服务器发送的数据在到达目的地时正在被破坏.
即使我关闭了应用程序,连接仍会挂在服务器的会话列表中.当我杀死一个会话时,它会挂在列表中并带有“KILLED”标签.
更新#3
Here是同样问题的演示应用程序.正如我之前所说,它出现在连接不良时,您可以使用WANem仿真器模拟此类连接. Here是我用于数据库连接的相同组件.希望得到你的帮助.
解决方法
此外,我找不到文档说明ODP.net不是线程安全的,但我找不到任何文档说它是,所以如果没有其他帮助尝试此更改:
Parallel.ForEach(Global.config.dbs,(l) => { Console.WriteLine("Started synchronization for {0}",l.key); Parallel.ForEach(l.departments,(department) => { // Now local to the executing thread. DBRepository db = new DBRepository(l.connectionString); DateTime ChangesFromTS = GetPrevIoUsIterationTS; List<DataObj> cdata = db.GetChangedDataDB(ChangesFromTS); // ... doing the work here } }