///<summary>Sets the connection of the current thread to the ConnectionName indicated. Connection details will be retrieved from ConnectionStore.xml.</summary> public static OpenDentBusiness.CentralConnection SetDbT(ConnectionNames dbName, DataConnection dataConn = null) { dataConn = dataConn ?? new DataConnection(); OpenDentBusiness.CentralConnection conn = GetConnection(dbName); _currentConnectionT = dbName; if (!string.IsNullOrEmpty(conn.ServiceURI)) { RemotingClient.SetRemotingT(conn.ServiceURI, RemotingRole.ClientWeb, (dbName == ConnectionNames.DentalOfficeReportServer)); } else if (!string.IsNullOrEmpty(conn.ConnectionString)) { dataConn.SetDbT(conn.ConnectionString, "", DatabaseType.MySql); } else { dataConn.SetDbT(conn.ServerName, conn.DatabaseName, conn.MySqlUser, conn.MySqlPassword, "", "", DatabaseType.MySql, true); } return(conn); }
///<summary>Perform the given function in the context of the given connectionString db and return a T.</summary> public static T GetT <T>(Func <T> fn, string connectionString, DatabaseType dbType = DatabaseType.MySql) { T ret = default(T); ExecuteThread(new ODThread((o) => { using (DataConnection dataConn = new DataConnection()) { dataConn.SetDbT(connectionString, "", dbType); ret = fn(); } })); return(ret); }
///<summary>Perform the given function in the context of the given connectionString db and return a T.</summary> public static T GetT <T>(Func <T> fn, string server, string db, string user, string password, string userLow, string passLow, DatabaseType dbType = DatabaseType.MySql) { T ret = default(T); ExecuteThread(new ODThread((o) => { using (DataConnection dataConn = new DataConnection()) { dataConn.SetDbT(server, db, user, password, userLow, passLow, dbType, true); ret = fn(); } })); return(ret); }
///<summary>Updates the current data connection settings of the central manager to the connection settings passed in. Setting refreshCache to true will cause the entire local cache to get updated with the cache from the connection passed in if the new connection settings are successful.</summary> public static bool UpdateCentralConnection(CentralConnection centralConnection, bool refreshCache) { UTF8Encoding enc = new UTF8Encoding(); byte[] EncryptionKey = enc.GetBytes("mQlEGebnokhGFEFV"); //Gotten from FormCentralManager constructor. Only place that does anything like this. string computerName = ""; string database = ""; string user = ""; string password = ""; if (centralConnection.ServerName != "") //Direct connection { computerName = centralConnection.ServerName; database = centralConnection.DatabaseName; user = centralConnection.MySqlUser; if (centralConnection.MySqlPassword != "") { password = CentralConnections.Decrypt(centralConnection.MySqlPassword, EncryptionKey); } try { DataConnection.DBtype = DatabaseType.MySql; OpenDentBusiness.DataConnection dcon = new OpenDentBusiness.DataConnection(); dcon.SetDbT(computerName, database, user, password, "", "", DataConnection.DBtype); RemotingClient.SetRemotingRoleT(RemotingRole.ClientDirect); if (refreshCache) { Cache.Refresh(InvalidType.AllLocal); } } catch { return(false); } } else if (centralConnection.ServiceURI != "") //Middle tier connection { RemotingClient.SetServerURIT(centralConnection.ServiceURI); RemotingClient.SetRemotingRoleT(RemotingRole.ClientWeb); } else { MessageBox.Show("Either a database or a Middle Tier URI must be specified in the connection."); return(false); } return(true); }
///<summary>Creates actions and runs them in parallel threads to process the insert commands in the queue.</summary> public static void InsertBatches() { if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { Meth.GetVoid(MethodBase.GetCurrentMethod()); return; } #if DEBUG Stopwatch s = new Stopwatch(); s.Start(); #endif _insertBatchCount = 0; try { #region Create List of Actions List <Action> listActions = new List <Action>(); int numThreads = Math.Max(INSERT_THREAD_MIN_COUNT, Environment.ProcessorCount); //use at least 8 threads, but use ProcessorCount if more than 8 cores for (int i = 0; i < numThreads; i++) //create numThreads number of actions, 1 per thread to run in parallel { listActions.Add(new Action(() => { if (!string.IsNullOrEmpty(_serverTo)) //SetDbT here if server is specified { DataConnection dcon = new DataConnection(); dcon.SetDbT(_serverTo, _databaseTo, _userTo, _passwordTo, "", "", DatabaseType.MySql); } bool isBatchQueued = false; bool insertFailed = true; while (!_areQueueBatchThreadsDone || isBatchQueued) //if queue batch thread is done and queue is empty, loop is finished { BatchQueries batch = null; try { lock (_lockObjQueueBatchQueries) { if (_queueBatchQueries.Count == 0) { //queueBatchThread must not be finished gathering batches but the queue is empty, give the batch thread time to catch up continue; } batch = _queueBatchQueries.Dequeue(); } if (batch == null || (string.IsNullOrEmpty(batch.CommandValuesInsert) && string.IsNullOrEmpty(batch.CommandBulkInsert))) { continue; } Db.NonQ(batch.CommandValuesInsert); insertFailed = false; } catch (Exception ex) { //just loop again and wait if necessary ex.DoNothing(); insertFailed = true; if (!string.IsNullOrEmpty(batch.CommandBulkInsert)) { try { //If multiple bulk insert commands get here at the same time they will fail 100% of the time for InnoDB an table due to //a MySQL deadlock issue caused by the sub-select that makes sure it is not trying to insert duplicate rows. Db.NonQ(batch.CommandBulkInsert); insertFailed = false; } catch (Exception ex2) { ex2.DoNothing(); insertFailed = true; } } continue; } finally { lock (_lockObjQueueBatchQueries) { if (!insertFailed) { insertFailed = true; _insertBatchCount++; } isBatchQueued = _queueBatchQueries.Count > 0; } } } //end of while loop })); //end of listActions.Add } //end of for loop #endregion Create List of Actions ODThread.RunParallel(listActions, TimeSpan.FromHours(12), numThreads, new ODThread.ExceptionDelegate((ex) => { ODEvent.Fire(ODEventType.ConvertDatabases, new ProgressBarHelper("Error processing batch insert: " + ex.Message, progressBarEventType: ProgBarEventType.TextMsg)); })); } catch (Exception ex) { ODEvent.Fire(ODEventType.ConvertDatabases, new ProgressBarHelper("Error inserting batch: " + ex.Message, progressBarEventType: ProgBarEventType.TextMsg)); } #if DEBUG s.Stop(); Console.WriteLine("InsertDataThread - Done, inserted " + _insertBatchCount + " batches: " + (s.Elapsed.Hours > 0?(s.Elapsed.Hours + " hours "):"") + (s.Elapsed.Minutes > 0?(s.Elapsed.Minutes + " min "):"") + (s.Elapsed.TotalSeconds - (s.Elapsed.Hours * 60 * 60) - (s.Elapsed.Minutes * 60)) + " sec"); #endif }