How can Hibernate and Mybatis use the same transaction?

original
2020/11/12 09:27
Reading number 3.2K

First, take a look at the source code:

Hibernate transaction:
org.springframework.orm.hibernate5.HibernateTransactionManager
DoBegin() processing code: process hibernate's sessionholder:

The holder source code of the datasource:
Determine whether there is a dataSource. If there is a dataSource, bind it to the connectionholder

Transaction processing in mybatis:
Data sourceutils is used to obtain the connection, while data sourceutils is read from the connectionholder

Therefore, in order to support both hibernate and mybatis transactions, the following configurations need to be made:
  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory"> 
      <ref bean="sessionFactory"/>
    </property>
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
  </bean>

It means that Hibernate transaction manager is used to manage all transactions, and the default datasource is hosted in the data source of Hibernate.
 

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top