001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.jpa;
018    
019    import javax.persistence.EntityManager;
020    import javax.persistence.EntityManagerFactory;
021    import javax.persistence.EntityTransaction;
022    
023    import org.apache.camel.impl.ServiceSupport;
024    
025    import org.springframework.orm.jpa.JpaCallback;
026    
027    import static org.apache.camel.util.ObjectHelper.notNull;
028    
029    /**
030     * @version $Revision: 563665 $
031     */
032    public class DefaultTransactionStrategy extends ServiceSupport implements TransactionStrategy {
033        private EntityManagerFactory entityManagerFactory;
034        private EntityManager entityManager;
035    
036        public DefaultTransactionStrategy(EntityManagerFactory entityManagerFactory) {
037            notNull(entityManagerFactory, "entityManagerFactory");
038            this.entityManagerFactory = entityManagerFactory;
039        }
040    
041        public DefaultTransactionStrategy(EntityManager entityManager) {
042            notNull(entityManager, "entityManager");
043            this.entityManager = entityManager;
044        }
045    
046        public Object execute(JpaCallback callback) {
047            EntityManager em = getEntityManager();
048            EntityTransaction transaction = em.getTransaction();
049            transaction.begin();
050    
051            try {
052                Object answer = callback.doInJpa(em);
053                transaction.commit();
054                return answer;
055            } catch (RuntimeException e) {
056                if (transaction != null) {
057                    transaction.rollback();
058                }
059                throw e;
060            }
061        }
062    
063        public EntityManager getEntityManager() {
064            if (entityManager == null) {
065                entityManager = entityManagerFactory.createEntityManager();
066            }
067            return entityManager;
068        }
069    
070        protected void doStart() throws Exception {
071            // force lazy construction
072            getEntityManager();
073        }
074    
075        protected void doStop() throws Exception {
076            if (entityManager != null) {
077                entityManager.close();
078            }
079        }
080    }