1 /* 2 * Copyright 2002-2006 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.orm.toplink; 18 19 import oracle.toplink.exceptions.TopLinkException; 20 import oracle.toplink.sessions.Session; 21 22 /** 23 * Callback interface for TopLink code. To be used with {@link TopLinkTemplate}'s 24 * execution methods, often as anonymous classes within a method implementation. 25 * A typical implementation will call TopLink Session CRUD to perform some 26 * operations on persistent objects. 27 * 28 * <p>The <code>Session</code> that gets passed into the <code>doInTopLink</code> method 29 * is usually a thread-safe <code>ClientSession</code>. Since this provides access to the 30 * TopLink shared cache, it is possible for implementations of this interface to return 31 * references to <i>read-only objects from the shared cache</i>. These objects 32 * <i>must not be modified</i> by application code outside of the DAO layer. 33 * If persistent objects need to be edited, they should be loaded from (or registered with) 34 * a TopLink UnitOfWork, or they should be explicitly copied and merged back into a 35 * <code>UnitOfWork</code> at a later point of time. 36 * 37 * <p>Users can access a <code>UnitOfWork</code> by using the <code>getActiveUnitOfWork</code> 38 * method on the <code>Session</code>. Normally, this will only be done when there is an 39 * active non-read-only transaction being managed by Spring's {@link TopLinkTransactionManager} 40 * or by an external transaction controller (usually a J2EE server's JTA provider, 41 * configured in TopLink). The <code>getActiveUnitOfWork</code> method will return 42 * <code>null</code> outside of a managed transaction. 43 * 44 * @author Juergen Hoeller 45 * @author <a HREF="mailto:@james.x.clark@oracle.com">James Clark</a> 46 * @see TopLinkTemplate 47 * @see TopLinkTransactionManager 48 */ 49 public interface TopLinkCallback { 50 51 /** 52 * Gets called by <code>TopLinkTemplate.execute</code> with an active 53 * <code>Session</code>. Does not need to care about activating or closing 54 * the TopLink <code>Session</code>, or handling transactions. 55 * 56 * <p>Note that write operations should usually be performed on the active 57 * <code>UnitOfWork</code> within an externally controlled transaction, through 58 * calling <code>getActiveUnitOfWork</code>. However, an implementation can also 59 * choose to use <code>acquireUnitOfWork</code> to create an independent 60 * <code>UnitOfWork</code>, which it needs to commit at the end of the operation. 61 * 62 * <p>Allows for returning a result object created within the callback, 63 * i.e. a domain object or a collection of domain objects. 64 * A thrown custom RuntimeException is treated as an application exception: 65 * It gets propagated to the caller of the template. 66 * 67 * @param session active TopLink Session 68 * @return a result object, or <code>null</code> if none 69 * @throws TopLinkException if thrown by the TopLink API 70 * @see oracle.toplink.sessions.Session#getActiveUnitOfWork() 71 * @see oracle.toplink.sessions.Session#acquireUnitOfWork() 72 * @see TopLinkTemplate#execute 73 * @see TopLinkTemplate#executeFind 74 */ 75 Object doInTopLink(Session session) throws TopLinkException; 76 77 } 78