


Detailed explanation of examples of Spring's use of annotations for transaction management configuration
This article mainly introduces Spring's use of annotations for transaction management configuration. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look
Usage steps:
Step 1. Introduce the
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
Step 2. Beans with @Transactional annotations are automatically configured to support declarative transactions
<!-- 事务管理器配置, Hibernate单数据源事务 --> <bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />
Step 3.At the declaration of the interface or class, write A @Transactional.
If you only write on the interface, the implementation class of the interface will be inherited. The specific methods of the implementation class of the interface can override the settings at the class declaration
@Transactional //Class-level annotations, applicable to all public methods in the class
Transaction propagation behavior and isolation level
Everyone is using spring When using annotation-based transaction management, you may be a little overwhelmed by the transaction propagation behavior and isolation level. The following is a detailed introduction for easy reference.
Thing annotation method: @Transactional
When marked in front of a class, it indicates that all methods in the marked class perform transaction processing, example:
@Transactional public class TestServiceBean implements TestService {}
When some methods in the class do not require things:
@Transactional public class TestServiceBean implements TestService { private TestDao dao; public void setDao(TestDao dao) { this.dao = dao; } @Transactional(propagation = Propagation.NOT_SUPPORTED) public List<Object> getAll() { return null; } }
Thing propagation behavior introduction:
Thing timeout setting:
Parameter name | Function description |
This attribute is used to set whether the current transaction is a read-only transaction. Set to true to indicate read-only, and false to indicate read-write. The default value is false. For example: @Transactional(readOnly=true) | |
This attribute is used to set the required rollback Exception class array, when the exception in the specified exception array is thrown in the method, the transaction will be rolled back. For example: |
Specify a single exception class: @Transactional(rollbackFor=RuntimeException.class) Specify multiple exception classes: @Transactional(rollbackFor={RuntimeException.class, Exception.class}) |
Parameter name |
Function description |
rollbackForClassName |
This property is used to set the need for rollback An array of rollover exception class names. When an exception in the specified exception name array is thrown in the method, the transaction will be rolled back. For example: Specify a single exception class name: @Transactional(rollbackForClassName="RuntimeException") Specify multiple exception class names: @Transactional (rollbackForClassName={"RuntimeException","Exception"}) |
#noRollbackFor |
This attribute is used to set an array of exception classes that do not require rollback. When an exception in the specified exception array is thrown in the method, the transaction will not be rolled back. For example: Specify a single exception class: @Transactional(noRollbackFor=RuntimeException.class) Specify multiple exception classes: @Transactional(noRollbackFor ={RuntimeException.class, Exception.class}) |
##noRollbackForClassName
|
This attribute is used to set an array of exception class names that do not require rollback. When an exception in the specified exception name array is thrown in the method, the transaction will not be rolled back. For example: Specify a single exception class name: @Transactional(noRollbackForClassName="RuntimeException") Specify multiple exception class names: @Transactional(noRollbackForClassName={"RuntimeException","Exception"})
|
For example: @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
|
|
The above is the detailed content of Detailed explanation of examples of Spring's use of annotations for transaction management configuration. For more information, please follow other related articles on the PHP Chinese website!

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
