Using spring MVC has been around for 2 years, but still stuck in the use stage, feeling like this is not the way to go, so still want to go deep to explore.
Spring Transaction Management
transactions, simply put, is that for an operation, either all succeed, or both fail, so that the consistency of the data, in spring MVC can also configure the corresponding transaction manager, there are two ways to implement: XML and annotations.
XML Configuration method implements transactions
In the spring configuration file, initialize a transaction manager and inject the data source:
<span style= "FONT-SIZE:18PX;" ><bean id= "TransactionManager"
class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">
<property name=" DataSource "ref=" Druiddatasource "/>
</bean></span>
To define the basic properties of a transaction:
<span style= "FONT-SIZE:18PX;" ><tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
< Tx:method name= "find*" read-only= "true"/>
<tx:method name= "update*" propagation= "REQUIRED"/>
< Tx:method name= "delete*" propagation= "REQUIRED"/>
<tx:method name= "save*" propagation= "REQUIRED"/>
</tx:attributes>
</tx:advice></span>
Transaction-manager for the transaction manager defined above
The Name property defines which methods are called;
Propagation defines the propagation property of the transaction, the default value is required, the function is "support the current transaction, if there is no transaction currently, create a new transaction, that is, there is only one transaction, when an exception occurs, all operations within the transaction are rolled back", the other 6 values can be found on the API, General selection of default values;
read-only function is "only accept read-only operation", the default value is False, if configured to True, when the configuration of the method in the database additions and deletions, the exception is reported as: caused By:java.sql.SQLException:Connection is Read-only. Queries leading to data modification is not allowed, the proper configuration of this property can improve performance.
Finally, the slice of the transaction is configured, and the transaction configuration is complete:
<span style= "FONT-SIZE:18PX;" ><aop:config>
<!--define a pointcut--
<aop:pointcut id= "interceptorpointcuts"
expression= " Execution (* com.jk.service.impl.*impl.* (..)) or execution (* com.jk.dao.*.* (..))/>
<!--notification of pointcuts and transactions to be adapted-- >
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "interceptorpointcuts"/>
</aop:config> </span>
annotation Way to implement a transaction
Similarly, in the spring configuration file, initialize a transaction manager and inject the data source:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><bean id= "TransactionManager"
class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">
<property name=" DataSource "ref=" Druiddatasource "/>
</bean></span></span>
Then turn on the annotations:
<tx:annotation-driven transaction-manager= "TransactionManager"/>
This allows you to use the annotations of the transaction in your code, plus the method that requires the transaction, where the attributes are the same as in the XML configuration:
@Transactional (propagation=propagation.required,rollbackforclassname= "Exception")