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.PersistenceException;
022
023 import org.apache.camel.impl.ServiceSupport;
024
025 import org.springframework.orm.jpa.JpaCallback;
026 import org.springframework.orm.jpa.JpaTemplate;
027 import org.springframework.orm.jpa.JpaTransactionManager;
028 import org.springframework.transaction.TransactionStatus;
029 import org.springframework.transaction.support.TransactionCallback;
030 import org.springframework.transaction.support.TransactionTemplate;
031
032 /**
033 * Delegates the strategy to the {@link JpaTemplate} and {@link TransactionTemplate} for transaction handling
034 *
035 * @version $Revision: 563665 $
036 */
037 public class JpaTemplateTransactionStrategy extends ServiceSupport implements TransactionStrategy {
038 private final JpaTemplate jpaTemplate;
039 private final TransactionTemplate transactionTemplate;
040
041 public JpaTemplateTransactionStrategy(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate) {
042 this.jpaTemplate = jpaTemplate;
043 this.transactionTemplate = transactionTemplate;
044 }
045
046 /**
047 * Creates a new implementation from the given JPA factory
048 */
049 public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf) {
050 JpaTemplate template = new JpaTemplate(emf);
051 return newInstance(emf, template);
052 }
053
054 public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf, JpaTemplate template) {
055 JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
056 transactionManager.afterPropertiesSet();
057
058 TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager);
059 tranasctionTemplate.afterPropertiesSet();
060
061 return new JpaTemplateTransactionStrategy(template, tranasctionTemplate);
062 }
063
064 public Object execute(final JpaCallback callback) {
065 return transactionTemplate.execute(new TransactionCallback() {
066 public Object doInTransaction(TransactionStatus status) {
067 return jpaTemplate.execute(new JpaCallback() {
068 public Object doInJpa(EntityManager entityManager) throws PersistenceException {
069 return callback.doInJpa(entityManager);
070 }
071 });
072 }
073 });
074 }
075
076 protected void doStart() throws Exception {
077 }
078
079 protected void doStop() throws Exception {
080 }
081 }