利用异步I/O的优点,它现在很容易编码和编写(使用Await和TAP方法),我想知道,如果我们应该默认使用async,只需要在需要时使用sync来调整性能.
异步I/O释放调用线程,并允许在等待结果时执行其他操作.另一方面,异步I/O比同步慢一点.
为了实施响应式UI,WinRT设计人员认为提供仅异步方法是可以接受的.
内部的AFAIK Windows文件I/O是异步的.天真地看着这个,我不清楚为什么.NET中的异步文件I/O应该比同步慢.
我通常喜欢简单性和健壮性,只在必要时调整性能.在过去,我们默认使用同步,除了调用某些服务以及手机等平台强制执行异步.我们很少使用异步调整.
出于好奇,我想知道.NET任务调度程序支持多少个处理器内核.
Windows Server 2012最多支持640个核心.(是).NET限制为64还是会使用所有可用内核?
EF默认为无并发控制(最后写入获胜),允许丢失更新.通过在RowVersion列上设置ConcurrencyMode = Fixed,可以显式配置执行乐观并发检查.
我们如何在所有表中的RowVersion列上自动设置ConcurrencyMode = Fixed?在从数据库重新创建EF模型时必须手动执行此操作,我们可能会忘记它在没有并发控制的情况下运行.
我正在为我的下一个新应用评估EF.
如何全局更改应用程序中所有EF事务的IsolationLevel?例如:假设我想使用"Read Committed Snapshot".
虽然当我显然需要一个TransactionScope时指定IsolationLevel是可以的(参见下面的代码),但是在TransactionScope中封装每个EF保存操作会很难看.
'OK
Using tsc As New TransactionScope(TransactionScopeOption.RequiresNew, TransactionOption.ReadCommitted)
UpdateShoppingCart
EnqueueNewOrder
SendConfirmationEmail
tsc.Complete
End Using
'Is this really the only way to avoid Serializable?
Using tsc As New TransactionScope(TransactionScopeOption.RequiresNew, TransactionOption.ReadCommitted)
_ctx.SaveChanges()
tsc.Complete
End Using
Class TransactionOption
Public Shared ReadOnly ReadCommitted As New TransactionOptions() With {
.IsolationLevel = IsolationLevel.ReadCommitted,
.Timeout = TransactionManager.DefaultTimeout
}
End Class
Run Code Online (Sandbox Code Playgroud)
我认为混合IsolationLevles并不是一个好主意.我错了吗?
使用Serializable和SQL Server(与Oracle相反)插入简单无辜的读取可能会导致转换锁定死锁.
来自EF常见问题解答: "建议您使用READ COMMITTED事务,并使用READ COMMITTED SNAPSHOT ISOLATION,如果您需要让读者不阻止编写者和编写者不阻止读者."
我不明白为什么EF默认使用Serializable并且很难改变默认的隔离级别 - 使用SQL Server(与Oracle的多版本相比)默认为悲观的并发模型.配置选项应该很容易实现 - 或者我在这里遗漏了什么?
在审批工作流程中,我希望确保提醒电子邮件只发送一次.
使用SqlCommand.ExecuteNonQuery,我可以通过测试返回值来确保这一点.使用EF的推荐解决方案是什么?根据文档ObjectContext.SaveChanges不返回等效值.
SqlCommand示例:(在SendMail失败的情况下,TransactionScope用于回滚数据库更新.)
Dim sql = "UPDATE LeaveApprovalRequests SET State = 'Reminded'" &
" WHERE ID=3 AND State <>'Reminded'"
Using scope As New TransactionScope
Using cnx As New SqlConnection(My.Settings.connectionString)
cnx.Open()
Dim cmd As New SqlCommand(sql, cnx)
If 1 = cmd.ExecuteNonQuery Then
SendMail()
End If
scope.Complete()
End Using
End Using
通过启用乐观并发(在RowVersion属性上使用ConcurrencyMode = Fixed)并捕获OptimisticConcurrencyException,我能够识别对象是否实际在商店中更新.现在,TransactionScope(用于在SendMail失败时回滚数据库更新)会引发死锁错误.为什么?
Using scope As New TransactionScope
Using ctx As New ApprovalEntities
Try
Dim approval = ctx.LeaveApprovalRequests.
Where(Function(r) r.ID = 3 And r.State = "Created"
).FirstOrDefault
If approval Is Nothing …Run Code Online (Sandbox Code Playgroud) 评估.NET实体框架我尝试使用乐观并发模式找到正确的模式来处理并发更新.
在文档和许多其他地方我看到以下模式:
Try
' Try to save changes, which may cause a conflict.
Dim num As Integer = context.SaveChanges()
Console.WriteLine("No conflicts. " & num.ToString() & " updates saved.")
Catch generatedExceptionName As OptimisticConcurrencyException
' Resolve the concurrency conflict by refreshing the
' object context before re-saving changes.
context.Refresh(RefreshMode.ClientWins, orders)
' Save changes.
context.SaveChanges()
Console.WriteLine("OptimisticConcurrencyException handled and changes saved")
End Try
我看到了以下问题
这是正确的,还是我错过了什么?
在UI中,我通常让用户解决并发冲突:
Try
_ctx.SaveChanges()
Catch ex As OptimisticConcurrencyException
MessageBox.Show("Data was modified by another User." & vbCrLf … 使用共享相同事件处理程序的 FileSystemWatchers 安全吗?
让多个 FileSystemWatcher 使用相同的事件处理程序监视不同的目录是否安全?
Class Snippets
Private _watchPaths As New List(Of String) From {"x:\Dir1", "x:\Dir2"}
Private _watchers As List(Of FileSystemWatcher)
Private _newFiles As New BlockingCollection(Of String)
Sub Watch()
Dim _watchPaths As New List(Of String) From {"x:\Dir1", "x:\Dir2"}
Dim watchers As List(Of FileSystemWatcher)
For Each path In _watchPaths
Dim watcher As New FileSystemWatcher
AddHandler watcher.Created, Sub(s, e)
_trace.DebugFormat("New file {0}", e.FullPath)
'Do a little more stuff
_newFiles.Add(e.FullPath)
End Sub
Next
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
或者我们必须将 FileSystemWatcher 包装在如下所示的类中以使事件处理程序线程安全?
Class FileWatcher …Run Code Online (Sandbox Code Playgroud)