"Interview" "Spring FAQ" "09"

Source: Internet
Author: User
Tags websphere application server

81, Simplejdbctemplate

The Simplejdbctemplate class is also based on the JdbcTemplate class, but takes advantage of java5+ 's variable parameter list and automatic boxing and unpacking to get more concise code.

Simplejdbctemplate mainly provides two types of methods: Query and Queryforxxx method, update and BatchUpdate method.

82. Integrated spring JDBC and best practices

In most cases, spring JDBC is used with the IOC container.   Use spring JDBC in a configuration way. And most of the time is developed using the JdbcTemplate class (or Simplejdbctemplate and namedparameterjdbctemplate), which means that the JdbcTemplate class can be used for 80% of the time, and only 20% Time is developed using other classes, conforming to the 80/20 rule.

Spring JDBC supports consistent database access by implementing Daosupport. SPRINGJDBC provides the following Daosupport implementations:

Jdbcdaosupport: Used to support consistent jdbctemplate access;

Namedparameterjdbcdaosupport: Inherit Jdbcdaosupport and provide namedparameterjdbctemplate access at the same time;

Simplejdbcdaosupport: Inherits Jdbcdaosupport while providing simplejdbctemplate access.

Because the JdbcTemplate, namedparameterjdbctemplate, simplejdbctemplate classes use Datasourceutils to get and release the connection, and the connection is bound to the thread, Therefore, these JDBC template classes are thread-safe, meaning that JdbcTemplate objects can be reused in multiple threads.

83. Spring's support for ORM

Spring's support for ORM is mainly reflected in the following areas:

A consistent exception architecture that wraps the proprietary exceptions thrown by the third-party ORM framework so that we only see the DataAccessException exception system in spring;

Consistent DAO abstraction Support: Provides DAO support class Hibernatedaosupport similar to Jdbcsupport, using Hibernatetemplate template classes to simplify common operations, Hibernatetemplate provides callback interfaces to support complex operations;

Spring Transaction Management: Spring provides consistent transaction management for all data access and simplifies transaction management through configuration.

Spring also provides support for testing, data source management, allowing easy testing and simplifying the use of data sources.

84. Four isolation levels are defined in the standard SQL specification:

Uncommitted read (readuncommitted): The lowest isolation level, a transaction can read to other transactions uncommitted update data, very insecure, may be missing updates, dirty read, non-repeatable read, Phantom Read;

Commit Read (readcommitted): A transaction can read the update data submitted by other transactions, cannot see the uncommitted update data, it is impossible to lose the update, dirty read, but may appear non-repeatable read, Phantom Read;

Repeatable Read (RepeatableRead): Ensure that multiple queries executed in the same transaction will return the same result, not affected by other transactions, may be missing updates, dirty reads, non-repeatable reads, but may appear phantom read;

Serialization (Serializable): The highest isolation level, where transactions are not allowed to execute concurrently, but must be serialized for the most secure, impossible updates, dirty reads, non-repeatable reads, and Phantom reads.

The higher the isolation level, the poorer the performance of the database transaction concurrency and the fewer operations that can be handled. Therefore, in the actual project development in order to consider concurrency performance generally use the read-committed isolation level, it can avoid loss of updates and dirty read, although non-repeatable read and Phantom read can not be avoided, but may be used in situations where the pessimistic lock or optimistic lock to solve these problems.

85. Database transaction type

Database transaction types have local and distributed transactions:

Local transaction: Is the ordinary transaction, can guarantee the operation of the single database of acid, is limited to a database;

Distributed transactions: Transactions that involve two or more database sources, that is, transactions that span multiple homogeneous or heterogeneous databases (consisting of local transactions per database), are designed to ensure that all operations of these local transactions are acid-capable, allowing transactions to span multiple databases;

86. Java Transaction type

Java transaction types have JDBC transactions and JTA transactions:

JDBC Transaction: A local transaction in the database transaction type, which manages the transaction through the control of the connection object;

JTA transaction: JTA refers to the Java Transaction API (Java TRANSACTIONAPI), is the Java EE Database transaction specification, JTA only provides the transaction management interface, by the application server vendor (such as WebSphere application Server) provides implementations where JTA transactions are more powerful than JDBC and support distributed transactions.

87. Java EE transaction type

Java EE transaction types have local and global transactions:

Local transaction: Implements the transaction using JDBC programming;

Global transactions: Provided by the application server, using JTA transactions;

88, according to whether to implement the transaction by programming

According to whether the transaction is programmatically implemented with declarative transactions and programmatic transactions;

Declarative transactions: Specify transaction information through annotations or XML configuration files;

Programmatic transactions: Implement transactions by writing code.

89. Spring provides transaction management

One of the core features of the spring framework is transaction management and provides a consistent abstraction of transaction management, which can help us:

Provides a consistent programmatic transaction management API that uses the API for transactional programming, whether using the Spring JDBC Framework or an integrated third-party framework;

Non-intrusive declarative transaction support.

Spring supports declarative transactional and programmatic transaction transaction types.

90. Spring support for programmatic transactions

The transactions in spring are divided into physical and logical transactions;

Physical transaction: The transaction support provided by the underlying database, such as the transaction provided by JDBC or JTA;

Logical transactions: Are spring-managed transactions, different from physical transactions, logical transactions provide richer control, and logical transactions must be used if you want the benefits of spring transaction management, so in spring if you do not specifically emphasize the general logic transaction;

Logical transactions support very low levels of control, as well as high-level solutions:

Low-level Solutions

Tool Classes: Use the tool class to get connections (sessions) and release connections (sessions), such as using the Connectionutils class in the Org.springframework.jdbc.datasource package to get and release connections with logical transaction functionality. Of course, the integrated third-party ORM Framework also provides similar tool classes, such as the Sessionfactoryutils tool class for Hibernate, the JPA entitymanagerfactoryutils, etc., and other tool classes that use similar * * * Utils name;

Gets the connection datasourceutils.getconnection (DataSource DataSource) that has the spring transaction (logical transaction) management feature// Release the connection datasourceutils.releaseconnection (Connection con, DataSource) with spring transaction (logical transaction) management capabilities
Transactionawaredatasourceproxy: Using this data source proxy class wrapper requires a data source supported by spring transaction management, which must be at the outermost layer, Used primarily for legacy projects where it is possible to use data sources directly to get connections and release connection support, or to use a variety of persistence frameworks in spring, which actually uses the Connectionutils tool class to acquire and release a true connection;

<!--Use this method to wrap the data source, must be at the outermost, Targetdatasource know the target data source--><bean id= "Datasourceproxy" class= " Org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy ">    <property name=" Targetdatasource "ref=" DataSource "/></bean>
By wrapping a data source as above, you can use physical transaction encoding in your project to obtain support for logical transactions, that is, support for getting connections and freeing connections directly from DataSource, and those connections automatically support spring logical transactions;

High-level Solutions

Template classes: Use the template classes provided by spring, such as the JdbcTemplate, hibernatetemplate, and jpatemplate template classes, which actually use the tool classes in a low-level solution to manage connections or sessions ;

Spring provides two programmatic transaction support: Implement and use the Transactiontemplate template class directly using Platformtransactionmanager to support logical transaction management. It is recommended to use Transactiontemplate template classes and high-level solutions when using programmatic transactions.


"Interview" "Spring FAQ" "09"

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.