提问人:Ronak Patel 提问时间:8/15/2023 最后编辑:Ronak Patel 更新时间:8/15/2023 访问量:34
避免来自 Spring JPA 存储库的提交查询
avoid commit queries from spring jpa repository
问:
当我将传播更改为 OR - 查询时,spring 正在发出查询,但对表的插入(带有传播REQUIRES_NEW)不能立即用于其他只读查询/线程。它在几毫秒的延迟后可用。这是预期行为吗?一个线程使用 ratio = REQUIRED 写入表,但当 mysql 自动提交在数据库服务器设置中为 ON 时,其他线程无法看到使用 prorogation NEVER 或 SUPPORTS 的记录。@Transactional
commit
NEVER
SUPPORTS
commit
2023-08-14T21:04:35.329619Z 1028 Query set session transaction read only
2023-08-14T21:04:35.329818Z 1028 Query SET autocommit=0
2023-08-14T21:04:35.330616Z 1028 Query select c1_0....<from table1>
2023-08-14T21:04:35.331177Z 1028 Query select f1_0....<from table2>
2023-08-14T21:04:35.331687Z 1028 Query commit
示例代码:
//one thread creating db rec using this
@Transactional(transactionManager = MYSQL_TRANSACTION_MANAGER, propagation = Propagation.REQUIRES_NEW)
public void persist(Foo foo) throws Exception {
em.persist(foo);
}
//other thread reading rec inserted by above method - this read is after over method completion - not concurrent - but possibly different db connection
@Transactional(readOnly = true,
transactionManager = MYSQL_TRANSACTION_MANAGER,
propagation = Propagation.SUPPORTS) //or NEVER
public Foo find(long id){
//String sql = ...;
Query q = em.createQuery(sql);
return q.getFirstResult();
}
答: 暂无答案
评论
SET global general_log_file='/tmp/mysql.log'; SET global log_output = 'file';SET global general_log = on;