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