【问题标题】:Always picking the first datasource when 2 datasources exist存在 2 个数据源时始终选择第一个数据源
【发布时间】:2019-08-27 20:21:59
【问题描述】:

我在 websphere 上使用 Spring 和 Hibernate 并尝试对数据库进行查询。我正在导入的 2 个不同项目中有 2 个数据源,我的问题是我只选择了一个数据源。这是每个项目的休眠 XML:

项目一:

<tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DbSource1" />
        <property name="mappingResources">

项目 2:

<bean id="SessionFactory2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DbSource2" />
    <property name="mappingResources">

-- 还有存储过程的 JDBC 模板

  <bean id="retrievalService" class="xx.xx.xx.JDBCRetrievalService">
    <property name="jdbcTemplate" ref="jdbcTemplate" />   </bean>

  <bean id="transactionalService" class="xx.xx.xx.JDBCTransactionalService">
    <property name="jdbcTemplate" ref="jdbcTemplate" />   </bean>


In my project application context, I simply import the 2 xmls above 
<import resource="classpath:DBsource-hibernate1.xml" />
  <import resource="classpath:DBSource-hibernate2.xml" />

现在在我的代码中我有以下内容:

public abstract class HibernateBaseDao implements Serializable {

    private static final long serialVersionUID = -8688473006487128511L;



    /** Hibernate Session factory. */
    @Qualifier("SessionFactory2")
    private SessionFactory sessionFactory;


    protected HibernateBaseDao() {
        this.sessionFactory = null;
    }


    protected SessionFactory getSessionFactory() {
        return sessionFactory;
    }


    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    protected Session getSession() {
        return sessionFactory != null ? sessionFactory.getCurrentSession() : null;
    }

}

@Component("MyDao")
public class MyDaoImpl
    extends HibernateBaseDao
    implements MyDaoI{

@Override
public int getValueFromDatabase(){
   SQLQuery sqlQuery =  (SQLQuery) createSQLQuery("select uservalue from MyTable");
   List<Integer> values= sqlQuery.list();
   values.get(0);
}

}

最后是我调用它的代码:

@自动连线 MyDao myDao;

    @Override
@Transactional()
public void runQuery() throws Exception
{
      myDao.getValueFromDatabase

    }

它返回说不存在表的问题。经过调查,我发现它正在选择第一个数据源。 因为这个,我已经拉了2天的头发。有什么建议???我正在扩展的基类中已经有 SessionFactory2 的限定符

非常感谢

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    您的问题是您没有在属性中注入 SessionFactory2。 @Qualifier 自己什么都不做,你需要添加@Autowire

    解决办法是:

    /** Hibernate Session factory. */
        @Autowire
        @Qualifier("SessionFactory2")
        private SessionFactory sessionFactory;
    

    此外,您不需要 getter/setter 来在私有属性上使用 @Autowired ;)

    【讨论】:

    • 我添加了自动连线。它仍在获取第一个数据源而不是第二个
    • 您是否尝试过删除 setter 中的 @Autowire?公共无效 setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }。请记住,此方法不是必需的(除非您明确将其用于其他任何事情)
    • 不过这并没有什么不同
    • 好的,我看到了,问题是你使用的是@Transactional()。使用事务涉及您使用“sessionFactory”配置的事务管理器。您应该从查询方法中删除 @Transactional (对于单个查询,它不是必需的)。如果您需要进一步的事务控制,我猜您将需要使用两个 TxManager。这是一个例子:stackoverflow.com/questions/4423125/….
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多