IO错误:从读取调用中减1 [英] IO Error: Got minus one from a read call

查看:132
本文介绍了IO错误:从读取调用中减1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用完了时间和想法。我需要使用Java和一个Oracle数据库进行预订整个飞机的模拟。



我们有一些如何做的指示和预期,但我们



数据库如下所示:





我们需要有一个不断运行的线程池来模拟10个同时用户试图预订飞机。当线程结束时,另一个线程代替它。一些线程必须立即(随机机会)既保留和书。其他人会预订,然后决定一秒钟,如果他们想要预订或不预订。如果他们决定等待,然后预订另一个线程可能在此期间保留,这意味着线程失去该预留并将终止。



现在这里有奇怪的部分:


  1. 然后告诉我标题的错误。

  2. 然后显示我到HelperClass.java行,
  3.   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:检查剩余的可用席位
    long id = r.nextLong();
    Future< Integer> submit = pool.submit(new UserThread(id));
    results.add(submit);
    threadsStarted ++;
    id ++;
    } while(!HelperClass.isAllBooked(CR9));

    pool.shutdown();

    来自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;
    }

    不要一步步填写代码,我只是停在这里将提供如果你需要更多的看到这个代码中的问题。请注意,这是原型代码,不应该是令人惊讶的安全或遵循每个代码道德在地球上。



    满栈:

      [HELPER CLASS] SQL ERROR:IO错误:从读取调用中减去一个
    线程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错误:从读取调用中获得减1
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)$ b $ < init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection。< init>(T4CConnection.java:385)
    at oracle.jdbc.driver.PhysicalConnection。 oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    在oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    在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)
    在dbassignment4.UserThread.call(UserThread.java:35)
    在dbassignment4.UserThread.call(UserThread.java:21)
    在java。 util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334)
    在java.util.concurrent.FutureTask.run(FutureTask.java:166)
    在java.util.concurrent.ThreadPoolExecutor。 runWorker(ThreadPoolExecutor.java:1110)
    在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)
    在java.lang.Thread.run(Thread.java:722)
    原因:oracle.net.ns.NetException:从读取调用
    减去一个在oracle.net.ns.Packet.receive(Packet.java:314)
    在oracle。 net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:153)
    在oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
    在oracle.jdbc.driver.T4CConnection.connect( T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ...另外15个

    BUILD STOPPED(总时间: 36秒)


    解决方案


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


    2. 似乎你正在使用getconnection()方法在你的帮助类中获得连接。你可以在其中除了连接和做一些操作。你在getConnection方法中得到Null Pointer异常。


    3. Finaly,你可以简单地写入查询返回true / false如果任何一个座位不是预订而不是迭代所有结果,并在找不到预订时停止。



    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.

    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:

    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.

    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).

    From 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

    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)
    

    解决方案

    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.

    这篇关于IO错误:从读取调用中减1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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