Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add support for TransactionScope #2

Open
moozzyk opened this issue Nov 20, 2016 · 4 comments
Open

Feature Request: Add support for TransactionScope #2

moozzyk opened this issue Nov 20, 2016 · 4 comments

Comments

@moozzyk
Copy link
Owner

moozzyk commented Nov 20, 2016

Ported from: https://efcache.codeplex.com/workitem/10

REPORTED ON:
REPORTED BY:
Nov 5, 2014 at 7:28 PM
icnocop (contact)

Hi.

Thank you for EFCache.

I'd like to request support for caching within a transaction scope.

EDIT [moozzyk]
The code snippet is irrelevant since it does not use TransactionScope and should work correctly; See my comment below.
/EDIT

using (DbContextTransaction databaseTransaction = context.Database.BeginTransaction())
{
    try
    {
        // entity framework queries here

        // commit
        databaseTransaction.Commit();
    }
    catch (Exception)
    {
        // rollback
        databaseTransaction.Rollback();

        throw;
    }
}

Currently, the entity sets that are indicated as cache-able do not behave as being cached when queried and the database is explicitly queried every time.

Thank you.

comments

moozzyk wrote Nov 11, 2014 at 10:37 PM [x]

I agree it would be useful to add support for TransactionScope

However, the code snippet you provided is not relevant for two reasons:
it does not use TransactionScope at all - Database.BeginTransaction() will start a regular DbTransaction and will not create an ambient (potentially distributed) transaction TransactionScope would create
since the code you provided is not using a TransactionScope EF would call the DbTransactionInterceptor on commit and on rollback meaning that the cache would work correctly since it already intercepts transaction calls to invalidate cache on commit
icnocop wrote Dec 23, 2014 at 1:34 PM [x]

Thank you.

However, I noticed that the cache is not persisted using using BeginTransaction() within a loop.

For example:

// open database connection for performance reasons
for (int i = 0; i < 100; i++)
{
    // the Entity Framework cache is not persisted between items within the for loop
    // the database is queried for the same data that was previously queried within the DbContextTransaction scope in previous iterations
    using (DbContextTransaction databaseTransaction = context.Database.BeginTransaction())
    {
        try
        {
            // entity framework queries here
    
            // commit
            databaseTransaction.Commit();
        }
        catch (Exception)
        {
            // rollback
            databaseTransaction.Rollback();
    
            throw;
        }
    }
}

Thank you.

moozzyk wrote Dec 26, 2014 at 8:00 PM [x]

Yes, for simplicity results for queries issued inside transactions are not cached (you can see it here: https://github.com/moozzyk/EFCache/blob/master/EFCache/CacheTransactionHandler.cs). This is because I would have to track them separately and merge the when the transaction is committed. In general it is best practice to keep the transaction as short as possible and do as few reads as possible. Note that EF by default will only create a transaction when saving results to the database (i.e. when you call SaveChanges) and won't create transactions for queries.

@aborja79
Copy link

aborja79 commented Jun 20, 2020

Very good job.
Is there any fix to this problem. My business layer is using transactions in all methods and I can't use the cache inside.
Which is the problem?
What would happen if I comment the (transaction == null) check inside GetItem
Thank you

@moozzyk
Copy link
Owner Author

moozzyk commented Jun 20, 2020

You will get invalid data when a transaction is present.

@aborja79
Copy link

aborja79 commented Jun 28, 2020

I made some changes to support Transactions. Could you review it, please?
CacheTransactionHandler.cs.txt

Thank you

@moozzyk
Copy link
Owner Author

moozzyk commented Jun 28, 2020

Sorry, I don't offer consulting services. It also has been a few years since I did any serious development for .NET and re-focusing would require time I cannot afford at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants