避免来自 Spring JPA 存储库的提交查询

avoid commit queries from spring jpa repository

提问人:Ronak Patel 提问时间:8/15/2023 最后编辑:Ronak Patel 更新时间:8/15/2023 访问量:34

问:

当我将传播更改为 OR - 查询时,spring 正在发出查询,但对表的插入(带有传播REQUIRES_NEW)不能立即用于其他只读查询/线程。它在几毫秒的延迟后可用。这是预期行为吗?一个线程使用 ratio = REQUIRED 写入表,但当 mysql 自动提交在数据库服务器设置中为 ON 时,其他线程无法看到使用 prorogation NEVER 或 SUPPORTS 的记录。@TransactionalcommitNEVERSUPPORTScommit

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();
    }
mysql 休眠 spring-data-jpa spring-transactions 自动提交

评论

0赞 Simon Martinelli 8/15/2023
请出示您的代码
0赞 Ronak Patel 8/15/2023
添加了@SimonMartinelli代码
0赞 Simon Martinelli 8/15/2023
你是如何衡量的?
0赞 Ronak Patel 8/16/2023
我在mysql服务器上启用了查询日志SET global general_log_file='/tmp/mysql.log'; SET global log_output = 'file';SET global general_log = on;
0赞 Simon Martinelli 8/16/2023
但是你没有检查代码中发生了什么。你应该调试这个

答: 暂无答案