Java Interview--spring Technical Essentials--spring data Access

Source: Internet
Author: User

support for DAO in spring

Spring's support for data Access Objects (DAO) is designed to simplify its use with data access technologies such as Jdbc,hibernateor JDO. This allows us to easily switch the persistence layer. Don't worry about capturing each technology-specific exception when coding.

    • optimized Exception type system : The data access exception is refined, and the exception type is enriched. (All unchecked Exception, this anomaly does not change, the same exception is used to denote the same phenomenon, regardless of the persistence technology used)
    • using the template callback mode, developers no longer write pattern code, simplifying programming :

Invariant: Resource acquisition, release of resources, exception conversions (Spring provides template classes for this).

Change: SQL, variable, result set extraction.

25 JDBC-based DAO (Hibernate-like)

Simplifying the operation of JDBC

    • The template is responsible for : Get release of JDBC object, conversion of exception type.
    • Development is responsible for : Providing SQL, setting variables in SQL, extracting resultset.

Application

    • Core Method : Query () update ()
    • Callback Interfaces : PreparedStatementCreator, PreparedStatementSetter, ResultSetExtractor, RowMapper

There are two ways to get JdbcTemplate in DAO:

    1. Give DAO injected jdbctempate: Bean configuration: datasource->jdbctemplate (requires bean factory control)->dao
    2. make DAO class Inheritance Jdbcdaosupport: Inherited Method Getjdbctemplate (), bean configuration: Datasource->dao.

spring+hibernate How to access the database

There are two ways to access hibernate in spring:

    • Use Hibernatetemplate and callback. ( recommended )
    • Inherit Hibernatedaosupport. ( not recommended )
    • Direct use of sessionfactory ( not recommended )

27 integrating Spring and hibernate with Hibernatedaosupport

Call Localsessionfactory with spring's sessionfactory. The integration process is divided into three steps:

    • Configure Hibernatesessionfactory.
    • Inheriting hibernatedaosupport implements a DAO.
    • Assemble in an AOP-supported transaction.

types of transaction management supported by spring

Spring supports two types of transaction management:

    • Programmatic Transaction Management : This means that you manage transactions programmatically, giving you great flexibility, but difficult to maintain.
    • Declarative Transaction Management : This means that you can separate business code from transaction management, and you only need to manage transactions with annotations and XML configurations.

Benefits of Spring Transaction management

    • It provides a constant programming pattern for different transactional APIs such as JTA,JDBC,HIBERNATE,JPA and JDO.
    • It provides a simple set of APIs for programmatic transaction management rather than some complex transactional APIs.
    • It supports declarative transaction management.
    • It is well integrated with spring's various data access abstraction layers.

30 common built-in transaction manager implementations

    • Datasourcetransactionmanager : In the Org.springframework.jdbc.datasource package, the data source transaction manager, which provides transaction management for a single javax.sql.DataSource transaction management, SPRINGJDBC abstract framework, Ibatis framework ;
    • Hibernatetransactionmanager : In the Org.springframework.orm.hibernate3 or HIBERNATE4 package, provides transaction management support for a single org.hibernate.SessionFactory transaction when integrating the Hibernate framework, which only supports hibernate3+ version, and the spring3.0+ version only supports hibernate3.2+ version;
    • Jtatransactionmanager : In the ORG.SPRINGFRAMEWORK.TRANSACTION.JTA package, provides support for distributed transaction management and delegates transaction management to the Java EE Application Server transaction manager;

7 of the propagation behavior of spring transactions (propagation)

    1. required : Specifies that the current method must run in a transactional environment, and if the current transaction environment joins the currently executing transactional environment, If there is no current transaction, create a new transaction. This is the default value.
    2. supports : Specifies that the current method joins the current transaction environment and is executed in a non-transactional manner if there is no current transaction.
    3. mandatory : Specifies that the current method must be joined to the current transaction environment and throws an exception if there is no current transaction.
    4. requires_new : The current method always initiates a new transaction for itself, if the current method is found to be running in a transaction , the original transaction is suspended and a transaction of its own is created, and the original transaction resumes execution until the end of the method commit.
    5. not_supported : The current method performs the operation in a non-transactional manner, suspending the current transaction if the current transaction exists. Wait for a non-transactional state to run, and then continue with the original transaction.
    6. never : The current method must never be executed within the scope of a transaction, and if the method executes within a transaction scope, the container throws an exception. Only if it is not connected to a transaction will it execute normally.
    7. nested : Specifies that when the current method executes, it is run in this nested transaction if a transaction already exists. If the current environment does not have a transaction running, create a new transaction and be independent of the parent transaction, which has multiple assurance points that can be rolled back. That means my own internal transaction rollback does not affect external transactions, only the Datasourcetransactionmanager transaction manager.

detailed description of Spring transaction configuration properties (<tx:method > or @transactional)

Table 3 Spring Transaction configuration Options

Property

Type

Default value

Description

Propagation

Propagation Enumeration

REQUIRED

Transaction propagation Properties

Isolation

Isolation Enumeration

Default ( The database that you use defaults to )

Transaction ISOLATION LEVEL

ReadOnly

Boolean

False

Whether to use optimized read-only transactions

Timeout

Int

-1

timed out ( seconds )

Rollbackfor

Class[]

{}

Exception classes that need to be rolled back

Rollbackforclassname

String[]

{}

Exception class name that needs to be rolled back

Norollbackfor

Class[]

{}

Exception classes that do not need to be rolled back

Norollbackforclassname

String[]

{}

Exception class name that does not need to be rolled back

http://zhou137520.iteye.com/blog/1675199


Java Interview--spring Technical Essentials--spring data Access

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.