Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 315422 - Enhanced EntityManagerImpl.unwrap to include additional session types
Summary: Enhanced EntityManagerImpl.unwrap to include additional session types
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Doug Clarke CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 244124
  Show dependency tree
 
Reported: 2010-06-02 12:41 EDT by Doug Clarke CLA
Modified: 2022-06-09 10:05 EDT (History)
3 users (show)

See Also:
gordon.yorke: review+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Doug Clarke CLA 2010-06-02 12:41:06 EDT
The unwrap method should be enhanced to support some additional internal session types to expand the usability of this method in combination with native API calls.

these sessions include:

- UnitOfWorkImpl

- ServerSession
- Server (interface)
- AbstractSession

The proposed method would look like:

    public <T> T unwrap(Class<T> cls) {
        try {
            if (cls.equals(UnitOfWork.class) || cls.equals(UnitOfWorkImpl.class)) {
                return (T) this.getUnitOfWork();
            } else if (cls.equals(JpaEntityManager.class)) {
                return (T) this;
            } else if (cls.equals(Session.class) || cls.equals(Server.class) || cls.equals(ServerSession.class) || cls.equals(AbstractSession.class)) {
                return (T) this.getServerSession();
            } else if (cls.equals(java.sql.Connection.class)) {
                UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.getUnitOfWork();
                if (!unitOfWork.isInTransaction()) {
                    unitOfWork.beginEarlyTransaction();
                    return (T) unitOfWork.getAccessor().getConnection();
                }
            }
            throw new PersistenceException(ExceptionLocalization.buildMessage("Provider-does-not-support-the-call", null));

        } catch (RuntimeException e) {
            throw e;
        }
    }
Comment 1 Doug Clarke CLA 2010-06-02 12:43:48 EDT
I am making this a blocker for 244124 (nested fetchgroup & LoadGroup) work since the suggested JpaHelper.load method will use this unwrap method to access the requires AbstractSession.
Comment 2 Andrei Ilitchev CLA 2010-06-03 11:00:36 EDT
    public <T> T unwrap(Class<T> cls) {
        try {
            if (cls.equals(UnitOfWork.class) || cls.equals(UnitOfWorkImpl.class) || cls.equals(RepeatableWriteUnitOfWork.class)) {
                return (T) this.getUnitOfWork();
            } else if (cls.equals(JpaEntityManager.class)) {
                return (T) this;
            } else if (cls.equals(Session.class) || cls.equals(Server.class) || cls.equals(ServerSession.class) || cls.equals(AbstractSession.class)) {            
                return (T) this.getServerSession();
            } else if (cls.equals(java.sql.Connection.class)) {
                UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl) this.getUnitOfWork();
                if (!unitOfWork.isInTransaction()) {
                    unitOfWork.beginEarlyTransaction();
                    return (T) unitOfWork.getAccessor().getConnection();
                }
            }
            throw new PersistenceException(ExceptionLocalization.buildMessage("Provider-does-not-support-the-call", null));

        } catch (RuntimeException e) {
            throw e;
        }
    }
Comment 3 Andrei Ilitchev CLA 2010-06-03 11:01:26 EDT
Cheched into trunk.
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:05:24 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink