001/* 002 * Copyright 2002-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 * 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.support; 018 019import org.aopalliance.intercept.MethodInterceptor; 020import org.aopalliance.intercept.MethodInvocation; 021import org.hibernate.FlushMode; 022import org.hibernate.HibernateException; 023import org.hibernate.Session; 024import org.hibernate.SessionFactory; 025 026import org.springframework.beans.factory.InitializingBean; 027import org.springframework.dao.DataAccessResourceFailureException; 028import org.springframework.lang.Nullable; 029import org.springframework.orm.hibernate5.SessionFactoryUtils; 030import org.springframework.orm.hibernate5.SessionHolder; 031import org.springframework.transaction.support.TransactionSynchronizationManager; 032import org.springframework.util.Assert; 033 034/** 035 * Simple AOP Alliance {@link MethodInterceptor} implementation that binds a new 036 * Hibernate {@link Session} for each method invocation, if none bound before. 037 * 038 * <p>This is a simple Hibernate Session scoping interceptor along the lines of 039 * {@link OpenSessionInViewInterceptor}, just for use with AOP setup instead of 040 * MVC setup. It opens a new {@link Session} with flush mode "MANUAL" since the 041 * Session is only meant for reading, except when participating in a transaction. 042 * 043 * @author Juergen Hoeller 044 * @since 4.2 045 * @see OpenSessionInViewInterceptor 046 * @see OpenSessionInViewFilter 047 * @see org.springframework.orm.hibernate5.HibernateTransactionManager 048 * @see TransactionSynchronizationManager 049 * @see SessionFactory#getCurrentSession() 050 */ 051public class OpenSessionInterceptor implements MethodInterceptor, InitializingBean { 052 053 @Nullable 054 private SessionFactory sessionFactory; 055 056 057 /** 058 * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions. 059 */ 060 public void setSessionFactory(@Nullable SessionFactory sessionFactory) { 061 this.sessionFactory = sessionFactory; 062 } 063 064 /** 065 * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions. 066 */ 067 @Nullable 068 public SessionFactory getSessionFactory() { 069 return this.sessionFactory; 070 } 071 072 @Override 073 public void afterPropertiesSet() { 074 if (getSessionFactory() == null) { 075 throw new IllegalArgumentException("Property 'sessionFactory' is required"); 076 } 077 } 078 079 080 @Override 081 public Object invoke(MethodInvocation invocation) throws Throwable { 082 SessionFactory sf = getSessionFactory(); 083 Assert.state(sf != null, "No SessionFactory set"); 084 085 if (!TransactionSynchronizationManager.hasResource(sf)) { 086 // New Session to be bound for the current method's scope... 087 Session session = openSession(sf); 088 try { 089 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 090 return invocation.proceed(); 091 } 092 finally { 093 SessionFactoryUtils.closeSession(session); 094 TransactionSynchronizationManager.unbindResource(sf); 095 } 096 } 097 else { 098 // Pre-bound Session found -> simply proceed. 099 return invocation.proceed(); 100 } 101 } 102 103 /** 104 * Open a Session for the given SessionFactory. 105 * <p>The default implementation delegates to the {@link SessionFactory#openSession} 106 * method and sets the {@link Session}'s flush mode to "MANUAL". 107 * @param sessionFactory the SessionFactory to use 108 * @return the Session to use 109 * @throws DataAccessResourceFailureException if the Session could not be created 110 * @since 5.0 111 * @see FlushMode#MANUAL 112 */ 113 @SuppressWarnings("deprecation") 114 protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { 115 Session session = openSession(); 116 if (session == null) { 117 try { 118 session = sessionFactory.openSession(); 119 session.setFlushMode(FlushMode.MANUAL); 120 } 121 catch (HibernateException ex) { 122 throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); 123 } 124 } 125 return session; 126 } 127 128 /** 129 * Open a Session for the given SessionFactory. 130 * @deprecated as of 5.0, in favor of {@link #openSession(SessionFactory)} 131 */ 132 @Deprecated 133 @Nullable 134 protected Session openSession() throws DataAccessResourceFailureException { 135 return null; 136 } 137 138}