Issue while saving the audit data into another database particularly during a Transaction scenario #712
Replies: 4 comments 3 replies
-
Could you share the code used to create the transaction or where the error occurs? Additionally, please provide the exception details, including the message and call stack trace |
Beta Was this translation helpful? Give feedback.
-
It's still unclear to me what you're trying to achieve. I notice multiple calls to Are both databases hosted on the same server? I tried reproducing the issue with the following code, and it works as expected, but I'm running both databases (app and audit) on the same MS SQL Server, just in different databases: TransactionManager.ImplicitDistributedTransactions = true;
using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
ctx.Values.Add(entity);
await ctx.SaveChangesAsync();
transactionScope.Complete(); builder.Services.AddDbContext<MyContext>(_ => _.UseSqlServer("data source=localhost;initial catalog=TestTran1;integrated security=true;Encrypt=False"));
builder.Services.AddDbContext<MyAuditContext>(_ => _.UseSqlServer("data source=localhost;initial catalog=TestTran2;integrated security=true;Encrypt=False"));
Audit.Core.Configuration.Setup()
.UseEntityFramework(d => d
.UseDbContext(ev => ev.GetEntityFrameworkEvent().GetDbContext().GetService<MyAuditContext>())
.AuditTypeMapper(t => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, entry, auditLog) =>
{
auditLog.Table = entry.Table;
auditLog.ChangeType = entry.Action;
auditLog.Value = entry.ToJson();
})
.IgnoreMatchedProperties(true)); |
Beta Was this translation helpful? Give feedback.
-
This seems to be a limitation for MySQL. Can you try with the suggestion in this SO answer? |
Beta Was this translation helpful? Give feedback.
-
If you use the MySqlDataProvider, the audit logs will not be inserted within the same transaction as the application database. In this case, I recommend using the EFDataProvider but avoid creating a TransactionScope. This limitation lies within the MySQL server and is unrelated to this library. My understanding is that TransactionScope will function across different connections only if your MySQL server supports XA transactions, which are available exclusively with the InnoDB engine. |
Beta Was this translation helpful? Give feedback.
-
I have an Application Db and audit Db, i want to log the audit records of Application into this audit Db. But my configuration fails during a transaction scenario saying multiple connections can't access same transaction
In program.cs :
So changes made to make it work:
When AuditDataProvider is set in program class it did not work as expected, instead setting audit provider in the AppDbContext stores the whole record into the Db as a single row but I want them as individual rows.. tried many ways but still no luck!
Beta Was this translation helpful? Give feedback.
All reactions