gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:12:41 25 4
gpt4 key购买 nike

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

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

像其他 spring-mvc 应用程序一样,我们有 3 层: Controller 、服务、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(){}
}

第二个数据源的方法也将位于此类内。我计划用 @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

根上下文.xml:

第一个数据源(主要):

<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 中,他们正在 Autowiring 两个 session 工厂。作者还使用了Qualifier注解来保持分隔。在您的 DAO 中,您可能想要执行以下操作:

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

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

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

关于Java, Spring : Create a separate datasource bean throws NonUnique error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com