在交易中关闭连接时会发生什么? [英] What happens when a connection is closed mid-transaction?

查看:58
本文介绍了在交易中关闭连接时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQLServer 和Springs JtaTransactionManager .

I'm using SQLServer along with Springs 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 时发生异常,会发生什么情况,通过封闭连接进行的更改是否可以回滚?

Will this method actually be transactional? What happens to the changes made when conn1 gets closed, are they commited to the database? What happens if an exception occurs while working with conn2, can the changes made through a closed connection be rolled back?

推荐答案

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

You shouldn't load/get connection or datasource manually inside @Transactional, especially when using JtaTransactionManager

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.

这篇关于在交易中关闭连接时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆