Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 334704 - native queries do not use the shared cache
Summary: native queries do not use the shared cache
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Chris Delahunt CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-18 15:58 EST by Chris Delahunt CLA
Modified: 2022-06-09 10:14 EDT (History)
0 users

See Also:


Attachments
proposed patch for isolation level setting (4.09 KB, patch)
2011-01-28 13:42 EST, Chris Delahunt CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Delahunt CLA 2011-01-18 15:58:29 EST
prior to EclipseLink 2.2 RC1, native queries would return Entity data from the shared cache.  For instance:
EntityManager em = emf.getEntityManager();
em.find(Employee.class, 1);
em.clear();
em.getTransaction().begin();

Query query = em.createNativeQuery("Select ID From EMPLOYEE Where ID=1");
Employee emp = query.getSingleResult();

in EclipseLink 2.2 M6 and previous (2.1 etc), the native query be the same as em.find(Employee.class, 1), returning an entity with all attributes read in as they were in the initial find call.  

In 2.2 RC1, the shared cache is not used, and the Entity is built using the native query row - in this case it will return an Employee with only the ID set.
Comment 1 Chris Delahunt CLA 2011-01-18 16:06:28 EST
This is due to two reasons.  The following in EJBQueryImpl's executeReadQuery method seems to start the transaction early unnecessarily:

if (getDatabaseQueryInternal().isUserDefined()) {
                // and there is an active transaction
                if (this.entityManager.checkForTransaction(false) != null) {
                    // verify whether uow has begun early transaction
                    if (session.isUnitOfWork() && !((UnitOfWorkImpl) session).wasTransactionBegunPrematurely()) {
                        // uow begins early transaction in case it hasn't
                        // already begun.
                        // TODO: This is not good, it means that no SQL queries
                        // can ever use the cache,
                        // using isUserDefined to mean an SQL query is also
                        // wrong.
                        ((UnitOfWorkImpl) session).beginEarlyTransaction();
                    }
                }
            }


The other reason is that revision 8720 for bug 232063 changed the ClassDescriptor's default unitOfWorkCacheIsolationLevel from ISOLATE_NEW_DATA_AFTER_TRANSACTION to ISOLATE_CACHE_AFTER_TRANSACTION.
Comment 2 Chris Delahunt CLA 2011-01-28 13:42:40 EST
Created attachment 187862 [details]
proposed patch for isolation level setting

Proposed fix setting the UnitOfWorkCacheIsolationLevel to the original ISOLATE_NEW_DATA_AFTER_TRANSACTION default.
Comment 3 Chris Delahunt CLA 2011-01-28 15:26:45 EST
Fix checked into main (revision 8900) and 2.2 (r8901).
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:07:47 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:14:10 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink