001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.batch;
018
019import javax.persistence.EntityManagerFactory;
020import javax.sql.DataSource;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
026import org.springframework.orm.jpa.JpaTransactionManager;
027import org.springframework.transaction.PlatformTransactionManager;
028
029/**
030 * A {@link BasicBatchConfigurer} tailored for JPA.
031 *
032 * @author Stephane Nicoll
033 */
034public class JpaBatchConfigurer extends BasicBatchConfigurer {
035
036        private static final Log logger = LogFactory.getLog(JpaBatchConfigurer.class);
037
038        private final EntityManagerFactory entityManagerFactory;
039
040        /**
041         * Create a new {@link BasicBatchConfigurer} instance.
042         * @param properties the batch properties
043         * @param dataSource the underlying data source
044         * @param transactionManagerCustomizers transaction manager customizers (or
045         * {@code null})
046         * @param entityManagerFactory the entity manager factory (or {@code null})
047         */
048        protected JpaBatchConfigurer(BatchProperties properties, DataSource dataSource,
049                        TransactionManagerCustomizers transactionManagerCustomizers,
050                        EntityManagerFactory entityManagerFactory) {
051                super(properties, dataSource, transactionManagerCustomizers);
052                this.entityManagerFactory = entityManagerFactory;
053        }
054
055        @Override
056        protected String determineIsolationLevel() {
057                logger.warn(
058                                "JPA does not support custom isolation levels, so locks may not be taken when launching Jobs");
059                return "ISOLATION_DEFAULT";
060        }
061
062        @Override
063        protected PlatformTransactionManager createTransactionManager() {
064                return new JpaTransactionManager(this.entityManagerFactory);
065        }
066
067}