为什么在 @Transactional 块中没有调用 AbstractRoutingDataSource.determineCurrentLookupKey()?

Art*_*Art 2 spring hibernate transactions

我在 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 />
Run Code Online (Sandbox Code Playgroud)

Tom*_*icz 5

想一想...

  1. 一些代码想要ConnectionDataSource. 可能是为了启动一个事务并运行一些 SQL 查询

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

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

  4. 连接被返回,AbstractRoutingDataSource就好像它是一个正常的来源。

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

简单地说 -determineCurrentLookupKey()不能在事务中运行,因为事务需要一个连接,而该方法的目的是确定DataSource使用哪个来获取连接。另见:鸡或蛋

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