001/*
002 * Copyright 2002-2018 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.hibernate5;
018
019import javax.persistence.EntityManager;
020
021import org.hibernate.FlushMode;
022import org.hibernate.Session;
023import org.hibernate.Transaction;
024
025import org.springframework.lang.Nullable;
026import org.springframework.orm.jpa.EntityManagerHolder;
027
028/**
029 * Resource holder wrapping a Hibernate {@link Session} (plus an optional {@link Transaction}).
030 * {@link HibernateTransactionManager} binds instances of this class to the thread,
031 * for a given {@link org.hibernate.SessionFactory}. Extends {@link EntityManagerHolder}
032 * as of 5.1, automatically exposing an {@code EntityManager} handle on Hibernate 5.2+.
033 *
034 * <p>Note: This is an SPI class, not intended to be used by applications.
035 *
036 * @author Juergen Hoeller
037 * @since 4.2
038 * @see HibernateTransactionManager
039 * @see SessionFactoryUtils
040 */
041public class SessionHolder extends EntityManagerHolder {
042
043        private final Session session;
044
045        @Nullable
046        private Transaction transaction;
047
048        @Nullable
049        private FlushMode previousFlushMode;
050
051
052        public SessionHolder(Session session) {
053                // Check below is always true against Hibernate >= 5.2 but not against 5.0/5.1 at runtime
054                super(EntityManager.class.isInstance(session) ? session : null);
055                this.session = session;
056        }
057
058
059        public Session getSession() {
060                return this.session;
061        }
062
063        public void setTransaction(@Nullable Transaction transaction) {
064                this.transaction = transaction;
065                setTransactionActive(transaction != null);
066        }
067
068        @Nullable
069        public Transaction getTransaction() {
070                return this.transaction;
071        }
072
073        public void setPreviousFlushMode(@Nullable FlushMode previousFlushMode) {
074                this.previousFlushMode = previousFlushMode;
075        }
076
077        @Nullable
078        public FlushMode getPreviousFlushMode() {
079                return this.previousFlushMode;
080        }
081
082
083        @Override
084        public void clear() {
085                super.clear();
086                this.transaction = null;
087                this.previousFlushMode = null;
088        }
089
090}