//Adds a new file. public void AddFile(File file) { bool isConnOpen = false; string sql = "INSERT INTO userfiles (file_owner, file_title, link) VALUES(@fileOwner, @fileTitle, @link)"; NpgsqlCommand cmd = new NpgsqlCommand(sql, MyConnection); cmd.Parameters.AddWithValue("@fileOwner", file.FileOwner); cmd.Parameters.AddWithValue("@fileTitle", file.FileTitle); cmd.Parameters.AddWithValue("@link", file.Link); if (MyConnection.State == System.Data.ConnectionState.Closed) { MyConnection.Open(); isConnOpen = true; MyTransaction = MyConnection.BeginTransaction(); } if (MyTransaction != null) { //used to participate in an opened trasaction happening somewhere else //assign the Transaction property to the opened transaction cmd.Transaction = MyTransaction; } cmd.ExecuteNonQuery(); MyTransaction.Commit(); if (isConnOpen) { MyConnection.Close(); isConnOpen = false; } }
public bool TxAbort(int txId) { lock (this) { Console.WriteLine("[TxAbort] Request."); if (!clientTransactions.ContainsKey(txId)) { throw new TxException(txId, "Transaction with id " + txId + "does not exists!"); } MyTransaction t = (MyTransaction)clientTransactions[txId]; foreach (DataServerInfo p in t.Participants) { try { p.remoteServer.doAbort(t.TxId); } catch (RemotingException re) { Console.WriteLine("[TxAbort]:\n" + re); throw new TxException(txId, "TxAbort transaction with id " + txId + "failed."); } catch (Exception) { //Console.WriteLine("TxAbort had one participant DataServer failed. Aborting the remaining..."); } } Console.WriteLine("---"); return(true); } }
public ExecuteResult AddComment(BlogCommentAddPageDTO dto, IUserBasicInfo userInfo) { ExecuteResult result = new ExecuteResult() { IsSuccess = true }; BlogCommentEntity commentEntity = Mapper.DynamicMap <BlogCommentEntity>(dto); commentEntity.InsertTime = DateTime.Now; commentEntity.UserID = userInfo.UserID; commentEntity.RealName = userInfo.RealName; MyTransaction transaction = this._commentDal.OpenTransaction(); try { this._commentDal.Add(commentEntity); this._blogDal.UpdateBlogComment(dto.BlogID, 1); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } return(result); }
//读写器录入 public int insertWriterReader(WriterReaderBean bean, Window win) { try { return(MyTransaction.WriterReader(bean, win)); } catch (Exception e) { e.GetBaseException(); return(BaseRequest.SYSTEM_EXCEPTION); } }
public int ExeTransactSql(string sSQL, SortedList paramList) { SqlTransaction MyTransaction; //String _ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString; SqlConnection myConnection = new SqlConnection(_ConnString); myConnection.Open(); MyTransaction = myConnection.BeginTransaction(); SqlCommand cmd = new SqlCommand(sSQL, myConnection, MyTransaction); int result; try { cmd.CommandType = CommandType.StoredProcedure; for (int x = 0; x <= paramList.Count - 1; x++) { cmd.Parameters.Add((String)paramList.GetKey(x), paramList.GetByIndex(x)); } result = Convert.ToInt32(cmd.ExecuteNonQuery()); MyTransaction.Commit(); cmd.Dispose(); cmd = null; myConnection.Close(); } catch (Exception ex) { if (ex.ToString() == "") { } MyTransaction.Rollback(); result = -1; //ErrorHandler.WriteError(Convert.ToString(ex)); if (myConnection.State == ConnectionState.Open) { myConnection.Close(); } myConnection.Dispose(); cmd.Dispose(); MyTransaction.Dispose(); } return(result); }
// interlocked -> ver: msdn.microsoft.com/en-us/library/dd78zt0c.aspx public int TxBegin(String clientUrl) { int txId; Console.WriteLine("[TxBegin] Client request"); lock (this) { txId = transactionId; Interlocked.Increment(ref transactionId); Transaction tx = new CommittableTransaction(); MyTransaction t = new MyTransaction(txId, clientUrl); clientTransactions.Add(txId, t); Console.WriteLine("---"); return(txId); } }
public DataTable GetDataTableWithTransact(string sp, SortedList paramList) { SqlTransaction MyTransaction; DataTable dt = new DataTable(); SqlConnection myConnection = new SqlConnection(_ConnString); myConnection.Open(); MyTransaction = myConnection.BeginTransaction(); SqlCommand cmd = new SqlCommand(sp, myConnection, MyTransaction); try { cmd.Connection = myConnection; cmd.CommandText = sp; cmd.CommandType = CommandType.StoredProcedure; for (int x = 0; x <= paramList.Count - 1; x++) { cmd.Parameters.Add((String)paramList.GetKey(x), paramList.GetByIndex(x)); } SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); da.Dispose(); MyTransaction.Commit(); cmd.Dispose(); cmd = null; myConnection.Close(); } catch (Exception ex) { if (ex.ToString() == "") { } MyTransaction.Rollback(); dt = null; //ErrorHandler.WriteError(Convert.ToString(ex)); if (myConnection.State == ConnectionState.Open) { myConnection.Close(); } myConnection.Dispose(); cmd.Dispose(); MyTransaction.Dispose(); } return(dt); }
public MyTransaction OpenTransaction() { MyTransaction transaction = null; if (!DbTransactionContext.HasTransaction(db.Database)) { transaction = new MyTransaction(db.BeginTransaction()); DbTransactionContext.AddTransaction(db.Database, transaction); } else { transaction = DbTransactionContext.GetTransaction(db.Database); } transaction.AddCount(); return(transaction); }
public ExecuteResult AddBlogTraffic(long blogID, string ip) { ExecuteResult result = new ExecuteResult() { IsSuccess = true }; if (_trafficDal.ExistView(blogID, ip)) { return(result); } var blogEntity = base.Single(m => m.ID == blogID); if (blogEntity == null) { return(result); } BlogTrafficLogEntity trafficEntity = new BlogTrafficLogEntity() { BlogID = blogID, IP = ip, InsertTime = DateTime.Now }; blogEntity.Traffic += 1; MyTransaction transaction = OpenTransaction(); try { long id = _trafficDal.Add(trafficEntity, true); base.UpdateOnly(blogEntity, m => new { m.Traffic }, m => m.ID == blogID); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } return(result); }
static void Main(string[] args) { _transactionRepository = new Repository <MyTransaction, int>(new Context()); var tr1 = new MyTransaction { Value = 9.65M, Description = "test transaction", CreatedDate = DateTime.UtcNow, UpdatedDate = DateTime.UtcNow, Title = "test" }; _transactionRepository.Create(tr1); var model = _transactionRepository.GetById(1); _transactionRepository.Remove(model); }
//添加设备 public int addEquipment(DeviceBean bean, Window win) { return(MyTransaction.addEquipmentService(bean, win)); }
//申请报修设备入库 public int repairEquipmentComing(int id) { return(MyTransaction.repairEquipmentCome(id)); }
public bool TxCommit(int txId) { int faulty = 0; // Auxiliar ArrayList to save the DataServer to whom we successfully issued the canCommit ArrayList pServers = new ArrayList(); Console.WriteLine("[TxCommit] Client request"); if (!clientTransactions.ContainsKey(txId)) { throw new TxException(txId, "Transaction with id " + txId + "does not exists!"); } try { lock (this) { MyTransaction t = (MyTransaction)clientTransactions[txId]; _myCommitDecision = true; for (int i = 0; i < t.Participants.Count; ++i) { faulty = i; DataServerInfo ds = (DataServerInfo)t.Participants[i]; _myCommitDecision = _myCommitDecision && ds.remoteServer.canCommit(t.TxId); pServers.Add(t.Participants[i]); } // Compara o tamanho dos arrays para saber se foi possivel fazer o canCommit a todos // se não for igual, aos que fizeram canCommit manda agora abortar e retorna false if (pServers.Count != t.Participants.Count) { foreach (DataServerInfo pt in pServers) { pt.remoteServer.doAbort(txId); } return(false); } if (_myCommitDecision) { Console.WriteLine("[TxCommit] Every Server voted Yes"); // try to commit infinite times foreach (DataServerInfo p in t.Participants) { while (true) { p.remoteServer.doCommit(t.TxId); break; } } } else { Console.WriteLine("[TxCommit] Some Server voted No."); _myCommitDecision = false; TxAbort(txId); } } Console.WriteLine("---"); return(true); } catch (RemotingException re) { Console.WriteLine("[TxCommit]:\n" + re); throw new TxException(txId, "TxCommit transaction with id " + txId + "failed. canCommit voting failed."); } catch (Exception) { Console.WriteLine("TxCommit can not complete. One participant DataServer is failed."); Console.WriteLine("The faulty DataServer will be removed from the participants of the Transaction."); Console.WriteLine("The current transaction will be aborted!"); MyTransaction t = (MyTransaction)clientTransactions[txId]; t.Participants.RemoveAt(faulty); TxAbort(txId); throw new OperationException("TxCommit can not be executed. Server does not respond. This transaction was automatically aborted!"); } }
public MyTransaction CreatMyTransaction(MyTransaction myTransaction) { _ctx.MyTransactions.Add(myTransaction); _ctx.SaveChanges(); return(myTransaction); }
async private Task Issue914_Repro_Example2() { var transaction = new MyTransaction(); await transaction.DisposeAsync(); }
async private Task Issue914_Repro_Example1() { await using var transaction = new MyTransaction(); }
//申请报修 public int repairEquipment(int id) { return(MyTransaction.addRepair(id)); }