How to use transactionattributes in spring configuration

Recently, I encountered this problem. When using the jpatemplate provided by spring for query, if the amount of data exceeds 100, the query efficiency will be significantly reduced. Since the two-way association within JPA is used at the beginning, there are too many internal associations among entities, affecting all operations. Therefore, it is suspected that it is due to the association relationship of JPA. However, the effect after removing the correlation is not significant.

Find the related configuration of spring and find the original problem about "transactionattributes". The original configuration is as follows:

Using the above configuration, the log printed by JPA shows that each query needs to be updated. After consulting the data of relevant spring, we find the significance of various attributes of transactionattributes. Now we share the data as follows:

PROPAGATION_ Required -- supports the current transaction. If there is no transaction, a new transaction will be created. This is the most common choice.

PROPAGATION_ Supports -- supports the current transaction. If there is no transaction, it will be executed in a non transactional manner.

PROPAGATION_ Mandatory -- supports the current transaction. If there is no transaction, an exception will be thrown.

PROPAGATION_ REQUIRES_ New -- create a new transaction. If there is a current transaction, suspend the current transaction.

PROPAGATION_ NOT_ Supported -- perform the operation in a non transactional manner. If there is a current transaction, suspend the current transaction.

PROPAGATION_ Never -- execute in a non transactional manner. If there is a transaction currently, an exception will be thrown.

PROPAGATION_ Nested -- if a transaction currently exists, it is executed within a nested transaction. If there is no current transaction, the and

PROPAGATION_ Required similar operations.

At present, all transactions use the value of "protection_required" attribute, and the operation permission of the control transaction is read-only, so as to ensure that the data will not be updated during query. According to the above definition, the "deployment_required" attribute will create transactions for all operations, resulting in the phenomenon that update operations will also be performed during query in JPA logs, resulting in low efficiency. If the transaction type of all query operations is changed to "deployment_never", the query efficiency will be improved immediately. However, at this time, there is a problem to worry about: for example, in a savexxx() method, if the operation process of update, query and re update is used inside the method, will exceptions be thrown due to the above configuration when calling the query. In addition, if

A configuration in which:

-Exception means that when an exception is thrown, the transaction is rolled back- For rollback + for commit

Readonly is read only. Set the operation permission to read only. It is generally used for query and optimization

summary

The above is all about how to use transactionattributes in spring configuration. I hope it will be helpful to you. Interested friends can refer to: talking about the advantages of spring boot to spring, detailed introduction to spring transaction propagation properties and isolation level, and talking about the recommended deployment method of spring boot microservice project. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>