有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在事务中关闭连接时会发生什么?

我正在使用SQLServer和弹簧JtaTransactionManager

给出这样一种方法:

@Transactional
public void something() {
    try (final Connection conn1 = getConnection()) {
        //insert/update stuff in database
    }


    try (final Connection conn2 = getConnection()) {
        //insert/update stuff in database
    }
}

这种方法实际上是事务性的吗?当conn1关闭时所做的更改会发生什么变化,它们是否会提交到数据库?如果在使用conn2时发生异常,那么通过关闭的连接所做的更改是否可以回滚


共 (1) 个答案

  1. # 1 楼答案

    您不应该在@Transactional内手动加载/获取连接或数据源,尤其是在使用JtaTransactionManager

    The JtaTransactionManager does not need to know about the DataSource, or any other specific resources, because it uses the container’s global transaction management infrastructure.