Some concepts of read-only transactions (@Transactional (readOnly = True))

Source: Internet
Author: User
Tags rollback

READ: From this point of time set the point of Start (Point a) to the end of this transaction, other transactions submitted data, the transaction will not be visible. (No data submitted by others after point A in the query)

Application occasions:

If you execute a single query statement at a time, it is not necessary to enable transaction support, and the database defaults to support read consistency during SQL execution;
If you execute more than one query at a time, such as a statistical query, report query, in this scenario, multiple query SQL must ensure the overall read consistency, otherwise, after the previous SQL query, after the SQL query, the data was changed by other users, then the overall statistical query will appear reading data inconsistent state, At this point, transaction support should be enabled.
"Note that multiple queries are executed at a time to count certain information, and in order to ensure the overall consistency of the data, read-only transactions are used"

How to set up:

For read-only queries, you can specify that the transaction type is ReadOnly, that is, a read-only transaction.
Because a read-only transaction does not have modifications to the data, the database provides some optimizations for read-only transactions, such as Oracle for read-only transactions, does not start rollback segments, and does not log rollback log.

(1) in JDBC, the method for specifying read-only transactions is: Connection.setreadonly (true);

(2) in Hibernate, the method for specifying read-only transactions is: Session.setflushmode (flushmode.never);
At this point, hibernate also provides some optimizations for the session for read-only transactions

(3) In spring's hibernate package, the method for specifying read-only transactions is: In the bean configuration file, the Prop attribute increases "readOnly"
or @transactional (readonly=true) in annotation mode
"If" transaction is marked as Read-only, Spring'll set the Hibernate session ' s flush mode to Flush_never,
And would set the JDBC transaction to Read-only "that is, setting up read-only transactions in spring is the use of the above two ways

When a transaction is set to read-only, the equivalent of setting the database to a read-only database, an error occurs when a write operation is made

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.