spring transaction problem, in a method, you need to update the data of a table and use the latest data. How should you write annotations using things?
spring transaction problem, in a method, you need to update the data of a table and use the latest data. How should you write annotations using things?
//ApropagationREQUIRED
public void A(){
//B,BpropagationREQUIRES_NEW
B();
//C,CpropagationREQUIRES_NEW
C();
}
this should be a problem with the isolation level of database transactions. The default isolation level of the database is repeatable
, which means that the operation within the transaction is visible in this transaction, but before committing, the operation does not affect other things, and other things cannot see the operation. The four isolation levels of
database are demonstrated below with mysql
database:
create table test (id int not null,name vachar(20),primary key(id));
insert into test (1,'AA');
insert into test (2,'BB');
insert into test (3,'CC');
insert into test (4,'DD');
insert into test (5,'EE');
READ UNCOMMITTED
uncommitted reads, changes in transactions, even if they are not committed, are visible to other transactions, and the update operation of transaction 1 is visible when transaction 2 is not committed. Under this isolation level, there may be dirty reading, unrepeatable reading, phantom reading, and so on. //1
//
select @@tx_isolation;
//
set session transaction isolation level READ UNCOMMITTED;
//
set autocommit = off;
//
begin;
// test
select * from test;
//
update test set name = 'AAAAA' where id = 1;
//test
//
select * from test;
//,
rollback;
READ COMMITTED
(committed read / non-repeatable read): any changes made by the transaction from the beginning to the commit are not visible to other transactions. or the above example, the update operation of thing 1 is invisible to thing 2 before it is submitted, and can only be seen after it is submitted. Under this isolation level, there may be unrepeatable readings, phantom readings, and so on.
REPEATABLE READ
(repeatable): the result of reading the same record multiple times in the same transaction is consistent, and the MySQL default isolation level. There may be phantom reading at this level of isolation. SERIALIZABLE
(serialization): transactions are executed serially, read and write locked. the above four isolation levels reflect the degree of interaction between things. But in the same thing, its update operations are visible to itself. It doesn't exist in the same thing, it's updated above, but it can't be seen below. Unless you are using the propagation characteristics of spring things, you open another thing in the thing, so that in two things, the update of one thing and the other thing cannot be seen.
add:
I wrote an example with SpringBoot
. Under the influence of transaction propagation attribute and isolation level, the previous update may occur, but you can't see it later. Here is the code. Pay attention to the comments.
SpringBoot pom.xml
the file adds the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
main class:
@EnableAutoConfiguration
@ComponentScan
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringBootApplication.class);
Map<String, Object> defaultMap = new HashMap<String, Object>();
//
defaultMap.put("spring.datasource.driverClassName", "com.mysql.jdbc.Driver");
defaultMap.put("spring.datasource.url", "jdbc:mysql://192.168.1.105:3306/sakila?useUnicode=true&characterEncoding=utf-8");
defaultMap.put("spring.datasource.username", "root");
defaultMap.put("spring.datasource.password", "root");
application.setDefaultProperties(defaultMap);
ApplicationContext context = application.run(args);
//1
SpringTransaction_1 transaction = context.getBean("springTransaction_1", SpringTransaction_1.class);
transaction.transaction_1();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("JVM.....");
}));
}
}
thing 1:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
public class SpringTransaction_1 {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
SpringTransaction_2 springTransaction_2;
/**
*
*
* :transaction_1transaction_2transaction_2
* AOP
*/
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
public void transaction_1(){
List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from test");
System.out.println("initial view in transaction_1");
list.forEach(System.out::println);
springTransaction_2.transaction_2();
list = jdbcTemplate.queryForList("select * from test");
System.out.println("view in transaction_1 after transaction_2");
list.forEach(System.out::println);
}
}
thing 2:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Service
public class SpringTransaction_2 {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* @Transactional REQUIREDTransaction1
* REPEATABLE_READ,transaction_1
*
* REQUIRES_NEWREPEATABLE_READ,
* transaction_1
*/
@Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.REPEATABLE_READ)
public void transaction_2(){
List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from test");
System.out.println("initial view in transaction_2");
list.forEach(System.out::println);
System.out.println("update record in transaction_2");
System.out.println("update test set name = 'AB' where id = 1");
jdbcTemplate.update("update test set name = 'AB' where id = 1");
System.out.println("view in transaction_2 after update");
list = jdbcTemplate.queryForList("select * from test");
list.forEach(System.out::println);
}
}
output:
transaction_1 begin
initial view in transaction_1
{id=1, name=AB}
transaction_2 begin
initial view in transaction_2
{id=1, name=AB}
update record in transaction_2
update test set name = 'AAAS' where id = 1
view in transaction_2 after update
{id=1, name=AAAS}
transaction_2 end
view in transaction_1 after transaction_2
//transaction_2transaction_1 transaction_1
{id=1, name=AB}
transaction_1 end
the complete code is placed in github
Previous: React, initiates a request using this.props.dispatch (actions.update), but does not execute
Why did spring fail to automatically match fields? Wechat s front end reads as follows: Java: ...
Front-end proxy server interface controlleraxios postaxiosgetoption tried to implement the filter interface in the background, but it didn t work. Later, I deleted it. Anyway, I have tried many methods that can be found on the Internet . ...
such as the title. such as the title. such as the title. ...
1. Recently, when I was learning hibernate, a question suddenly occurred to me: can I use hibernate instead of a database to store data? Can it cooperate with hibernate with simple file access? 2. In order to answer this question, I think we should fi...
there is a set collection of tag classes in the user class; maintain relationships only from the user class, one-to-many @ OneToMany @JoinTable(name="shuo_tag", inverseJoinColumns=@JoinColumn(name="tag_id"), jo...
thymeleaf 2.+ requires < script th:inline= "javascript " > declare this Is there a better way after thymeleaf3? can the declaration be omitted? I read the document saying that it is recommended to remove . Template updates remove any th:inline= ...
when I actually use it, it s not what the documentation says. my thymeleaf version The code calls the tostring method of the class in this way <script> console.log([[${games}]]) < script> chorme console: an error will be prompted, b...
tomcat can be accessed normally at the beginning, but after a week or two of operation, it cannot be accessed normally, and it does not report an error, but it just cannot be accessed, which is very strange. jdk and tomcat are both 32-bit, is that the r...
questions about parsing parameters in request and putting them back in request get and post follow the method , parsing is normal, but an exception is thrown when a put request is encountered. put requests request of spring , while tomc...
problem description: the front end invokes a service An at the back end, and this service A contains the sub-service BMagne C * C * C has its own independent transaction mechanism and does not affect each other. After is executed, the final return fro...
there is a problem that I can t solve when I just learned ssm,. No, no, no. The directory structure is as follows error message: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTes...
error prompt: 08-Apr-2018 22:43:20.746 [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bea...
requirements: http: localhost:80 order service?method=getTradeInfo http: localhost:80 order service?method=createTrade requires method to decide which method to map to which controller, and how should it be implemented? now only knows that spring...
the work java spring does is not caused by the language feature limitations of java. so python should need something similar, but why didn t it develop? ...
several pages are packaged with vue-cil and published to the java server. The value sent by, spring mvc to vue,vue has not been received. Can vue use ${variable} to receive the value? Because the page developed by vue is released to the backend, and the ...
problem description: basically prevents users from making malicious requests, limits the number of visits per unit time , and then makes an aop, but I can t get the return value of the aop method in the controller layer, as follows: @Pointcut("...
RT. I want to verify that in the request InnerValidatedRequest, if the fields houseName and houseAddress, in the InnerValidatedRequestData member object are annotated with the @ Validated of SpringMVC, what else do you need to add? if this is the c...
package spittr.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springfra...
in the spring in actin fourth edition, Chapter V 5.4.2 check data section, use JSR303 for data verification, but it does not take effect, and there is no error uses the implementation of hibernate-validator, and gradle depends on: @RequestMapping(va...
when learning springboot, add: to pom.xml <dependency> <groupId>org.springframework.boot< groupId> <artifactId>spring-boot-starter-actuator< artifactId> < dependency> After restart the projec...