使用一个连接样式选择执行两个Java PreparedStatements [英] Executing two Java PreparedStatements with one connection - style choice

查看:157
本文介绍了使用一个连接样式选择执行两个Java PreparedStatements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经意识到我确实在没有回馈社区的情况下问了太多问题,但我希望你对此有所了解。假设我有

Okay, I've realized that I really have asked way too many questions without contributing back to the community, but I want your opinions on this. Say if I have

private void closeAll(ResultSet rs, PreparedStatement ps, Connection con) {
    if (rs != null)
        try {
            rs.close();
        } catch (SQLException e) {
        }
    if (ps != null)
        try {
            ps.close();
        } catch (SQLException e) {
        }
    if (con != null)
        try {
            con.close();
        } catch (SQLException e) {
        }
}

我想用一个连接在MySQL数据库上做几个操作。是否更好写

and I wanted to do several operations on my MySQL database using a single connection. Is it better to write

Connection con = ...;
PreparedStatement ps = null;
ResultSet rs = null;
try {
    ps = con.prepareStatement(...);
    rs = ps.executeQuery();
    if (rs.next()) ...;
} catch (SQLException e) {
    System.err.println("Error: " + e);
} finally {
    closeAll(rs, ps, null);
}
try {
    ps = con.prepareStatement(...);
    rs = ps.executeQuery();
    if (rs.next()) ...;
} catch (SQLException e) {
    System.err.println("Error: " + e);
} finally {
    closeAll(rs, ps, con);
}

Connection con = ...;
PreparedStatement ps = null;
ResultSet rs = null;
try {
    ps = con.prepareStatement(...);
    rs = ps.executeQuery();
    if (rs.next()) ...;
    rs.close();
    ps.close();

    ps = con.prepareStatement(...);
    rs = ps.executeQuery();
    if (rs.next()) ...;
} catch (SQLException e) {
    System.err.println("Error: " + e);
} finally {
    closeAll(rs, ps, con);
}

我认为更好的意思是更安全,更清晰,更简洁或更健壮。我不确定后者是否总是关闭任何准备好的语句和结果集在遇到异常时都是打开的,而我相信它确实看起来更简洁。但前者看起来更好,因为它更加一致,但由于它使用了更多的try finally块,所以它会增加开销。

I consider better to mean either safer, clearer, more concise, or more robust. I'm not sure whether the latter will always close whichever prepared statements and result sets are open whenever it encounters an exception, while I believe it does look more concise. But the former looks nicer since it's more consistent, yet it puts more overhead since it uses more try finally blocks.

我意识到Java 7的自动资源管理是Project Coin的一部分将迫使我倾向于前者,因为标题中使用的资源在内部是隐式最终的。但是,在我不得不担心修改我的代码以使其适应ARM并且能够删除样板代码之前我还有一段时间,所以问题仍然存在:在上述两种样式中,哪种更好的做法?如果他们都做了预期的行为,后者会给我一个明显的性能提升,可以原谅丑陋的风格吗?

I realize that Java 7's automatic resource management part of Project Coin will force me to lean to the former since the resources used in the header are implicitly final in the body. However, I have quite some time before I have to worry about revising my code to adapt it to ARM and be able to remove the boilerplate code, so the question still stands: of the above two styles, which would be better practice? If they both do the expected behaviors, will the latter give me a noticeable performance boost that would excuse the "uglier" style?

推荐答案

在我看来,这是个人偏好的情况。在我的时代,我编写了类似于两个块的代码。至于性能,我认为不会有特别显着的差异,只有性能测试才能说明。

It seems to me this is a case of personal preference. In my time I've written code that resembles both blocks. As regards performance I don't think there would be a particularly noticeable difference, only performance tests would tell.

可能是这样一种情况:一个版本提供的毫秒左右的差异并不是十分钟左右的另一个人读取代码六个月的重要一半在你写完之后就会花钱询问原因。

It might be a case that the millisecond or so difference that one version delivers isn't half as important as the ten minutes or so that another person reading your code six months after you've written it would spend asking why.

我个人的偏好是第二个。你说你持有一个对数据库开放的连接。使用第一个代码块,如果你得到一个异常抛出,那将被处理但是你会下降到第二个try / catch并再试一次。如果第一个失败,您可能不希望这样。对于第二个,例外情况会导致您退出代码然后关闭连接。

My personal preference would be the second. You say you're holding a connection open to the database. With the first block of code if you get an exception thrown, that would be handled but then you'd drop down to the second try/catch and try again. You might not want that if the first one failed. With the second, an exception would cause you come out of the code and then close your connection.

我主要使用C#进行编程。大约八年前,我上次做任何Java。但我认为有很多C#程序员都在考虑这个问题。无论如何我都有。

I've programmed mainly in C#. It was about eight years ago when I last did any Java. But I think there is and have been plenty of C# programmers who've pondered this. I have at any rate.

这篇关于使用一个连接样式选择执行两个Java PreparedStatements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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