"Interview" "Spring FAQ" "05"

Source: Internet
Author: User

The 5 types oftransaction properties are declarative transaction management :

Propagation behavior: The propagation behavior defines the transaction boundary between the client and the calling method.

Isolation level: The isolation level defines the degree to which a transaction may be affected by other concurrent transactions.

Read Only: Indicates whether the transaction is read-only.

Transaction timeout: Specifies the maximum time that the transaction executes, and the timeout is rolled back.

Rollback rule: Defines which exceptions cause transactions to be rolled back and which do not.

Read, Dirty, non-repeatable, and phantom reading

Dirty reads: When a transaction reads a transaction that also overwrites uncommitted data. Assuming that the rewrite is rolled back later, the data obtained by the first transaction is invalid.

Do not reread: a thing runs the same query two or more times or more than two times, but gets different data each time.

This is typically due to the fact that a concurrent transaction has updated data during two queries.

Phantom reads: A transaction reads a few rows of data, and then a concurrent transaction inserts some data.

In the subsequent query, the first transaction found a number of transactions that did not already exist.

43.7 Kinds of propagation behavior of spring Affairs

The difference between propagation_nested and propagation_requires_new

They are similar, all like a nested transaction. Assume that there is no active transaction. will open a new transaction. With Propagation_requires_new, the inner transaction and the outer transaction are like two separate transactions. Once an inner transaction commits, the outer transaction cannot roll it back. Two transactions do not affect each other. Two transactions are not a true nested transaction. At the same time it needs to be supported by the JTA transaction manager.

When using propagation_nested, the rollback of the outer transaction can cause a rollback of the inner transaction. The exception of the inner transaction does not cause the rollback of the outer transaction. It is a real nested transaction.

44.5 Isolation levels for spring transactions

45. Spring Note explains:

Configure Spring XML Writing in a scanned fashion

<context:component-scanbase-package= "Here is the package name"/>

@Service for labeling business layer builds

@Controller for labeling control-level components

@Component a generic component for a class that does not understand

@Repository User Labeling Database Access Components

@PostConstruct methods to run when an object is created

Methods to run @PreDestroy objects when they are destroyed

@Autowired direct access to private properties, enter values directly into the private property.

@Resource injection based on type

46. How Spring MVC works:

1.spring MVC Please submit all requests to dispatcherservlet, which will entrust the other modules of the application to be responsible for the actual processing of the request.

2.DispatcherServlet queries one or more handlermapping to find the controller that handles the request.

3.DispatcherServlet request submitted to target Controller

4.Controller after the business logic is processed, it returns a Modelandview

5.Dispathcher querying one or more viewresolver view resolvers, locating the View object specified by the Modelandview object

6. The View object is responsible for rendering back to the client.

47. What are the strengths of spring?

1. Spring is a layered architecture, and you can choose to use the layers you need without having to worry about the parts you don't need.

2. Spring is Pojo (Plain ordinary Java Object), Pojo programming enables sustainable construction and improved testing capabilities

3. Dependency injection and IOC make JDBC operations simple

4. Spring is open source free of charge

5. Spring makes the management of objects centralized and simple

48, Spring's transaction management

Spring provides several classes for transaction processing:

Transactiondefinition//Transaction Property Definition

Transcationstatus//Represents the current transaction. Able to commit, rollback.

Platformtransactionmanager This is the underlying interface that spring provides for managing transactions, with an implementation of an abstract class Abstractplatformtransactionmanager, The transaction management classes we use, such as Datasourcetransactionmanager, are subclasses of this class.

Transaction Management provided by Spring

The transaction management provided by spring can be divided into two categories: programmatic implementation of transactions, and AOP configuration declarative resolution. Programming, more flexible, but the code is large, there is a lot of repeated code, declarative more flexible than the programming type.

To implement a transaction programmatically, here is one of the implementations:

The Execute () method of the Transactiontemplate has a transactioncallback-type parameter, which defines a dointransaction () method, which is usually implemented in the form of an anonymous inner class The Transactioncallback interface and writes the business logic code in its dointransaction () method. You can use the default transaction commit and rollback rules here, so that you do not have to explicitly invoke any transaction-managed APIs in your business code.

The Dointransaction () method has a parameter of type Transactionstatus, and we are able to call the Setrollbackonly () method of the parameter at any point in the method to identify the transaction as a rollback. To run the transaction rollback.

Based on the default rule, assume that an unchecked exception is thrown during the execution of the callback method, or the Transacationstatus.setrollbackonly () method is explicitly called, the transaction is rolled back. The transaction is committed if the transaction has finished running or if an exception of type checked has been thrown.

The Transactioncallback interface has a sub-interface transactioncallbackwithoutresult that defines a Dointransactionwithoutresult () method in the interface. The Transactioncallbackwithoutresult interface is primarily used in cases where the return value is not required for a transaction.

Of course. For cases where the return value is not required. We are still able to use the Transactioncallback interface and return arbitrary values in the method.

Declarative things management. Examples.

<context:component-scanbase-package= "COM.CHOU.SPRING.JDBC"/><!--configuration Data Source-<beanid= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" > <!--Connection Info--<prop Erty name= "Driverclass" value= "${db.driverclass}"/> <property name= "Jdbcurl" value= "${db.url}"/> <proper Ty name= "user" value= "${db.username}"/> <property name= "password" value= "${db.password}"/> <!--C Onnection Pooling Info--<propertyname= "Initialpoolsize" value= "1"/> <property name= "Minpoolsize" Valu e= "1"/> <property name= "maxpoolsize" value= "all/> <property name=" maxidletime "value=" 1800 "/> &lt ;p roperty name= "maxstatements" value= "0"/></bean><beanid= "Jdbctemplatedao" class= " Com.chou.spring.jdbc.dao.JdbcTemplateDao "> <property name=" dataSource "ref=" DataSource "/></bean> & lt;! --JDBC transaction manager--><beanid= "Jdbcttxmanager" class= "Org.sprinGframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref=" DataSource "/> </bean><tx:adviceid= "Txadvice" transaction-manager= "Jdbcttxmanager" > <tx:attributes> <tx:me Thodname= "domain*"/> </tx:attributes></tx:advice> <aop:configproxy-target-class= "true" > < aop:advisoradvice-ref= "Txadvice" pointcut= "Execution (*com.chou.spring.jdbc.service.jdbctemplateservice.* (..))" /></aop:config>


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"Interview" "Spring FAQ" "05"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.