Changeset 3602
- Timestamp:
- 07/02/08 16:13:38 (4 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java
r3577 r3602 23 23 import javax.persistence.EntityManager; 24 24 import javax.persistence.EntityManagerFactory; 25 import javax.persistence.EntityTransaction;26 25 import javax.persistence.PersistenceException; 27 26 … … 32 31 import org.restlet.data.Request; 33 32 import org.restlet.data.Response; 34 import org.restlet.data.Status;35 33 import org.sarugo.restlet.jpa.resource.EntityCollection; 36 34 import org.sarugo.restlet.jpa.resource.EntityResource; … … 107 105 } 108 106 return em; 107 } 108 109 /** Clears the thread-local {@link EntityManager}. */ 110 public void clearEntityManager() { 111 entityManager.set(null); 109 112 } 110 113 … … 128 131 129 132 /** 130 * Handle a request by: 131 * <ol> 132 * <li>Creating an {@link EntityManager} and beginning a transaction.</li> 133 * <li>Sending the request to the {@link EntityFinder} which matches the 134 * request URL.</li> 135 * <li>Commiting (or rolling-back) the transaction and clearing the 136 * {@link EntityManager}.</li> 137 * </ol> 133 * Handle a request by delegating to the internal router which holds the 134 * mappings. For transation support see {@link TransactionFilter}. 138 135 */ 139 136 @Override 140 137 public void handle(Request request, Response response) { 141 EntityTransaction t = getEntityManager().getTransaction(); 142 t.begin(); 143 try { 144 router.handle(request, response); 145 if (t.isActive()) { 146 getEntityManager().flush(); 147 t.commit(); 148 } 149 } catch (RuntimeException e) { 150 getLogger().log(Level.SEVERE, 151 "Unhandled exception caught, rolling back transaction.", e); 152 if (t.isActive()) { 153 t.rollback(); 154 } 155 response.setStatus(Status.SERVER_ERROR_INTERNAL); 156 } finally { 157 entityManager.set(null); 158 } 138 router.handle(request, response); 159 139 } 160 140
