Changeset 3602

Show
Ignore:
Timestamp:
07/02/08 16:13:38 (4 years ago)
Author:
michael
Message:

Separate transaction handling. Closes #3, spent 0.5

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/java/org/sarugo/restlet/jpa/EntityRouter.java

    r3577 r3602  
    2323import javax.persistence.EntityManager; 
    2424import javax.persistence.EntityManagerFactory; 
    25 import javax.persistence.EntityTransaction; 
    2625import javax.persistence.PersistenceException; 
    2726 
     
    3231import org.restlet.data.Request; 
    3332import org.restlet.data.Response; 
    34 import org.restlet.data.Status; 
    3533import org.sarugo.restlet.jpa.resource.EntityCollection; 
    3634import org.sarugo.restlet.jpa.resource.EntityResource; 
     
    107105        } 
    108106        return em; 
     107    } 
     108 
     109    /** Clears the thread-local {@link EntityManager}. */ 
     110    public void clearEntityManager() { 
     111        entityManager.set(null); 
    109112    } 
    110113 
     
    128131 
    129132    /** 
    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}. 
    138135     */ 
    139136    @Override 
    140137    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); 
    159139    } 
    160140