Java IO 错误:从读取调用中得到减一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23347968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:07:22  来源:igfitidea点击:

IO Error: Got minus one from a read call

javadatabasemultithreadingoraclejdbc

提问by OmniOwl

I am running out of time and ideas. I need to make this simulation of booking an entire plane using Java and an Oracle Database.

我已经没有时间和想法了。我需要使用 Java 和 Oracle 数据库模拟预订整个飞机。

We have a few instructions on how to do it and what is expected, but our code keeps having this really odd and unexpected behaviour.

我们有一些关于如何做和预期的说明,但我们的代码一直有这种非常奇怪和意外的行为。

The database looks like this:

数据库看起来像这样:

Database

数据库

We need to have a pool of threads running constantly to simulate 10 simultaneous users trying to book a plane. When a thread ends, another one takes its place. Some of the threads have to do it instantly (random chance) both reserve and book. Others will reserve, then decide for a second if they want to book or not. If they decide to wait and then book another thread might reserve in the meantime which means the thread lose that reservation and will terminate. It can also wait for an extended time, in which it will be considered as a timeout.

我们需要一个不断运行的线程池来模拟 10 个同时尝试预订飞机的用户。当一个线程结束时,另一个线程取而代之。一些线程必须立即(随机机会)预订和预订。其他人会预订,然后再决定是否要预订。如果他们决定等待然后预订另一个线程可能会在此期间保留,这意味着该线程将丢失该保留并将终止。它还可以等待较长时间,在此期间将被视为超时。

Now here are the weird parts:

现在这里是奇怪的部分:

  1. It then tells me the error of the title.
  2. It then shows me to the HelperClass.java line about checking if everything is booked (included below).
  1. 然后它告诉我标题的错误。
  2. 然后它向我展示了 HelperClass.java 行,关于检查是否所有东西都被预订了(包括在下面)。

From Master.java

来自 Master.java

public static void main(String[] args) throws InterruptedException, ExecutionException {

    long start = System.nanoTime();
    Random r = new Random(start);
    ExecutorService pool = Executors.newFixedThreadPool(10);
    int threadsStarted = 0;
    List<Future<Integer>> results = new ArrayList<>();
    do {
        // TODO: Check for available seats left
        long id = r.nextLong();
        Future<Integer> submit = pool.submit(new UserThread(id));
        results.add(submit);
        threadsStarted++;
        id++;
    } while (!HelperClass.isAllBooked("CR9"));

    pool.shutdown();

From HelperClass.java

来自 HelperClass.java

public static boolean isAllBooked(String plane_no) {
    String sql = "SELECT * FROM SEAT WHERE plane_no=?";

    try(Connection conn = getConnection("db_010", "db2014");
            PreparedStatement st = conn.prepareStatement(sql)) {
        st.setString(1, plane_no);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            int i = rs.getInt("BOOKED");
            if (rs.wasNull()) {
                return false;
            }
        }
    } catch (SQLException ex) {
        System.out.println("[HELPER CLASS] SQL ERROR: " + ex.getMessage());
    }
    return true;
}

To not fill this post with code step by step, I just stop here and will provide if you need more to see the issues in this code. Please note that this is prototyping code and is not supposed to be amazingly secure or follow every code ethic on earth.

为了不一步一步地用代码填充这篇文章,我只是停在这里,如果您需要更多信息来查看此代码中的问题,我会提供。请注意,这是原型代码,不应具有惊人的安全性或遵循地球上的所有代码道德。

Full stack:

全栈:

[HELPER CLASS] SQL ERROR: IO Error: Got minus one from a read call
Exception in thread "main" java.lang.NullPointerException
    at dbassignment4.HelperClass.isAllBooked(HelperClass.java:50)
    at dbassignment4.Master.main(Master.java:36)
apr 28, 2014 8:02:59 PM dbassignment4.UserThread getConnection
SEVERE: null
java.sql.SQLRecoverableException: IO Error: Got minus one from a read call
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at dbassignment4.UserThread.getConnection(UserThread.java:76)
    at dbassignment4.UserThread.reserve(UserThread.java:63)
    at dbassignment4.UserThread.call(UserThread.java:35)
    at dbassignment4.UserThread.call(UserThread.java:21)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: oracle.net.ns.NetException: Got minus one from a read call
    at oracle.net.ns.Packet.receive(Packet.java:314)
    at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:153)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ... 15 more

BUILD STOPPED (total time: 36 seconds)

回答by Marshall Tigerus

I think this might be the problem

我认为这可能是问题所在

    ResultSet rs = st.executeQuery();
    while (rs.next()) {
        int i = rs.getInt("BOOKED");
        if (rs.wasNull()) {
            return false;
        }
    }

If rs is NULL, then you are calling getInt on a null, causing a NPE

如果 rs 为 NULL,则您在 null 上调用 getInt,导致 NPE

回答by Mani

  1. You are getting IO Exception. Possible reasons for this. Your server is not responding. if its already working check the connection through some tools ( sqlplus/toad/tns) , if everything works you can retry ( if datasource involved re-establish/refresh the state)

  2. Seems you are getting connection in your helper class using getconnection() method. In which you may excepting connection and doing some operation. You are getting Null Pointer exception at the getConnection method. You have to show us to give what is cause for Nullpointer.

  3. Finaly, you can simply write query to return true/false if any one of the seat not booked instead of iterating all the results and stop when its found not booked.

  1. 您收到 IO 异常。可能的原因。您的服务器没有响应。如果它已经在工作,通过一些工具(sqlplus/toad/tns)检查连接,如果一切正常,你可以重试(如果数据源涉及重新建立/刷新状态)

  2. 似乎您正在使用 getconnection() 方法在您的助手类中获得连接。您可以在其中排除连接并进行一些操作。您在 getConnection 方法中收到 Null Pointer 异常。您必须向我们展示导致 Nullpointer 的原因。

  3. 最后,您可以简单地编写查询以在未预订任何一个座位时返回真/假,而不是迭代所有结果并在发现未预订时停止。