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 org.springframework.orm.jpa.JpaCallback; 022 import org.springframework.orm.jpa.JpaTemplate; 023 import org.springframework.orm.jpa.JpaTransactionManager; 024 import org.springframework.transaction.TransactionStatus; 025 import org.springframework.transaction.support.TransactionCallback; 026 import org.springframework.transaction.support.TransactionTemplate; 027 028 import javax.persistence.EntityManager; 029 import javax.persistence.EntityManagerFactory; 030 import javax.persistence.PersistenceException; 031 032 /** 033 * Delegates the strategy to the {@link JpaTemplate} and {@link TransactionTemplate} for transaction handling 034 * 035 * @version $Revision: 525537 $ 036 */ 037 public class JpaTemplateTransactionStrategy extends ServiceSupport implements TransactionStrategy { 038 private final JpaTemplate jpaTemplate; 039 private final TransactionTemplate transactionTemplate; 040 041 /** 042 * Creates a new implementation from the given JPA factory 043 */ 044 public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf) { 045 JpaTemplate template = new JpaTemplate(emf); 046 return newInstance(emf, template); 047 } 048 049 public static JpaTemplateTransactionStrategy newInstance(EntityManagerFactory emf, JpaTemplate template) { 050 JpaTransactionManager transactionManager = new JpaTransactionManager(emf); 051 transactionManager.afterPropertiesSet(); 052 053 TransactionTemplate tranasctionTemplate = new TransactionTemplate(transactionManager); 054 tranasctionTemplate.afterPropertiesSet(); 055 056 return new JpaTemplateTransactionStrategy(template, tranasctionTemplate); 057 } 058 059 public JpaTemplateTransactionStrategy(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate) { 060 this.jpaTemplate = jpaTemplate; 061 this.transactionTemplate = transactionTemplate; 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 }