【问题标题】:Why close method of class that implements Autoclosable interface is called first than Sql connection close?为什么实现Autoclosable接口的类的close方法比Sql连接关闭先调用?
【发布时间】:2021-06-07 00:18:38
【问题描述】:

我找到了在提交失败时回滚事务的解决方案,并且效果很好。 但是你能解释一下为什么先调用 Autorollback 对象关闭方法而不是连接关闭吗?

自动回滚类:

public class AutoRollback implements AutoCloseable {
    private Connection conn;
    private boolean committed;
    public AutoRollback(Connection conn){
        this.conn = conn;
    }
    public void commit() throws SQLException {
        conn.commit();
        committed = true;
    }
    @Override
    public void close() throws SQLException {
        if(!committed) {
            conn.rollback();
        }
    }
}

使用自动回滚的服务方法示例:

try(Connection connection = MySQLDAOFactory.getConnection();
            AutoRollback autoRollback = new AutoRollback(connection)){
            result =  carDao.insertCar(connection,car);
            autoRollback.commit();
        } catch (SQLException | NamingException | MySQLEXContainer.MySQLDBExecutionException throwables) {
            throw new ApplicationEXContainer.ApplicationCanNotChangeException(throwables.getMessage(),throwables);
        }

为什么Autorollback的close方法会起作用?如果连接关闭了怎么调用rollback方法呢?所以唯一的解释是autorollback close方法在连接关闭之前被调用,但是为什么呢?

【问题讨论】:

    标签: java mysql database-connection try-with-resources autocloseable


    【解决方案1】:

    因为这就是 JLS 说它必须工作的方式:

    try-with-resources 语句使用变量(称为资源)进行参数化,这些变量在执行 try 块之前被初始化并以与初始化相反的顺序自动关闭,之后执行 try 块。

    Source

    【讨论】:

    • ...这是唯一有意义的顺序。
    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多