The final transaction commits

There are two main implementations

1.hibernate

2. The data source

That is, the transaction of ORM framework is called, and the transaction of ORM framework is called by JDBC client, that is, the submission method of JDBC API connection.


hibernate

public class HibernateTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, BeanFactoryAware, InitializingBean {protected void doCommit(DefaultTransactionStatus status) {// Get the Hibernate transaction manager HibernateTransactionManager.HibernateTransactionObject txObject = (HibernateTransactionManager.HibernateTransactionObject)status.getTransaction(); if (status.isDebug()) { this.logger.debug("Committing Hibernate transaction on Session [" + SessionFactoryUtils.toString(txObject.getSessionHolder().getSession()) + "]"); } try {/ / to commit the transaction txObject. GetSessionHolder (). The getTransaction (). The commit (); / / to commit the transaction} the catch (TransactionException var4) {throw new TransactionSystemException (" Could not commit to Hibernate transaction", var4); } catch (HibernateException var5) { throw this.convertHibernateAccessException(var5); }}Copy the code

The data source

public class DataSourceTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean {protected void doCommit(DefaultTransactionStatus status) {// Get the data source transaction manager DataSourceTransactionManager.DataSourceTransactionObject txObject = (DataSourceTransactionManager.DataSourceTransactionObject)status.getTransaction(); Connection con = txObject.getConnectionHolder().getConnection(); if (status.isDebug()) { this.logger.debug("Committing JDBC transaction on Connection [" + con + "]"); } try {// commit transaction con.mit (); / / to commit the transaction} the catch (SQLException var5) {throw new TransactionSystemException (" Could not commit JDBC transaction, "var5); }}Copy the code

thread

If a request comes down from a thread that may contain many transactions, how can transactions be propagated? 1. Create a new transaction for each transaction. 2. Create only the first transaction


What are their strengths and weaknesses? Application scenarios? A little.


What is the current state of the company’s system? What are the communication rules?

configuration

<! -- transaction --> <bean id="transactionManager_trade" Class = "org. Springframework. Orm. Hibernate3. HibernateTransactionManager" > / / hibernate transaction manager < property name="sessionFactory"> <ref bean="sessionFactory_trade" /> </property> </bean> <! --> <tx:advice ID ="txAdvice_trade" transaction-manager="transactionManager_trade"> <tx:attributes> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="insert*" rollback-for="hikefa.core.exception.BizException" /> <tx:method name="update*" rollback-for="hikefa.core.exception.BizException" /> <tx:method name="delete*" rollback-for="hikefa.core.exception.BizException" /> <tx:method name="*" Rollback - for = "hikefa. Core. Exception. BizException" / > / / all methods add transaction < / tx: attributes > < / tx: advice > < aop: config proxy-target-class="true"> <aop:advisor advice-ref="txAdvice_trade" pointcut="execution(* xxx.trade.dbservice.impl.. *Service*.*(..) </aop:config> </aop:config>Copy the code

What is the default propagation rule? If the current thread already has a transaction, no new transaction is created. If not, create a new transaction.

1. Code

Non-service class methods (){1. Call service1. method; // For example, create a payment order 2. Call the service2. // For example, create payment order details}Copy the code

2. What are the transaction propagation rules now? 1) Call service1. // After the method ends, the transaction has committed. Method // Since the above transaction has been committed and completed, a new transaction object will be created, and when the method ends, the new transaction object will commit the transaction

3. What is the default transaction propagation rule of Spring? code

Business method (){// Create a new transaction. 1. Call the Service1 class. Business method; // The current thread already has a transaction, so it will not create a new transaction object. 2. Invoke the Service2 class. Business methods. // The current thread already has a transaction, so it will not create a new transaction object. }Copy the code

At this point, there are three transactions, but they share one (that is, the first transaction object), but should the commit transaction be committed each time at the end of each method, or at the end of each transaction (that is, at the end of a business method of the Service0 class) and commit only once? Commit only once, because a transaction is an atomic operation, regardless of how many subtransactions it contains (i.e., nested transactions), so if there is a nested transaction, the outermost transaction commits only once, when the outermost business method ends.


Represents the relationship between the core class of the transaction object and the thread

How is the transaction of each requesting thread bound to the current thread? Based on the ThreadLocal.

reference

In-depth analysis of spring architecture and implementation principles