In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of Spring transaction processing Transactional and concurrent threads". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Spring transaction propagation mechanism and database isolation level
Four transaction isolation levels are defined in the standard SQL specification, and different isolation levels are different for transaction processing.
Unauthorized read (Read Uncommitted): also known as uncommitted read. Dirty reads are allowed but updates are not allowed to be lost, and if one transaction has already started writing data, the other data is not allowed to write at the same time but other transactions are allowed to read this row of data. This isolation level can be achieved through an exclusive write lock. The lowest level of transaction isolation that only guarantees that physically corrupted data is not read. In contrast to the READ COMMITTED isolation level, it allows you to read data that has been modified by other users but has not yet been committed.
Authorized read (Read Committed): also known as committed read. Allows non-repeatable reads but does not allow dirty reads. This can be achieved through instant shared read locks and exclusive write locks, where transactions reading data allow other transactions to continue to access the row's data, but uncommitted write transactions will prevent other transactions from accessing the row. The default level of SQL Server. At this isolation level, the SELECT command does not return uncommitted (Committed) data, nor can it return dirty data.
Repeatable read (Repeatable Read): disable non-repeatable and dirty reads. But sometimes phantom data can occur, which can be achieved through "shared read locks" and "exclusive write locks". Reading data transactions will prohibit writing transactions (but read transactions are allowed), and write transactions will prohibit any other transactions. At this isolation level, the data read with the SELECT command will not be changed throughout the execution of the command. This option affects the performance of the system, and it is best not to use this isolation level if it is not necessary.
Serial (Serializable): also called serial readable. Strict transaction isolation, which requires transaction serialization. Transactions can only be executed one by one, but not concurrently. If transaction serialization cannot be achieved only through "row-level locks", you must ensure that the newly inserted data will not be accessed by the transaction that has just performed the query operation. The highest level of transaction isolation, with complete isolation between transactions. If the transaction runs at a serially readable isolation level, you can guarantee that any concurrent overlapping transactions are serial.
Isolation level update missing dirty read repeat read phantom read unauthorized read NYYY authorized read NNYY repeatable read NNNY serial NNNN
Spring specifies seven types of transaction propagation behaviors in the TransactionDefinition interface, which specify how transactions propagate when transaction methods and transaction methods are nested calls:
Package org.springframework.transaction.annotation;import org.springframework.transaction.TransactionDefinition; / * * Enumeration that represents transaction propagation behaviors for use * with the {@ link Transactional} annotation, corresponding to the * {@ link TransactionDefinition} interface. * * @ author Colin Sampaleanu * @ author Juergen Hoeller * @ since 1.2 * / public enum Propagation {/ * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction attribute of the same name. *
This is the default setting of a transaction annotation. * / REQUIRED (TransactionDefinition.PROPAGATION_REQUIRED), / * Support a current transaction, execute non-transactionally if none exists. * Analogous to EJB transaction attribute of the same name. *
Note: For transaction managers with transaction synchronization, * PROPAGATION_SUPPORTS is slightly different from no transaction at all, * as it defines a transaction scope that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on * the actual synchronization configuration of the transaction manager. * @ see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization * / SUPPORTS (TransactionDefinition.PROPAGATION_SUPPORTS), / * * Support a current transaction, throw an exception if none exists. * Analogous to EJB transaction attribute of the same name. * / MANDATORY (TransactionDefinition.PROPAGATION_MANDATORY), / * Create a new transaction, and suspend the current transaction if one exists. * Analogous to the EJB transaction attribute of the same name. *
Note: Actual transaction suspension will not work out-of-the-box on * all transaction managers. This in particular applies to JtaTransactionManager, * which requires the {@ code javax.transaction.TransactionManager} to be * made available it to it (which is server-specific in standard J2EE). * @ see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager * / REQUIRES_NEW (TransactionDefinition.PROPAGATION_REQUIRES_NEW), / * * Execute non-transactionally, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. *
Note: Actual transaction suspension will not work on out-of-the-box * on all transaction managers. This in particular applies to JtaTransactionManager, * which requires the {@ code javax.transaction.TransactionManager} to be * made available it to it (which is server-specific in standard J2EE). * @ see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager * / NOT_SUPPORTED (TransactionDefinition.PROPAGATION_NOT_SUPPORTED), / * * Execute non-transactionally, throw an exception if a transaction exists. * Analogous to EJB transaction attribute of the same name. * / NEVER (TransactionDefinition.PROPAGATION_NEVER), / * Execute within a nested transaction if a current transaction exists, * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB. *
Note: Actual creation of a nested transaction will only work on specific * transaction managers. Out of the box, this only applies to the JDBC * DataSourceTransactionManager when working on a JDBC 3.0 driver. * Some JTA providers might support nested transactions as well. * @ see org.springframework.jdbc.datasource.DataSourceTransactionManager implements nested transactions by creating Savepoint. If the inner transaction throws an exception (unchecked exception), it rolls back to savepoint, but does not affect the outer transaction. The rollback of the outer transaction rolls back the inner transaction together. * / NESTED (TransactionDefinition.PROPAGATION_NESTED); private final int value; Propagation (int value) {this.value = value;} public int value () {return this.value;}} / * * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "ASIS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * / package org.springframework.transaction; import java.sql.Connection; / * * Interface that defines Spring-compliant transaction properties. * Based on the propagation behavior definitions analogous to EJB CMT attributes. * *
Note that isolation level and timeout settings will not get applied unless * an actual new transaction gets started. As only {@ link # PROPAGATION_REQUIRED}, * {@ link # PROPAGATION_REQUIRES_NEW} and {@ link # PROPAGATION_NESTED} can cause * that, it usually doesn't make sense to specify those settings in other cases. * Furthermore, be aware that not all transaction managers will support those * advanced features and thus might throw corresponding exceptions when given * non-default values. * *
The {@ link # isReadOnly () read-only flag} applies to any transaction context, * whether backed by an actual resource transaction or operating non-transactionally * at the resource level. In the latter case, the flag will only apply to managed * resources within the application, such as a Hibernate {@ code Session}. * * @ author Juergen Hoeller * @ since 08.05.2003 * @ see PlatformTransactionManager#getTransaction (TransactionDefinition) * @ see org.springframework.transaction.support.DefaultTransactionDefinition * @ see org.springframework.transaction.interceptor.TransactionAttribute * / public interface TransactionDefinition {/ * Support a current transaction; create a new one if none exists. * Analogous to the EJB transaction attribute of the same name. *
This is typically the default setting of a transaction definition, * and typically defines a transaction synchronization scope. * / int PROPAGATION_REQUIRED = 0; / * * Support a current transaction; execute non-transactionally if none exists. * Analogous to the EJB transaction attribute of the same name. *
NOTE: For transaction managers with transaction synchronization, * {@ code PROPAGATION_SUPPORTS} is slightly different from no transaction * at all, as it defines a transaction scope that synchronization might apply to. * As a consequence, the same resources (a JDBC {@ code Connection}, a * Hibernate {@ code Session}, etc) will be shared for the entire specified * scope. Note that the exact behavior depends on the actual synchronization * configuration of the transaction manager! *
In general, use {@ code PROPAGATION_SUPPORTS} with care! In particular, do * not rely on {@ code PROPAGATION_REQUIRED} or {@ code PROPAGATION_REQUIRES_NEW} * within a {@ code PROPAGATION_SUPPORTS} scope (which may lead to * synchronization conflicts at runtime). If such nesting is unavoidable, make sure * to configure your transaction manager appropriately (typically switching to * "synchronization on actual transaction") * @ see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization * @ see org.springframework.transaction.support.AbstractPlatformTransactionManager#SYNCHRONIZATION_ON_ACTUAL_TRANSACTION * / int PROPAGATION_SUPPORTS = 1; / * * Support a current transaction; throw an exception if no current transaction * exists. Analogous to the EJB transaction attribute of the same name. *
Note that transaction synchronization within a {@ code PROPAGATION_MANDATORY} * scope will always be driven by the surrounding transaction. * / int PROPAGATION_MANDATORY = 2; / * Create a new transaction, suspending the current transaction if one exists. * Analogous to the EJB transaction attribute of the same name. *
NOTE: Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * @ link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@ code javax.transaction.TransactionManager} * to be made available it to it (which is server-specific in standard J2EE). *
A {@ code PROPAGATION_REQUIRES_NEW} scope always defines its own * transaction synchronizations. Existing synchronizations will be suspended * and resumed appropriately. * @ see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager * / int PROPAGATION_REQUIRES_NEW = 3; / * * Do not support a current transaction; rather always execute non-transactionally. * Analogous to the EJB transaction attribute of the same name. *
NOTE: Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * @ link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@ code javax.transaction.TransactionManager} * to be made available it to it (which is server-specific in standard J2EE). *
Note that transaction synchronization is not available within a * {@ code PROPAGATION_NOT_SUPPORTED} scope. Existing synchronizations * will be suspended and resumed appropriately. * @ see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager * / int PROPAGATION_NOT_SUPPORTED = 4; / * * Do not support a current transaction; throw an exception if a current transaction * exists. Analogous to the EJB transaction attribute of the same name. *
Note that transaction synchronization is not available within a * {@ code PROPAGATION_NEVER} scope. * / int PROPAGATION_NEVER = 5; / * Execute within a nested transaction if a current transaction exists, * behave like {@ link # PROPAGATION_REQUIRED} else. There is no analogous * feature in EJB. *
NOTE: Actual creation of a nested transaction will only work on * specific transaction managers. Out of the box, this only applies to the JDBC * {@ link org.springframework.jdbc.datasource.DataSourceTransactionManager} * when working on a JDBC 3.0 driver. Some JTA providers might support * nested transactions as well. * @ see org.springframework.jdbc.datasource.DataSourceTransactionManager * / int PROPAGATION_NESTED = 6; / * * Use the default isolation level of the underlying datastore. * All other levels correspond to the JDBC isolation levels. * @ see java.sql.Connection * / int ISOLATION_DEFAULT =-1; / * * Indicates that dirty reads, non-repeatable reads and phantom reads * can occur. *
This level allows a row changed by one transaction to be read by another * transaction before any changes in that row have been committed (a "dirty read"). * If any of the changes are rolled back, the second transaction will have * retrieved an invalid row. * @ see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED * / int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED; / * * Indicates that dirty reads are prevented; non-repeatable reads and * phantom reads can occur. *
This level only prohibits a transaction from reading a row * with uncommitted changes in it. * @ see java.sql.Connection#TRANSACTION_READ_COMMITTED * / int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED; / * * Indicates that dirty reads and non-repeatable reads are prevented; * phantom reads can occur. *
This level prohibits a transaction from reading a row with uncommitted changes * in it, and it also prohibits the situation where one transaction reads a row, * a second transaction alters the row, and the first transaction re-reads the row, * getting different values the second time (a "non-repeatable read"). * @ see java.sql.Connection#TRANSACTION_REPEATABLE_READ * / int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ; / * * Indicates that dirty reads, non-repeatable reads and phantom reads * are prevented. *
This level includes the prohibitions in {@ link # ISOLATION_REPEATABLE_READ} * and further prohibits the situation where one transaction reads all rows that * satisfy a {@ code WHERE} condition, a second transaction inserts a row * that satisfies that {@ code WHERE} condition, and the first transaction * re-reads for the same condition, retrieving the additional "phantom" row * in the second read. * @ see java.sql.Connection#TRANSACTION_SERIALIZABLE * / int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE; / * Use the default timeout of the underlying transaction system, * or none if timeouts are not supported. * / int TIMEOUT_DEFAULT =-1; / * * Return the propagation behavior. *
Must return one of the {@ code PROPAGATION_XXX} constants * defined on {@ link TransactionDefinition this interface}. * @ return the propagation behavior * @ see # PROPAGATION_REQUIRED * @ see org.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive () * / int getPropagationBehavior (); / * * Return the isolation level. *
Must return one of the {@ code ISOLATION_XXX} constants * defined on {@ link TransactionDefinition this interface}. *
Only makes sense in combination with {@ link # PROPAGATION_REQUIRED} * or {@ link # PROPAGATION_REQUIRES_NEW}. *
Note that a transaction manager that does not support custom isolation levels * will throw an exception when given any other level than {@ link # ISOLATION_DEFAULT}. * @ return the isolation level * / int getIsolationLevel (); / * * Return the transaction timeout. *
Must return a number of seconds, or {@ link # TIMEOUT_DEFAULT}. *
Only makes sense in combination with {@ link # PROPAGATION_REQUIRED} * or {@ link # PROPAGATION_REQUIRES_NEW}. *
Note that a transaction manager that does not support timeouts will throw * an exception when given any other timeout than {@ link # TIMEOUT_DEFAULT}. * @ return the transaction timeout * / int getTimeout (); / * * Return whether to optimize as a read-only transaction. *
The read-only flag applies to any transaction context, whether * backed by an actual resource transaction * ({@ link # PROPAGATION_REQUIRED} / {@ link # PROPAGATION_REQUIRES_NEW}) or * operating non-transactionally at the resource level * ({@ link # PROPAGATION_SUPPORTS}). In the latter case, the flag will * only apply to managed resources within the application, such as a * Hibernate {@ code Session}.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.