简体   繁体   中英

Oracle SQL randomly not fetch data on continous hit on the same table

I am working on Oracle sql queries with java code in Oracle ADF. I have written piece of code as below

public String getValue(Connection cn, String val1) throws SQLException {
    String sql = "select price FROM table1 WHERE val1= ?";
    PreparedStatement ps = cn.prepareStatement(sql);
    ps.setString(1, val1);
    ResultSet rs = ps.executeQuery();
    try
    {
        
        if(rs.next()){
            return rs.getString("price");
        }
        else{
            System.out.println("price is null");
        }
    }catch(Exception e){
        e.printStackTrace();
        throw e;
    }
    finally{
        rs.close();
        ps.close();
        
        }
    return null;
    }

I am hitting this method more than 100000 times. in these many times, val1 is repeating. so for same val1, in some of the cases, its price is fetched, but in some cases, its not.

 val1 is in the method --->1
 price 3
 val1 is in the method --->2
 price 5
 val1 is in the method --->1
 price 3
 val1 is in the method --->1
 price null ---> here in previous hit, for 1 - it has fetched price 3, but in this it has fetched null for the same value 1.
 val1 is in the method --->1
 price 3

I have tried other approaches like,

1.  String sql = "select price FROM table1 WHERE val1='"+val1+"'";
2. I have checked that all the resources are getting closed or not, but could not find any issue.

I request you to help in this matter.

Thanks & Regards, Shaili.

Try apply synchronized keyword in your get value method. Another reason maybe in database have 2 row with value val1 = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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