My first thought is to consider some unit of work pattern, but what I'm doing is pretty small and lightweight and I want to avoid overengineering my solution..
I would like to have a single transaction scope across more than one RavenDB database.. something like
class WidgetFacade
{
void SaveData (Widget widget, User user)
{
IDocumentStore documentStore = new DocumentStore { Url = "http://server:8080/" };
IDocumentSession session = documentStore.OpenSession("database1");
session.Store(user);
session = documentStore.OpenSession("database2");
session.Store(widget);
session.Savechanges();
session.Dispose();
}
}
You can't. Sessions are designed to operate on a single database.
However, you can get two sessions to participate in the same transaction.
var documentStore = new DocumentStore { Url = "http://server:8080/" };
using (var ts = new TransactionScope())
{
using (var session = documentStore.OpenSession("database1"))
{
session.Store(user);
session.SaveChanges();
}
using (var session = documentStore.OpenSession("database2"))
{
session.Store(widget);
session.SaveChanges();
}
ts.Complete();
}
If the second operation fails for any reason, the first will also be rolled back - even though you've saved changes and completed the session.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With