001/* 002 * Copyright 2002-2012 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.hibernate3; 018 019import org.hibernate.HibernateException; 020import org.hibernate.classic.Session; 021import org.hibernate.context.CurrentSessionContext; 022import org.hibernate.engine.SessionFactoryImplementor; 023 024/** 025 * Implementation of Hibernate 3.1's CurrentSessionContext interface 026 * that delegates to Spring's SessionFactoryUtils for providing a 027 * Spring-managed current Session. 028 * 029 * <p>Used by Spring's {@link LocalSessionFactoryBean} when told to expose a 030 * transaction-aware SessionFactory. This is the default as of Spring 2.5. 031 * 032 * <p>This CurrentSessionContext implementation can also be specified in custom 033 * SessionFactory setup through the "hibernate.current_session_context_class" 034 * property, with the fully qualified name of this class as value. 035 * 036 * @author Juergen Hoeller 037 * @since 2.0 038 * @see SessionFactoryUtils#doGetSession 039 * @see LocalSessionFactoryBean#setExposeTransactionAwareSessionFactory 040 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 041 */ 042@Deprecated 043@SuppressWarnings("serial") 044public class SpringSessionContext implements CurrentSessionContext { 045 046 private final SessionFactoryImplementor sessionFactory; 047 048 049 /** 050 * Create a new SpringSessionContext for the given Hibernate SessionFactory. 051 * @param sessionFactory the SessionFactory to provide current Sessions for 052 */ 053 public SpringSessionContext(SessionFactoryImplementor sessionFactory) { 054 this.sessionFactory = sessionFactory; 055 } 056 057 058 /** 059 * Retrieve the Spring-managed Session for the current thread, if any. 060 */ 061 @Override 062 public Session currentSession() throws HibernateException { 063 try { 064 return (org.hibernate.classic.Session) SessionFactoryUtils.doGetSession(this.sessionFactory, false); 065 } 066 catch (IllegalStateException ex) { 067 throw new HibernateException(ex.getMessage()); 068 } 069 } 070 071}