public static MyDbConnection2 PopDBConnection(string dbKey) { MyDbConnection2 conn = null; DbHelperMySQL3.SemaphoreClientsNoPool.WaitOne(); try { string connectionString; lock (DbHelperMySQL3.Mutex) { if (!DbHelperMySQL3.ConnectionStringDict.TryGetValue(dbKey, out connectionString)) { connectionString = PubConstant.ConnectionString; string dbName = PubConstant.GetDatabaseName(dbKey); int idx0 = connectionString.IndexOf("database=") + "database=".Length; int idx = connectionString.IndexOf(';', idx0); string datebaseName = connectionString.Substring(idx0, idx - idx0); connectionString = connectionString.Replace(datebaseName, dbName); DbHelperMySQL3.ConnectionStringDict[dbKey] = connectionString; } } conn = new MyDbConnection2(connectionString, DbHelperMySQL3.CodePageNames); if (!conn.Open()) { conn = null; } } catch (Exception ex) { conn = null; } finally { if (null == conn) { DbHelperMySQL3.SemaphoreClientsNoPool.Release(); } } return(conn); }
/// <summary> /// 执行Sql和Oracle滴混合事务 /// </summary> /// <param name="list">SQL命令行列表</param> /// <param name="oracleCmdSqlList">Oracle命令行列表</param> /// <returns>执行结果 0-由于SQL造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功</returns> public static int ExecuteSqlTran(List <CommandInfo> list, List <CommandInfo> oracleCmdSqlList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = myDE.CommandText; SqlParameter[] cmdParms = (SqlParameter[])myDE.Parameters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.SolicitationEvent) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式"); //return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (isHave) { //引发事件 myDE.OnSolicitationEvent(); } } if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式"); //return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0"); //return 0; } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0"); //return 0; } continue; } int val = cmd.ExecuteNonQuery(); if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行"); //return 0; } cmd.Parameters.Clear(); } string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC"); bool res = OracleHelper.ExecuteSqlTran(oraConnectionString, oracleCmdSqlList); if (!res) { tx.Rollback(); throw new Exception("Oracle执行失败"); // return -1; } tx.Commit(); return(1); } catch (System.Data.SqlClient.SqlException e) { tx.Rollback(); throw e; } catch (Exception e) { tx.Rollback(); throw e; } } }
public static int ExecuteSqlTran(List <CommandInfo> list, List <CommandInfo> oracleCmdSqlList) { int result; using (MySqlConnection conn = new MySqlConnection(DbHelperMySQL.connectionString)) { conn.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; MySqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = myDE.CommandText; MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters; DbHelperMySQL.PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.SolicitationEvent) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式"); } object obj = cmd.ExecuteScalar(); if (obj == null && obj == DBNull.Value) { } bool isHave = Convert.ToInt32(obj) > 0; if (isHave) { myDE.OnSolicitationEvent(); } } if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式"); } object obj = cmd.ExecuteScalar(); if (obj == null && obj == DBNull.Value) { } bool isHave = Convert.ToInt32(obj) > 0; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0"); } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0"); } } else { int val = cmd.ExecuteNonQuery(); if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { tx.Rollback(); throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行"); } cmd.Parameters.Clear(); } } string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC"); if (!OracleHelper.ExecuteSqlTran(oraConnectionString, oracleCmdSqlList)) { tx.Rollback(); throw new Exception("执行失败"); } tx.Commit(); result = 1; } catch (MySqlException e) { tx.Rollback(); throw e; } finally { if (cmd != null) { cmd.Dispose(); } if (conn != null) { conn.Close(); } } } return(result); }
/// <summary> /// 从连接队列中取一个空闲数据库连接 /// </summary> /// <returns></returns> public static MyDbConnection2 PopDBConnection(string dbKey) { MyDbConnection2 conn = null; if (UsePool) { int round = 0; MyDbConnectionPool pool = null; lock (Mutex) { if (!DBConnsDict.TryGetValue(dbKey, out pool)) { string connectionString = PubConstant.ConnectionString; string dbName = PubConstant.GetDatabaseName(dbKey); if (null == dbName) { return(null); } int idx0 = connectionString.IndexOf("database=") + "database=".Length; int idx1 = connectionString.IndexOf(';', idx0); string datebaseName = connectionString.Substring(idx0, idx1 - idx0); pool = new MyDbConnectionPool(); DBConnsDict[dbKey] = pool; pool.DatabaseKey = dbKey; pool.ConnectionString = connectionString.Replace(datebaseName, dbName); } } if (null != pool) { do { //防止无法获取, 阻塞等待 if (pool.SemaphoreClients.WaitOne(1000)) { lock (pool.DBConns) { conn = pool.DBConns.Dequeue(); break; } } else { lock (Mutex) { if (pool.ConnCount < MaxCount) { try { conn = new MyDbConnection2(pool.ConnectionString, CodePageNames); if (conn.Open()) { conn.DatabaseKey = dbKey; pool.ConnCount++; break; } } catch (System.Exception ex) { LogManager.WriteExceptionUseCache(ex.ToString()); } } } } } while (round++ < 150); } } else { SemaphoreClientsNoPool.WaitOne(); try { string connectionString; bool change = false; lock (Mutex) { if (!ConnectionStringDict.TryGetValue(dbKey, out connectionString)) { connectionString = PubConstant.ConnectionString; string dbName = PubConstant.GetDatabaseName(dbKey); int idx0 = connectionString.IndexOf("database=") + "database=".Length; int idx1 = connectionString.IndexOf(';', idx0); string datebaseName = connectionString.Substring(idx0, idx1 - idx0); connectionString = connectionString.Replace(datebaseName, dbName); ConnectionStringDict[dbKey] = connectionString; } } conn = new MyDbConnection2(connectionString, CodePageNames); if (!conn.Open()) { conn = null; } } catch (System.Exception ex) { conn = null; } finally { if (null == conn) { SemaphoreClientsNoPool.Release(); } } } return(conn); }
/// <summary> /// /// </summary> /// <param name="list"></param> /// <param name="oracleCmdSqlList"></param> /// <returns></returns> /// <exception cref="Exception"></exception> /// <exception cref="MySqlException"></exception> public static int ExecuteSqlTran(List <CommandInfo> list, List <CommandInfo> oracleCmdSqlList) { int num2; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand cmd = new MySqlCommand { Connection = connection }; MySqlTransaction trans = connection.BeginTransaction(); cmd.Transaction = trans; try { foreach (CommandInfo info in list) { object obj2; bool flag; string commandText = info.CommandText; MySqlParameter[] parameters = (MySqlParameter[])info.Parameters; PrepareCommand(cmd, connection, trans, commandText, parameters); if (info.EffentNextType == EffentNextType.SolicitationEvent) { if (info.CommandText.ToLower().IndexOf("count(") == -1) { trans.Rollback(); throw new Exception("违背要求" + info.CommandText + "必须符合select count(..的格式"); } obj2 = cmd.ExecuteScalar(); flag = false; if ((obj2 == null) && (obj2 == DBNull.Value)) { flag = false; } flag = Convert.ToInt32(obj2) > 0; if (flag) { info.OnSolicitationEvent(); } } if ((info.EffentNextType == EffentNextType.WhenHaveContine) || (info.EffentNextType == EffentNextType.WhenNoHaveContine)) { if (info.CommandText.ToLower().IndexOf("count(") == -1) { trans.Rollback(); throw new Exception("SQL:违背要求" + info.CommandText + "必须符合select count(..的格式"); } obj2 = cmd.ExecuteScalar(); flag = false; if ((obj2 == null) && (obj2 == DBNull.Value)) { flag = false; } flag = Convert.ToInt32(obj2) > 0; if (!((info.EffentNextType != EffentNextType.WhenHaveContine) || flag)) { trans.Rollback(); throw new Exception("SQL:违背要求" + info.CommandText + "返回值必须大于0"); } if ((info.EffentNextType == EffentNextType.WhenNoHaveContine) && flag) { trans.Rollback(); throw new Exception("SQL:违背要求" + info.CommandText + "返回值必须等于0"); } } else { int num = cmd.ExecuteNonQuery(); if ((info.EffentNextType == EffentNextType.ExcuteEffectRows) && (num == 0)) { trans.Rollback(); throw new Exception("SQL:违背要求" + info.CommandText + "必须有影响行"); } cmd.Parameters.Clear(); } } if (!OracleHelper.ExecuteSqlTran(PubConstant.GetConnectionString("ConnectionStringPPC"), oracleCmdSqlList)) { trans.Rollback(); throw new Exception("执行失败"); } trans.Commit(); num2 = 1; } catch (MySqlException exception) { trans.Rollback(); throw exception; } catch (Exception exception2) { trans.Rollback(); throw exception2; } } return(num2); }