001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.shard;
016    
017    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
018    
019    import org.hibernate.HibernateException;
020    import org.hibernate.SessionFactory;
021    import org.hibernate.classic.Session;
022    import org.hibernate.engine.SessionFactoryImplementor;
023    
024    import org.springframework.orm.hibernate3.SessionFactoryUtils;
025    import org.springframework.orm.hibernate3.SpringSessionContext;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ShardSpringSessionContext extends SpringSessionContext {
031    
032            public ShardSpringSessionContext(SessionFactoryImplementor sessionFactory) {
033                    super(null);
034            }
035    
036            @Override
037            public Session currentSession() throws HibernateException {
038                    try {
039                            if (_sessionFactory == null) {
040                                    _sessionFactory = (SessionFactory)PortalBeanLocatorUtil.locate(
041                                            "liferayHibernateSessionFactory");
042                            }
043    
044                            return (Session)SessionFactoryUtils.doGetSession(
045                                    _sessionFactory, false);
046                    }
047                    catch (IllegalStateException ise) {
048                            throw new HibernateException(ise);
049                    }
050            }
051    
052            private SessionFactory _sessionFactory;
053    
054    }