001/* 002 * Copyright 2002-2015 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 org.hibernate.FlushMode; 020import org.hibernate.Session; 021import org.hibernate.Transaction; 022 023import org.springframework.transaction.support.ResourceHolderSupport; 024import org.springframework.util.Assert; 025 026/** 027 * Session holder, wrapping a Hibernate Session and a Hibernate Transaction. 028 * HibernateTransactionManager binds instances of this class to the thread, 029 * for a given SessionFactory. 030 * 031 * <p>Note: This is an SPI class, not intended to be used by applications. 032 * 033 * @author Juergen Hoeller 034 * @since 4.2 035 * @see HibernateTransactionManager 036 * @see SessionFactoryUtils 037 */ 038public class SessionHolder extends ResourceHolderSupport { 039 040 private Session session; 041 042 private Transaction transaction; 043 044 private FlushMode previousFlushMode; 045 046 047 public SessionHolder(Session session) { 048 Assert.notNull(session, "Session must not be null"); 049 this.session = session; 050 } 051 052 public Session getSession() { 053 return this.session; 054 } 055 056 public void setTransaction(Transaction transaction) { 057 this.transaction = transaction; 058 } 059 060 public Transaction getTransaction() { 061 return this.transaction; 062 } 063 064 public void setPreviousFlushMode(FlushMode previousFlushMode) { 065 this.previousFlushMode = previousFlushMode; 066 } 067 068 public FlushMode getPreviousFlushMode() { 069 return this.previousFlushMode; 070 } 071 072 073 @Override 074 public void clear() { 075 super.clear(); 076 this.transaction = null; 077 this.previousFlushMode = null; 078 } 079 080}