gpt4 book ai didi

spring - 为什么不在 @Transactional block 内调用 AbstractRoutingDataSource.determineCurrentLookupKey()?

转载 作者:行者123 更新时间:2023-12-02 22:44:03 29 4
gpt4 key购买 nike

我在 Spring 中使用 Hibernate,相关配置:

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

<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>

<tx:annotation-driven />
<aop:aspectj-autoproxy />

最佳答案

想一想...

  1. 一些代码想要从DataSource 获取Connection。可能是为了启动事务并运行一些 SQL 查询

  2. AbstractRoutingDataSource 执行 determineCurrentLookupKey() 以便从一组可用的 DataSource 中找到合适的 DataSource

    <
  3. 查找键用于获取当前 DataSourceAbstractRoutingDataSource 从该数据源返回 JDBC 连接。

  4. 连接从 AbstractRoutingDataSource 返回,就好像它是一个普通源一样。

现在您要问为什么 determineCurrentLookupKey() 不在事务中运行?第一个 Spring 必须转到第 1 点。以获取启动事务所需的一些数据库连接。看下一点。看到问题了吗?对我来说就像是无限递归。

简单地说 - determineCurrentLookupKey() 不能在事务中运行,因为事务需要连接,该方法的目的是确定使用哪个 DataSource 来获取一个连接。另请参阅:Chicken or the egg .

同样,工程师们无法使用计算机来设计第一台计算机。

关于spring - 为什么不在 @Transactional block 内调用 AbstractRoutingDataSource.determineCurrentLookupKey()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10324644/

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