简体   繁体   English

在多线程环境中重用ResultSet成员变量

[英]Reuse of ResultSet member variable in a multi-threaded environment

I am implementing a code that uses JDBC driver. 我正在实现使用JDBC驱动程序的代码。

Below is the code that I made. 下面是我编写的代码。

public class MysqlUtils {
    public Connection conn;
    public ResultSet rs;
    public PreparedStatement stmt;

    public MysqlUtils(String address, String id, String passwd) {
        try {
            conn = DriverManager.getConnection(address, id, passwd);
            stmt = null;
            rs = null;
        } catch (SQLException e) {
            // error management
        }
    }

    public void getSomeData(long id) {
        try {
            stmt = conn.prepareStatement("SELECT * FROM some_table");
            rs = stmt.executeQuery();
            rs.next();
            System.out.println(rs.getString("some_column");
        } catch (SQLException e) {
            // error management
        }
    }
}

I have declared Connection conn, ResultSet rs, PreparedStatement stmt as member variables because somehow I thought that might help me enhance performance. 我已经将Connection conn,ResultSet rs,PreparedStatement stmt声明为成员变量,因为我以某种方式认为这可以帮助我提高性能。

I have a couple of questions. 我有一些问题。

  1. If I call getSomeData() consecutively, will stmt and rs be assigned new objects every time? 如果我连续调用getSomeData(),是否每次都会给stmt和rs分配新的对象?

  2. Regardless of the answer to the question above, if I run this code in a multi-threaded environment(multiple threads using MysqlUtils class), will there be a mix-up because I didn't declare ResultSet rs in getSomeData()? 不管上述问题的答案是什么,如果我在多线程环境(使用MysqlUtils类使用多个线程)中运行此代码,是否会因为我没有在getSomeData()中声明ResultSet rs而产生混乱?

  3. Was declaring Connection conn, ResultSet rs, PreparedStatement stmt as member variables a bad choice? 将Connection conn,ResultSet rs,PreparedStatement stmt声明为成员变量是否是错误的选择? In other words, is my implementation of JDBC a viable one? 换句话说,我的JDBC实现是否可行?

Thanks for the help. 谢谢您的帮助。

  1. Yes. 是。 The method will be executed, and thus stmt and rs will take new values. 该方法将被执行,因此stmtrs将采用新值。 Of course, you might have multiple instances of your class, and thus multiple instances of those two fields. 当然,您可能有您的类的多个实例,因此这两个字段的多个实例。
  2. Yes. 是。 This code is completele thread-* un *safe. 这段代码是完整的thread- * un * safe。 Public fields in general should almost always be avoided. 一般而言,应始终避免公共领域。 Especially in a multi-threaded environment 特别是在多线程环境中
  3. Yes, it's a bad choice. 是的,这是一个糟糕的选择。 The scope of a variable should be as small as possible. 变量的范围应尽可能小。 And these variables are used in a single method, and reassigned every time. 这些变量仅在一种方法中使用,并且每次都重新分配。 They should be local variable. 它们应该是局部变量。

Also: 也:

  • a method getSomeData() should return something, and not just printing something 方法getSomeData()应该返回某些内容,而不仅仅是打印一些内容
  • the ResultSet and the Statement should be closed, in a finally block 在finally块中应关闭ResultSet和Statement
  • I hope the error management doesn't consist in swallowing the exception 我希望错误管理不会包含吞下异常

I would advise using spring-jdbc, which takes care of all the plumbing code for you, and avoids all the problems your code currently has. 我建议使用spring-jdbc,它可以为您处理所有管道代码,并避免了代码当前存在的所有问题。

不要在方法之外使用ResultSet。...使用while(rs.next)(rs = resultSet)循环数据库表并检索值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM