"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 the variable list of java5+ and self-actively 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). You can use the JdbcTemplate class for 80% of the time, and only 20% of the time to develop with other classes, conforming to the 80/20 rule.

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

Jdbcdaosupport: Used to support consistent jdbctemplate access.

Namedparameterjdbcdaosupport: Inherit Jdbcdaosupport, provide namedparameterjdbctemplate interview at the same time;

Simplejdbcdaosupport: Inherit Jdbcdaosupport, provide simplejdbctemplate interview at the same time.

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. That is, the JdbcTemplate object can be reused in multiple threads.

83. Spring's support for ORM

Spring's support for ORM Main table is now in the following areas:

A consistent exception architecture that wraps the proprietary exceptions thrown by the third-party ORM framework. So that we can only see the DataAccessException anomaly system in spring.

Consistent DAO abstraction Support: Provides DAO support class Hibernatedaosupport similar to Jdbcsupport. Use the Hibernatetemplate template class to simplify frequently used 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 supports testing and data source management, agreeing to a convenient test. Simplifies the use of data sources.

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

Uncommitted read (readuncommitted): lowest isolation level. It is very insecure for a transaction to be able to read uncommitted update data from another transaction. There may be missing updates, dirty reads, non-repeatable reads, and Phantom reads.

Commit Read (readcommitted): One transaction can read the updated data submitted by another transaction, cannot see the uncommitted update data, it is impossible to lose the update, dirty read. But may appear cannot read repeatedly, the Phantom reads;

Repeatable Read (RepeatableRead): Multiple queries running in the same transaction will return the same result, unaffected by other transactions. There may be missing updates, dirty reads, non-repeatable reads, but phantom reads may occur.

Serialization (Serializable): The highest level of isolation. The transaction is not agreed to run concurrently, but must be serialized to run, the most secure. Updates, dirty reads, non-repeatable reads, and Phantom reads are unlikely to occur.

The higher the isolation level. The poorer the performance of database transactions concurrently runs. The fewer operations you can handle.

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-repetition and phantom reading can not be avoided, but can be used in the event of a pessimistic or optimistic lock to solve these problems.

85. Database transaction type

Database transaction types have local and distributed transactions:

Local transaction: It is ordinary transaction, can guarantee the acid of operation on single database, be limited to a database.

Distributed transactions: Transactions that involve two or more database sources. A transaction that spans multiple homogeneous or heterogeneous databases (consisting of local transactions for each database). Distributed transactions are designed to ensure that the entire operation of these local transactions is acid, 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), which is the Java EE Database transaction specification. JTA only provides a transaction management interface. Implemented by application Server vendors (such as WebSphere application Server), 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 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 this 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

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. Unlike physical transactions, logical transactions provide richer control and, assuming that the benefits of spring transaction management are desired, logical transactions must be used, so it is assumed in spring that there is no particular emphasis on logical transactions;

Logical transactions support very low levels of control. There are also high-level solutions:

Low-level solution

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 integration of the third-party ORM Framework also provides similar tool classes, such as the Sessionfactoryutils tool class for Hibernate, JPA entitymanagerfactoryutils, etc., other tool classes are used like * * * 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. Its internal actually uses the Connectionutils tool class to get and release the 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 directly from the DataSource to get the connection and release the connection, and these connections themselves actively support spring logical transactions;

High-level Solutions

template class: Use the template class provided by spring. such as JdbcTemplate, Hibernatetemplate, and jpatemplate template classes, and these template classes are in fact using a tool class 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 assumed that the Transactiontemplate template class and high-level resolution are recommended for 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.