001/*
002 * Copyright 2002-2007 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 *      https://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.orm.jpa;
018
019import javax.persistence.EntityManager;
020
021import org.springframework.transaction.SavepointManager;
022import org.springframework.transaction.support.ResourceHolderSupport;
023import org.springframework.util.Assert;
024
025/**
026 * Holder wrapping a JPA EntityManager.
027 * JpaTransactionManager binds instances of this class to the thread,
028 * for a given EntityManagerFactory.
029 *
030 * <p>Note: This is an SPI class, not intended to be used by applications.
031 *
032 * @author Juergen Hoeller
033 * @since 2.0
034 * @see JpaTransactionManager
035 * @see EntityManagerFactoryUtils
036 */
037public class EntityManagerHolder extends ResourceHolderSupport {
038
039        private final EntityManager entityManager;
040
041        private boolean transactionActive;
042
043        private SavepointManager savepointManager;
044
045
046        public EntityManagerHolder(EntityManager entityManager) {
047                Assert.notNull(entityManager, "EntityManager must not be null");
048                this.entityManager = entityManager;
049        }
050
051
052        public EntityManager getEntityManager() {
053                return this.entityManager;
054        }
055
056        protected void setTransactionActive(boolean transactionActive) {
057                this.transactionActive = transactionActive;
058        }
059
060        protected boolean isTransactionActive() {
061                return this.transactionActive;
062        }
063
064        protected void setSavepointManager(SavepointManager savepointManager) {
065                this.savepointManager = savepointManager;
066        }
067
068        protected SavepointManager getSavepointManager() {
069                return this.savepointManager;
070        }
071
072        @Override
073        public void clear() {
074                super.clear();
075                this.transactionActive = false;
076                this.savepointManager = null;
077        }
078
079}