【发布时间】:2022-07-22 14:37:51
【问题描述】:
我有这个从另一个服务调用的方法:
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void execute(String sql) {
Query query = entityManager.createNativeQuery(sql);
query.executeUpdate();
}
基本上客户端加载多个sql文件并在新事务中运行每个sql文件,以免影响其他文件的执行。
例如,这是一个 sql 文件的示例,它正在清理一些数据:
begin;
delete from table t where t.created_at < current_date - interval '2 month';
commit;
我要做的是记录每笔交易的结果。例如这里,我想显示删除了多少条记录。我怎么能从 Spring 开始呢?我知道您可以通过以下方式记录更具体的内容:
logging.level.org.springframework.transaction=TRACE
,但我仍然看不到任何结果。这揭示了有关将运行的 sql 以及事务何时开始/结束的信息。
第二个解决方案是检查以下结果:
int count = query.executeUpdate();
,但计数为 0,即使执行了 sql 代码并删除了数百行。
感谢您的建议!
【问题讨论】:
-
int count = query.executeUpdate();将是正确的方法,但我猜你没有得到结果,因为 SQL 文件执行自己的事务
标签: java spring-boot hibernate spring-data-jpa spring-data