本文整理汇总了C#中LockType.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# LockType.ToString方法的具体用法?C# LockType.ToString怎么用?C# LockType.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LockType
的用法示例。
在下文中一共展示了LockType.ToString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetLock
public bool GetLock(Guid entityId, LockType lockType)
{
LockType value;
if (locks.TryGetValue(entityId, out value) && lockType == value)
return true;
var sessionManager = SessionManager.Manager;
var session = sessionManager.GetSession(sessionManager.Current);
using (var transaction = session.DbInfo.Connection.BeginTransaction(IsolationLevel.Serializable))
{
try
{
var reader = ExecuteReader(entityId, session, (SqlTransaction)transaction);
var query = CREATE_QUERY;
if (reader.HasRows)
{
while (reader.Read())
{
var sessionId = reader.GetGuid(0);
var type = reader.GetInt16(1);
if (!Enum.TryParse<LockType>(type.ToString(), out value))
{
reader.Close();
throw new Exception("Wrong LockType fetched. " + type);
}
if ((lockType == LockType.Write || value == LockType.Write) && session.Id.CompareTo(sessionId) != 0)
{
reader.Close();
throw new Exception(string.Format("Can't get {0} lock for {1}, session {2} has {3} lock.", lockType.ToString(), SessionManager.Manager.GetSession(session.Id).Name, SessionManager.Manager.GetSession(sessionId).Name, value.ToString()));
}
if (session.Id.CompareTo(sessionId) == 0)
query = UPDATE_QUERY;
}
}
reader.Close();
ExecuteNonQuery(query, entityId, session, lockType, transaction);
transaction.Commit();
Console.WriteLine(string.Format("Session {0} adquired {1} lock for VersionId: {2}", session.Name, lockType.ToString(), entityId));
if (locks.ContainsKey(entityId))
locks[entityId] = lockType;
else
locks.Add(entityId, lockType);
}
catch (Exception ex)
{
transaction.Rollback();
Console.WriteLine(ex.Message);
return false;
}
}
return true;
}
示例2: GetLock
public void GetLock(Guid entityId, LockType lockType)
{
LockType value;
if (locks.TryGetValue(entityId, out value) && lockType == value)
return;
var connection = new SqlConnection(SQL_CONNECTION);
connection.Open();
using (var transaction = connection.BeginTransaction(IsolationLevel.Serializable))
{
try
{
var reader = ExecuteReader(entityId, connection, transaction);
var query = CREATE_QUERY;
if (reader.HasRows)
{
while (reader.Read())
{
var currentOwnerId = reader.GetGuid(0);
var type = reader.GetInt16(1);
if (!Enum.TryParse<LockType>(type.ToString(), out value))
{
reader.Close();
throw new Exception("Wrong LockType fetched. " + type);
}
if ((lockType == LockType.Write || value == LockType.Write) && Session.Id.CompareTo(currentOwnerId) != 0)
{
reader.Close();
throw new Exception(string.Format("Can't get {0} lock for {1}, session ID '{2}' has {3} lock.", lockType.ToString(), Session.Name, currentOwnerId.ToString(), value.ToString()));
}
if (Session.Id.CompareTo(currentOwnerId) == 0)
query = UPDATE_QUERY;
}
}
reader.Close();
ExecuteNonQuery(query, entityId, lockType, connection, transaction);
transaction.Commit();
if (locks.ContainsKey(entityId))
locks[entityId] = lockType;
else
locks.Add(entityId, lockType);
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
finally
{
connection.Close();
}
}
}
示例3: AddRecordLock
public static LockType AddRecordLock(string DBAlias, string TableName, string KeyFields, string KeyValues, ref string UserID, LockType lt)
{
CheckTable();
var result = LockType.Idle;
using (var connection = AllocateConnection())
{
connection.Open();
var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
var command = connection.CreateCommand();
command.Transaction = transaction;
try
{
var parameters = AddParameters(new string[] { "DBALIAS", "TABLENAME", "KEYFIELDS", "KEYVALUES" }, new object[] { DBAlias, TableName, KeyFields, KeyValues }, command);
command.CommandText = string.Format("SELECT * FROM SYS_RECORDLOCK WHERE DBALIAS = {0} AND TABLENAME = {1} AND KEYFIELDS = {2} AND KEYVALUES = {3}", parameters);
var adpater = DBUtils.CreateDbDataAdapter(command);
var dataSet = new DataSet();
adpater.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count > 0)
{
var lockUser = dataSet.Tables[0].Rows[0]["USERID"].ToString();
if (string.Compare(lockUser, UserID, true) != 0)
{
UserID = lockUser;
result = (LockType)Enum.Parse(typeof(LockType), dataSet.Tables[0].Rows[0]["STATUS"].ToString(), true);
}
}
else
{
command.Parameters.Clear();
parameters = AddParameters(new string[] { "USERID", "DBALIAS", "TABLENAME", "KEYFIELDS", "KEYVALUES", "STATUS" }, new object[] { UserID, DBAlias, TableName, KeyFields, KeyValues, lt.ToString() }, command);
command.CommandText = string.Format("INSERT INTO SYS_RECORDLOCK (USERID, DBALIAS, TABLENAME, KEYFIELDS, KEYVALUES, STATUS) VALUES ({0}, {1}, {2}, {3}, {4}, {5})", parameters);
command.ExecuteNonQuery();
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
return result;
}
示例4: VerifyLock
private bool VerifyLock(Guid entityId, LockType lockType, out List<LockItem> lockItems)
{
var hasLocks = locksSet.TryGetValue(entityId, out lockItems);
if (hasLocks)
{
if (lockItems[0].Type == lockType)
{
var session = GetSession();
Console.WriteLine(string.Format("{0} can't get read lock for entity {1}. {2} has {3} lock", session.Name, entityId, lockItems[0].Session.Name), lockType.ToString());
return false;
}
}
return true;
}