Spring hibernate配置双数据源动态切换失败

发布于 2022-09-05 03:23:40 字数 6451 浏览 15 评论 0

问题描述:需要连接到两个mysql,根据网上挺详细的教程 配置教程 进行配置,但是动态切换还是失败,所有配置都与链接中相同,

 <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <!-- MySQL5 -->
              <property name="driverClass" value="${driverClassName}"></property>
              <property name="jdbcUrl" value="${zabbix_url}"></property>
              <property name="user" value="${zabbix_username}"></property>
              <property name="password" value="${zabbix_password}"></property>
              <property name="maxPoolSize" value="40"></property>
              <property name="minPoolSize" value="1"></property>
              <property name="initialPoolSize" value="1"></property>
              <property name="maxIdleTime" value="20"></property>
       </bean>

       <!--&lt;!&ndash; 使用C3P0数据源,two MySQL数据库 &ndash;&gt;-->
       <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <!-- MySQL5 -->
              <property name="driverClass" value="${driverClassName}"></property>
              <property name="jdbcUrl" value="${dataset_url}"></property>
              <property name="user" value="${dataset_username}"></property>
              <property name="password" value="${dataset_password}"></property>
              <property name="maxPoolSize" value="40"></property>
              <property name="minPoolSize" value="1"></property>
              <property name="initialPoolSize" value="1"></property>
              <property name="maxIdleTime" value="20"></property>
       </bean>

       <bean id="dynamicDataSource" class="com.aa.ee.datasource.DynamicDataSource">
              <property name="targetDataSources">
                     <map key-type="java.lang.String">
                            <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>
                            <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>
                     </map>
              </property>
              <property name="defaultTargetDataSource" ref="dataSourceOne"></property>
       </bean>

     
       <bean id="sessionFactory"
             class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dynamicDataSource" />
              <property name="packagesToScan" value="com.aa.ee.entity" />
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                     </props>
              </property>
       </bean>



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



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


       <aop:config>
             <aop:pointcut id="transactionPointcut" expression="execution(* com.aa.ee.DAO..*.*(..))" />
              <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />

       </aop:config>

       <tx:advice id="txAdvice" transaction-manager="transactionManager">
              <tx:attributes>
                     <tx:method name="add*" propagation="REQUIRED"/>
                     <tx:method name="get*" propagation="REQUIRED"/>
                     <tx:method name="find*" propagation="REQUIRED"/>
                     <tx:method name="update*" propagation="REQUIRED"/>
                     <tx:method name="save*" propagation="REQUIRED"/>
                     <tx:method name="*" read-only="true" />
              </tx:attributes>
       </tx:advice>

       <aop:config>
                     <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">
                            <aop:pointcut id="daoOne" expression="execution(* com.aa.ee.DAO.impl.*.*(..))" />
                            <aop:pointcut id="daoTwo" expression="execution(* com.aa.ee.DAO.datasetImpl.*.*(..))" />

                            <aop:before pointcut-ref="daoOne" method="setdataSourceOne" />
                            <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />
                     </aop:aspect>
       </aop:config>

sessionFactory使用部分(DatasetSnapshotMonitor 是数据库Two中的表):


    @Resource
    private SessionFactory sessionFactory;

    public DatasetSnapshotMonitor getById(int id) {
        Criteria criteria=sessionFactory.getCurrentSession().createCriteria(DatasetSnapshotMonitor.class);
        criteria.add(Restrictions.eq("id",id));
        DatasetSnapshotMonitor result= (DatasetSnapshotMonitor) criteria.uniqueResult();
        return result;
    }

最后报错:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'one.dataset_snapshot_monitor' doesn't exist.
dataset_snapshot_monitor应该属于database two的,说明sessionFactory并没有动态绑定到数据库two,请问大神们大概什么原因?
补充一点:
sessionFactory.getSessionFactoryOptions().getInterceptor()
输出为org.hibernate.EmptyInterceptor@b4f7c62,这个算异常嘛?
另外附上一张异常配置图,第二个<aop:pointcut的expression显示不正常,下面的pointcut-ref不正常红字aop配置小异常

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2022-09-12 03:23:40

已解决,换了一种方式,配置两个sessionFactory

<bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <!-- MySQL5 -->
              <property name="driverClass" value="${driverClassName}"></property>
              <property name="jdbcUrl" value="${zabbix_url}"></property>
              <property name="user" value="${zabbix_username}"></property>
              <property name="password" value="${zabbix_password}"></property>
              <property name="maxPoolSize" value="40"></property>
              <property name="minPoolSize" value="1"></property>
              <property name="initialPoolSize" value="1"></property>
              <property name="maxIdleTime" value="20"></property>
       </bean>

       <!--<!– 使用C3P0数据源,dataset MySQL数据库 –>-->
       <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <!-- MySQL5 -->
              <property name="driverClass" value="${driverClassName}"></property>
              <property name="jdbcUrl" value="${dataset_url}"></property>
              <property name="user" value="${dataset_username}"></property>
              <property name="password" value="${dataset_password}"></property>
              <property name="maxPoolSize" value="40"></property>
              <property name="minPoolSize" value="1"></property>
              <property name="initialPoolSize" value="1"></property>
              <property name="maxIdleTime" value="20"></property>
       </bean>

       <!-- zabbix session工厂 -->
       <bean id="zabbixSessionFactory"
             class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSourceOne" />
              <property name="packagesToScan" value="com.pp.aa.entity" />
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                     </props>
              </property>
       </bean>

       <!-- dataset session工厂 -->
       <bean id="datasetSessionFactory"
             class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSourceTwo" />
              <property name="packagesToScan" value="com.pp.aa.entity.dataset" />
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                     </props>
              </property>
       </bean>

然后在使用的时候通过@Qualifier指明是哪一个session

    @Autowired
    @Qualifier("datasetSessionFactory")
    private SessionFactory datasetSessionFactory;

或者

    @Autowired
    @Qualifier("zabbixSessionFactory")
    private SessionFactory zabbixSessionFactory;

另外一点需要注意,会报一个错:No bean named 'sessionFactory' is defined
解决方案:
在web.xml

<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

后面添加:

        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>zabbixSessionFactory</param-value>
        </init-param>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文