Helper class to simplify the development and generation of code that accesses TopLink through the SessionManager (sessions config XML). Responsibilities:
- Lookup of a session by name using default or provided sessions config location
- Support lookup of active UnitOfWork and Session in JTA environments
- Hot/Re-deployment handling of applications
- Detachment helpers to simplify usage within a local session bean
Basic usage example:
SessionFactory = sessionFactory = new SessionFactory("session-name"); ... public List read(Vector args) { Session session = sessionFactory.acquireSession(); List results = (List) session.executeQuery("query-name", MyClass.class, args); session.release(); return results; } public void write(MyClass detachedInstance) { UnitOfWork uow = sessionFactory.acquireUnitOfWork(); MyClass workingCopy = (MyClass) uow.readObject(detachedInstance); if (workingCopy == null) { throw new MyException("Cannot write changes. Object does not exist"); } uow.deepMergeClone(detachedInstance); uow.commit(); }
Detachment: The detach helper methods are provided to assist with the construction of applications. This helper class was designed for use within session beans (SB) and in the case of local SBs the objects returned are not serialized. Since EclipseLink's default behavior is to return the shared instance from the cache and rely on developers to only modify instances within a UnitOfWork this may be an issue. The client to the local session bean may try to modify the instance and thus corrupt the cache. By detaching the object the client to the session bean gets its own isolated copy that it can freely modify. This provides the same functionality as with a remote session bean and allows the developer the choice in how/when objects are detached.
Note: The above code example shows how a detached instance can have changes made to it persisted through use of the UnitOfWork merge API.
@author Doug Clarke & John Braken
@version 10.1.3
@since Dec 10, 2006