我必須將一些數據從SQL Server 2008數據庫同步到MySql數據庫。 我正在使用C#和EF來管理數據。流是:從SQL服務器讀取未同步的行,將條目(通過類EntitiesConverter)轉換爲MySql數據庫實體,向MySql添加行,將SQL行標記爲已同步(並在兩個上下文中保存更改)。分佈式事務SQL Server <-> MySql。例外:MySQL Connector/Net目前不支持分佈式事務
代碼:
public static int SyncAttivitaToHiPlan(int start, int? end)
{
var options = new TransactionOptions();
options.IsolationLevel = IsolationLevel.Serializable;
using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
{
//MySql ObjectContext
var hpContext = new hiplanEntities1();
//SQL Server ObjectContext
var otoContext = new OTOCOMMEntities1();
var otoProvider = OtoCommEntitiesProvider.GetInstance();
IEnumerable<Attivita> attivitaEntries;
if (end == null)
attivitaEntries = otoProvider.GetUnsyncedAttivita(start);
else
attivitaEntries = otoProvider.GetUnsyncedAttivita(start, (int)end);
var tvavalavs = new ConcurrentQueue<tvavalav>();
//ObjectContext is not thread-safe
//attivitaEntries.AsParallel().AsOrdered().ForAll((a) => tvavalavs.Enqueue(EntitiesConverter.BuildTvavalavFromAttivita(a)));
foreach (var a in attivitaEntries)
{
tvavalavs.Enqueue(EntitiesConverter.BuildTvavalavFromAttivita(a));
}
foreach (var t in tvavalavs)
{
hpContext.tvavalav.AddObject(t);
}
// attivitaEntries.AsParallel().ForAll((a) => a.Synced = true);
foreach (var a in attivitaEntries)
{
a.Synced = true;
}
hpContext.SaveChanges();
otoContext.SaveChanges();
}
return 0; //TODO: return the number of synced rows.
}
}
當我嘗試它,我得到一個MySQL連接異常:「MySQL的連接器/網絡目前不支持分佈式事務。」
我該如何解決這個問題?有沒有辦法在單個事務中完成此操作(「虛擬」單身事務,因爲我正在使用2個不同的數據庫)而沒有發生該異常?