Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:56:48+00:00 2026-05-27T22:56:48+00:00

With RavenDB, creating an IDocumentSession upon app start-up (and never closing it until the

  • 0

With RavenDB, creating an IDocumentSession upon app start-up (and never closing it until the app is closed), allows me to use optimistic concurrency by doing this:

public class GenericData : DataAccessLayerBase, IGenericData
{
    public void Save<T>(T objectToSave)
    {
        Guid eTag = (Guid)Session.Advanced.GetEtagFor(objectToSave);
        Session.Store(objectToSave, eTag);
        Session.SaveChanges();
    }
}

If another user has changed that object, then the save will correctly fail.

But what I can’t do, when using one session for the lifetime of an app, is seeing changes, made by other instances of the app (say, Joe, five cubicles away), to documents. When I do this, I don’t see Joe’s changes:

public class CustomVariableGroupData : DataAccessLayerBase, ICustomVariableGroupData
{
    public IEnumerable<CustomVariableGroup> GetAll()
    {
        return Session.Query<CustomVariableGroup>();
    }
}

Note: I’ve also tried this, but it didn’t display Joe’s changes either:

return Session.Query<CustomVariableGroup>().Customize(x => x.WaitForNonStaleResults());

Now, if I go the other way, and create an IDocumentSession within every method that accesses the database, then I have the opposite problem. Because I have a new session, I can see Joe’s changes. Buuuuuuut… then I lose optimistic concurrency. When I create a new session before saving, this line produces an empty GUID, and therefore fails:

Guid eTag = (Guid)Session.Advanced.GetEtagFor(objectToSave);

What am I missing? If a Session shouldn’t be created within each method, nor at the app level, then what is the correct scope? How can I get the benefits of optimistic concurrency and the ability to see others’ changes when doing a Session.Query()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:56:49+00:00Added an answer on May 27, 2026 at 10:56 pm

    Disclaimer: I know this can’t be the long-term approach, and therefore won’t be an accepted answer here. However, I simply need to get something working now, and I can refactor later. I also know some folks will be disgusted with this approach, lol, but so be it. It seems to be working. I get new data with every query (new session), and I get optimistic concurrency working as well.

    The bottom line is that I went back to one session per data access method. And whenever a data access method does some type of get/load/query, I store the eTags in a static dictionary:

    public IEnumerable<CustomVariableGroup> GetAll()
    {
        using (IDocumentSession session = Database.OpenSession())
        {
            IEnumerable<CustomVariableGroup> groups = session.Query<CustomVariableGroup>();
            CacheEtags(groups, session);
            return groups;
        }
    }
    

    Then, when I’m saving data, I grab the eTag from the cache. This causes a concurrency exception if another instance has modified the data, which is what I want.

    public void Save(EntityBase objectToSave)
    {
        if (objectToSave == null) { throw new ArgumentNullException("objectToSave"); }
    
        Guid eTag = Guid.Empty;
    
        if (objectToSave.Id != null)
        {
            eTag = RetrieveEtagFromCache(objectToSave);
        }
    
        using (IDocumentSession session = Database.OpenSession())
        {
            session.Advanced.UseOptimisticConcurrency = true;
            session.Store(objectToSave, eTag);
            session.SaveChanges();
            CacheEtag(objectToSave, session);  // We have a new eTag after saving.
        }
    }
    

    I absolutely want to do this the right way in the long run, but I don’t know what that way is yet.

    Edit: I’m going to make this the accepted answer until I find a better way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying migrating an existing web application to use RavenDB. I currently have pages
I've never used RavenDb it looks interesting but I'm falling at the first hurdle.
I recently started using RavenDb. I am converting a relational dbase to use RavenDb.
I am using ravendb 1.0.531 in an MVC 3 app to store my data.
Is there any way to tell RavenDb to use WaitForNonStaleResults mode for all queries
Hi I'm trying to use RavenDB to serialize F# collections. This is no problem
I want to use RavenDB with ninject in my asp.net mvc3 project, Any idea
Building a .NET4 app with RavenDB using Mono 2.10.8. When I try to do
I am new to RavenDB and from my understanding, when you ask for the
I am using RavenDB in In-Memory mode for unit testing. My queries are backed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.