Java, Spring : 创建一个单独的数据源 bean 抛出 NonUnique 错误

Java, Spring : Create a separate datasource bean throws NonUnique error

我正在开发一个 Spring-MVC 应用程序,到目前为止我们只有一个数据源。对于我们的要求,大多数交易都是短期的,我们确实有很少需要很长时间来处理的应用程序。为避免系统因这些请求而阻塞,我们计划创建一个单独的数据源。

除此之外,这些方法的优先级较低,因为用户知道这些方法需要更长的时间。为此,我只是调用 Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 。这够了吗?重点是,这些方法不应该在 CPU、磁盘、I/O 等方面消耗更多时间

与其他 spring-mvc 应用程序一样,我们有 3 层,控制器、服务和 DAO。一些服务层方法将在这些数据源之间共享。示例:long_method_service_layer()-->dependent_method_service_layer()。还有short_method_service_layer()-->dependent_method_service_layer();

那么现在当第二个数据源调用dependent_method()时,它是否也具有低优先级?

目前,我们的服务层是这样配置的:

@Service
@Transactional
public class DownloadTokenServiceImpl implements DownloadTokenService{

    private final DownloadTokenDAO downloadTokenDAO;

    @Autowired
    public DownloadTokenServiceImpl(DownloadTokenDAO downloadTokenDAO){
        this.downloadTokenDAO = downloadTokenDAO;
    }

@Override
public void method_name(){}
}

来自第二个数据源的方法也将在此 class 中。我打算用 @Transactional("2nd_source") 单独注释这些方法。这个对吗?

最后,我现在尝试的错误日志:

Caused by: 
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: hibernate4AnnotatedSessionFactory_extended,hibernate4AnnotatedSessionFactory
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:814)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    at org.s

root-context.xml :

第 1 data_source(小学):

<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
        <beans:property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
       <beans:property name="maximumPoolSize" value="50" />
        <beans:property name="maxLifetime" value="200000" />
        <beans:property name="idleTimeout" value="25000" />
        <beans:property name="leakDetectionThreshold" value="200000"/>
        <beans:property name="connectionTimeout" value="200000"/>
        <beans:property name="dataSourceProperties">
            <beans:props>
                <beans:prop key="url">jdbc:postgresql://localhost:5432/DB_NAME</beans:prop>
                <beans:prop key="user">username</beans:prop>
                <beans:prop key="password">password</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="packagesToScan" value="com.our_app.spring.model"/>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
                <beans:prop key="cache.use_second_level_cache">true</beans:prop>
                <beans:prop key="cache.use_query_cache">true</beans:prop>
                <beans:prop key="hibernate.order_updates">true</beans:prop>
                <beans:prop key="show_sql">false</beans:prop>
                <beans:prop key="connection.release_mode">after_statement</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>


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

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

第二个数据源(长 运行 交易需要,hikari 值在其工作后进行调整):

 <beans:bean id="extended_transactions_data_source" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
        <beans:property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
        <beans:property name="maximumPoolSize" value="50" />
        <beans:property name="maxLifetime" value="200000" />
        <beans:property name="idleTimeout" value="25000" />
        <beans:property name="leakDetectionThreshold" value="200000"/>
        <beans:property name="connectionTimeout" value="200000"/>
        <beans:property name="dataSourceProperties">
            <beans:props>
                <beans:prop key="url">jdbc:postgresql://localhost:5432/Db_NAME</beans:prop>
                <beans:prop key="user">USERNAME</beans:prop>
                <beans:prop key="password">passwoRD</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>


    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory_extended"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="extended_transactions_data_source"/>
        <beans:property name="packagesToScan" value="com.our_app.spring.model"/>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
                <beans:prop key="cache.use_second_level_cache">true</beans:prop>
                <beans:prop key="cache.use_query_cache">true</beans:prop>
                <beans:prop key="hibernate.order_updates">true</beans:prop>
                <beans:prop key="show_sql">false</beans:prop>
                <beans:prop key="connection.release_mode">after_statement</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

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

    <beans:bean id="transactionManager_extended" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory_extended"/>
    </beans:bean>

我不认为这与 this post 完全相同,但有一些强烈的相似之处。看看那里接受的答案并注意,在 DAO 中,他们正在自动装配两个会话工厂。作者也在使用 Qualifier 注解来保持分离。在你的 DAO 中,你可能想做这样的事情:

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

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

认为在这种情况下,您不必限定事务注释,因为您的 DAO 实现必须显式使用一个会话工厂或另一个。