protected bool Connect(Task task, Database readerDB, out IDBMSReader reader, Database writerDB, out IDBMSWriter writer) { reader = null; writer = null; if (readerDB != null) { reader = DBMSFactory.GetDBMSReaderByName(readerDB.DBMS); if (reader == null) { string errMsg = $"数据库类型 {readerDB.DBMS} 不支持!"; Logger.WriteLog("系统", errMsg); task.ErrorMsg = errMsg; return(false); } bool rst = reader.Connect(readerDB); if (!rst) { task.ErrorMsg = reader.LastError; return(false); } } if (writerDB != null) { writer = DBMSFactory.GetDBMSWriterByName(writerDB.DBMS); if (writer == null) { string errMsg = $"数据库类型 {writerDB.DBMS} 不支持!"; Logger.WriteLog("系统", errMsg); task.ErrorMsg = errMsg; return(false); } bool rst = writer.Connect(writerDB); if (!rst) { task.ErrorMsg = writer.LastError; return(false); } } return(reader != null || writer != null); }
private void IntegrateTaskWithScript(IntegrationTask task, Dictionary <string, object> parms, IDBMSWriter dest, out string failReason) { ConcurrentQueue <object> scripts = new ConcurrentQueue <object>(); bool read = false; string reason = "取消操作"; Parallel.ForEach(CreateThreadAction(), act => { try { // “读”线程:个别数据源读取失败,容错继续执行 if ("read".Equals(act)) { uint bufSize = task.ReadPages * 3; IDataFilter filter = DataFilterFactory.GetFilterByName(task.Table.Filter); IDataWrapper data = null; foreach (Database db in task.Sources) { if (status.Stopped || task.Status == DataStates.Error) { break; } uint fromRow = 1, toRow = task.ReadPages * task.Table.PageSize; Dictionary <string, object> tmpParams = new Dictionary <string, object>(parms); // 连接数据源 if (!Connect(task, db, out IDBMSReader source, null, out _)) { task.Status = DataStates.RunningError; reason = task.ErrorMsg; break; } while (true) { // 等待缓冲区可用 while (scripts.Count > bufSize && !status.Stopped && task.Status != DataStates.Error) { Thread.Sleep(50); } if (status.Stopped || task.Status == DataStates.Error) { break; } // 取数 if (source.QueryPage(task.Table, fromRow, toRow, WithEnums.NoLock, tmpParams, out data)) { try { object script = null; data.MapFields(task.Table.DestFields); while (dest.BuildScript(task.Table, data, filter, out script) && !status.Stopped && task.Status != DataStates.Error) { scripts.Enqueue(script); } // 获取不到预期的记录数,作最后一页处理 if (data.ReadCount != task.ReadPages * task.Table.PageSize || status.Stopped) { break; } } finally { data.Close(); } } else { task.Status = DataStates.RunningError; reason = source.LastError; break; } fromRow = toRow + 1; toRow += task.ReadPages * task.Table.PageSize; } source.Close(); }