001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jpa;
019    
020    import org.apache.camel.impl.ServiceSupport;
021    import static org.apache.camel.util.ObjectHelper.notNull;
022    import org.springframework.orm.jpa.JpaCallback;
023    
024    import javax.persistence.EntityManager;
025    import javax.persistence.EntityManagerFactory;
026    import javax.persistence.EntityTransaction;
027    
028    /**
029     * @version $Revision: 525537 $
030     */
031    public class DefaultTransactionStrategy extends ServiceSupport implements TransactionStrategy {
032        private EntityManagerFactory entityManagerFactory;
033        private EntityManager entityManager;
034    
035        public DefaultTransactionStrategy(EntityManagerFactory entityManagerFactory) {
036            notNull(entityManagerFactory, "entityManagerFactory");
037            this.entityManagerFactory = entityManagerFactory;
038        }
039    
040        public DefaultTransactionStrategy(EntityManager entityManager) {
041            notNull(entityManager, "entityManager");
042            this.entityManager = entityManager;
043        }
044    
045        public Object execute(JpaCallback callback) {
046            EntityManager em = getEntityManager();
047            EntityTransaction transaction = em.getTransaction();
048            transaction.begin();
049    
050            try {
051                Object answer = callback.doInJpa(em);
052                transaction.commit();
053                return answer;
054            }
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    }