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.classic.Session;
021    import org.hibernate.engine.SessionFactoryImplementor;
022    
023    import org.springframework.orm.hibernate3.SessionFactoryUtils;
024    import org.springframework.orm.hibernate3.SpringSessionContext;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class ShardSpringSessionContext extends SpringSessionContext {
030    
031            public ShardSpringSessionContext(SessionFactoryImplementor sessionFactory) {
032                    super(null);
033            }
034    
035            @Override
036            public Session currentSession() throws HibernateException {
037                    try {
038                            if (_shardSessionFactoryTargetSource == null) {
039                                    _shardSessionFactoryTargetSource =
040                                            (ShardSessionFactoryTargetSource)
041                                                    PortalBeanLocatorUtil.locate(
042                                                            "shardSessionFactoryTargetSource");
043                            }
044    
045                            return (Session)SessionFactoryUtils.doGetSession(
046                                    _shardSessionFactoryTargetSource.getSessionFactory(), false);
047                    }
048                    catch (IllegalStateException ise) {
049                            throw new HibernateException(ise);
050                    }
051            }
052    
053            private ShardSessionFactoryTargetSource _shardSessionFactoryTargetSource;
054    
055    }