Home Articles

使用Hibernate的Spring mutliple数据源给出:No Session found错误

Asked
Viewed 1928 times
0

我正在使用Spring 4和Hibernate 4并尝试配置多个数据源 . 当我使用新的第二个时,我总是得到一个错误 .

package org.miso.vre.config;

import java.util.Properties;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan("org.miso.vre")
@PropertySource("classpath:database.properties")
public class DatabaseConfig {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_DATABASE_MISOFT_DRIVER = "db.misoft.driver";
    private static final String PROPERTY_NAME_DATABASE_MISOFT_PASSWORD = "db.misoft.password";
    private static final String PROPERTY_NAME_DATABASE_MISOFT_URL = "db.misoft.url";
    private static final String PROPERTY_NAME_DATABASE_MISOFT_USERNAME = "db.misoft.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource;
    }

    @Bean
    public DataSource dataSourceMISOFT() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_MISOFT_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_MISOFT_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_MISOFT_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_MISOFT_PASSWORD));

        return dataSource;
    }

    private Properties hibProperties() {
        Properties properties = new Properties();
        properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
        return properties;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource());
        sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        sessionFactoryBean.setHibernateProperties(hibProperties());
        return sessionFactoryBean;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactoryMISOFT() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSourceMISOFT());
        sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
        sessionFactoryBean.setHibernateProperties(hibProperties());
        return sessionFactoryBean;
    }
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.miso.vre.model.VRE_Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Repository
public class VRETestTableDAOImpl implements VRETestTableDAO {

    @Autowired
    @Qualifier(value="sessionFactoryMISOFT")
    private SessionFactory sessionFactoryMISOFT;

    private Session getCurrentSession() {
        return sessionFactoryMISOFT.getCurrentSession();
    }

    public VRE_Record getVRE_Record(int id) {
        VRE_Record vRE_Record = (VRE_Record) getCurrentSession().get(VRE_Record.class, id);
        return vRE_Record;
    }
}
package org.miso.vre.service;

import org.miso.vre.dao.VRETestTableDAO;
import org.miso.vre.model.VRE_Record;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by PDuer on 12/29/2015.
 */
@Service
@Transactional
public class TestServiceImpl implements TestService {

    @Autowired
    private VRETestTableDAO vreTestTableDAO;

    @Override
    public VRE_Record getVRERecord (int id) {
        return vreTestTableDAO.getVRE_Record(id);
    }
}

exception org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.HibernateException:找不到当前线程的会话org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822 )javax.servlet.http.HttpServlet.service(HttpServlet.java:622)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)javax.servlet.http.HttpServlet.service(httpServlet.java:729 )org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73)root cause org.hibernate .HibernateException:找不到当前线程的会话org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1013)org.miso.vre.dao .VRETestTableDAOImpl.getCurrentSession(VRETestTableDAOImpl.java:18)org .miso.vre.dao.VRETestTableDAOImpl.getVRE_Record(VRETestTableDAOImpl.java:22)org.miso.vre.service.TestServiceImpl.getVRERecord(TestServiceImpl.java:22)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect . NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

1 Answer

  • 0

    原因是, sessionFactoryMISOFT 与任何事务管理器都没有关联 . 只有 sessionFactory 通过 transactionManager() 方法与事务管理器关联 .

    您需要创建一个类似的方法,例如 transactionManagerMISOFT() 并将 sessionFactoryMISOFT 与此新的transactionManager相关联 .

    当使用@Transaction属性时,我们需要指定需要使用哪个transactionManager .

    在我的例子中,我已经定义了两个sessionFactories和两个transactionManager方法,如下所示:

    @Bean
    public HibernateTransactionManager transactionManagerABC() {
        return new HibernateTransactionManager(sessionFactoryABC().getObject());
    }
    
    @Bean
    public HibernateTransactionManager transactionManagerXYZ() {
        return new HibernateTransactionManager(sessionFactoryXYZ().getObject());
    }
    

    在使用它时,我在您的服务类中指定了transactionManagerABC或transactionManagerXYZ以及Transactional属性,如下所示:

    @Transactional(transactionManager="transactionManagerABC")
    public Forest getForestById(int id) {
        return forestDAO.getForestById(id);
    }
    

    您需要为sessionFactory使用带有Autowired注释的限定符,我相信您已经拥有:

    @Qualifier("sessionFactoryABC")
    private SessionFactory sessionFactory;
    

Related