我将隔离级别设置为READ_UNCOMMITTED,使用调试点更新两个select查询之间的值,我断言第一个和第二个read值不相同,但实际上第二个select值等于第一个select值。
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void readUncommitted() {
String firstReadPassword = userRepository.findFirstByUsername("admin").map(User::getPassword).get();
log.info("first read user admin password is {}", firstReadPassword);
// debug point below, and do update password
String secondReadPassword = userRepository.findFirstByUsername("admin").map(User::getPassword).get();
log.info("second read user admin password is {}", secondReadPassword);
assertNotEquals(firstReadPassword, secondReadPassword);
}
}
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
Optional<User> findFirstByUsername(String username);
}
@SpringBootTest
public class ReadUncommittedTest {
@Configuration
@ComponentScan("com.pohvii.playground.transaction.readuncommitted.service")
@EntityScan("com.pohvii.playground.users.entity")
@EnableJpaRepositories(basePackages = {"com.pohvii.playground.transaction.readuncommitted.repository"})
@Import({
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
})
@EnableTransactionManagement
static class TestConfiguration {
}
@Autowired
private UserService userService;
@Test
void readUncommittedTest() throws Exception {
userService.readUncommitted();
}
}
下面是我的测试日志
2021-05-08 10:15:27.939 TRACE 45788 --- [ main] t.a.AnnotationTransactionAttributeSource : Adding transactional method 'com.pohvii.playground.transaction.readuncommitted.service.impl.UserServiceImpl.readUncommitted' with attribute: PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED
2021-05-08 10:15:31.497 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Bound value [org.springframework.jdbc.datasource.ConnectionHolder@7b5021d1] for key [HikariDataSource (NotebookHikariCP)] to thread [main]
2021-05-08 10:15:31.498 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Bound value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] to thread [main]
2021-05-08 10:15:31.498 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Initializing transaction synchronization
2021-05-08 10:15:31.498 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Getting transaction for [com.pohvii.playground.transaction.readuncommitted.service.impl.UserServiceImpl.readUncommitted]
2021-05-08 10:15:32.681 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:15:32.681 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@7b5021d1] for key [HikariDataSource (NotebookHikariCP)] bound to thread [main]
2021-05-08 10:15:32.681 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findFirstByUsername]
2021-05-08 10:15:32.686 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:15:32.769 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:15:32.777 DEBUG 45788 --- [ main] org.hibernate.SQL : select user0_.id as id1_0_, user0_.password as password2_0_, user0_.password_salt as password3_0_, user0_.role as role4_0_, user0_.username as username5_0_, user0_.version as version6_0_ from users user0_ where user0_.username=? limit ?
2021-05-08 10:15:32.914 TRACE 45788 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [admin]
2021-05-08 10:15:32.977 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findFirstByUsername]
2021-05-08 10:15:32.977 INFO 45788 --- [ main] c.p.p.t.r.service.impl.UserServiceImpl : first read user admin password is TohH
2021-05-08 10:15:33.038 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.toString]: This method is not transactional.
2021-05-08 10:15:33.045 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.toString]: This method is not transactional.
2021-05-08 10:16:11.336 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:16:11.337 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@7b5021d1] for key [HikariDataSource (NotebookHikariCP)] bound to thread [main]
2021-05-08 10:16:11.337 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findFirstByUsername]
2021-05-08 10:16:11.337 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:16:11.337 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] bound to thread [main]
2021-05-08 10:16:11.345 DEBUG 45788 --- [ main] org.hibernate.SQL : select user0_.id as id1_0_, user0_.password as password2_0_, user0_.password_salt as password3_0_, user0_.role as role4_0_, user0_.username as username5_0_, user0_.version as version6_0_ from users user0_ where user0_.username=? limit ?
2021-05-08 10:16:11.345 TRACE 45788 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [admin]
2021-05-08 10:16:11.567 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findFirstByUsername]
2021-05-08 10:16:11.589 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.toString]: This method is not transactional.
2021-05-08 10:16:11.597 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.toString]: This method is not transactional.
2021-05-08 10:16:33.829 INFO 45788 --- [ main] c.p.p.t.r.service.impl.UserServiceImpl : second read user admin password is TohH
2021-05-08 10:16:33.859 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : No need to create transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.toString]: This method is not transactional.
2021-05-08 10:16:35.791 TRACE 45788 --- [ main] o.s.t.i.TransactionInterceptor : Completing transaction for [com.pohvii.playground.transaction.readuncommitted.service.impl.UserServiceImpl.readUncommitted] after exception: org.opentest4j.AssertionFailedError: expected: not equal but was: <TohH>
2021-05-08 10:16:35.791 TRACE 45788 --- [ main] o.s.t.i.RuleBasedTransactionAttribute : Applying rules to determine whether transaction should rollback on org.opentest4j.AssertionFailedError: expected: not equal but was: <TohH>
2021-05-08 10:16:35.791 TRACE 45788 --- [ main] o.s.t.i.RuleBasedTransactionAttribute : Winning rollback rule is: null
2021-05-08 10:16:35.791 TRACE 45788 --- [ main] o.s.t.i.RuleBasedTransactionAttribute : No relevant rollback rule found: applying default rules
2021-05-08 10:16:35.878 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Clearing transaction synchronization
2021-05-08 10:16:35.878 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Removed value [org.springframework.orm.jpa.EntityManagerHolder@21d9cd04] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@260e3837] from thread [main]
2021-05-08 10:16:35.878 TRACE 45788 --- [ main] .s.t.s.TransactionSynchronizationManager : Removed value [org.springframework.jdbc.datasource.ConnectionHolder@7b5021d1] for key [HikariDataSource (NotebookHikariCP)] from thread [main]
将对findfirstbyusername().get()
调用的结果分配给单独的变量,并在调试时比较它们的引用ID。你会有答案的。
正确的测试涉及在两个调用之间调用EntityManager.clear()
。
我将spring应用程序连接到smartbaer servicev,在那里创建了虚拟数据源(Postgres)。 驱动程序类: 连接字符串(本地servicev虚拟服务器url): 应用程序.属性:- spring.datasource.driver-class-name=com.smartbear.servicev.jdbc.driver.jdbcvirtdriver spring.dataso
问题内容: 我想将隔离级别设置为。如何使用gorm orm for postgres实现此目的。 示例代码: 问题答案: 我在这里有完全一样的问题: 和pg完全一样。
问题内容: 也许有人可以帮助我解决Spring(3.1)/ Postgresql(8.4.11)中的事务性问题 我的交易服务如下: Spring配置Webapp包含: 让我们说一个请求“ x”和一个请求“ y”同时执行并到达注释“比较”(方法insertObject)。然后,允许他们两个都插入一个新对象,并提交他们的事务。 为什么我没有RollbackException?据我所知,这就是可序列化等
本文向大家介绍浅析MYSQL REPEATABLE-READ隔离级别,包括了浅析MYSQL REPEATABLE-READ隔离级别的使用技巧和注意事项,需要的朋友参考一下 REPEATABLE-READ 即可重复读,set autocommit= 0或者START TRANSACTION状态下select表的内容不会改变。这种隔离级别可能导致读到的东西是已经修改过的。 比如: 回话一中读取一个字段
我想使用eclipse链接设置隔离级别,我尝试了以下两种方法: > java.sql.连接 Database aseLogin setTransationIsolation方法 如您所见,getTransactionSolation()方法的返回值之间存在一些不一致。我的问题是,在这两种情况下真正设置了哪种事务隔离?我知道eclipse链接在默认情况下使用不同的连接进行读写操作,即Database
我有一个对具有 SQL API 的 Cosmos 容器的长期运行查询,该查询需要 10 分钟以上才能完成。有没有办法将数据库的隔离级别设置为“可重复读取”或“可序列化”,以避免幻像读取,如这里所定义的那样? 我知道我们可以为 Cosmos DB 设置一致性级别,但它仅适用于地质副本之间的读/写一致性,而不适用于事务隔离。
本文向大家介绍事务的隔离级别有哪些?相关面试题,主要包含被问及事务的隔离级别有哪些?时的应答技巧和注意事项,需要的朋友参考一下 SQL 标准定义了四个隔离级别: READ-UNCOMMITTED(读取未提交): 最低的隔离级别,允许读取尚未提交的数据变更,可能会导致脏读、幻读或不可重复读。 READ-COMMITTED(读取已提交): 允许读取并发事务已经提交的数据,可以阻止脏读,但是幻读或不可重
JPA锁(乐观/悲观)和数据库隔离级别(以http://www . PostgreSQL . org/docs/9.1/static/transaction-iso . html为例)之间有没有相互影响? EJB3.2规范(8.3.2“隔离级别”)说Bean提供者负责设置事务的隔离级别,所以一般情况下我应该不会在意,但无论如何我还是很困惑。在PostgreSQL的示例中,根据提到的来源,默认隔离级