简体   繁体   中英

executeUpdate() in java takes long time to execute

I am using a simple piece of code to update a single row in Oracle DB, the query updates just a single row but still the execution hangs on stmt.executeUpdate().

It does not throw any exception, but the control just hangs there.

I am lost here as the same piece of code worked fine earlier.

  try {
      DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
      conn = DriverManager.getConnection(url,username,pwd);
      stmt = conn.createStatement();
      String sql = "update report set status =" +"'"+NO+"'"+ " where name=" +"'"+ name+"'"+ " and user_id=" +"'"+sub+"'"; 
      System.out.println(sql);          
      stmt.executeUpdate(sql)

You should never create statements by concatenating variables like this. This leaves you open to SQL injection . Furthermore, each statement will be seen by Oracle as a new statement which will need a hard parse, which can lead to performance problems. Instead, use preparedStatement .

However, I think that in your case it's not the cause of your problem. When a simple update hangs, it is almost always one of these reasons:

  1. The row is locked by another transaction.
  2. You are updating a key that is referenced by another table as an unindexed foreign key .
  3. The table has an ON UPDATE trigger that does lots of works.

I'm pretty sure that you're in the first case. Oracle prevents multiple users from updating the same row at the same time, for consistency reasons mostly. This means that a DML statement will wait upon another transaction if the modified row is locked. It will wait until the blocking transaction commits or rollbacks.

I advise you to always lock a row before trying to update it by doing a SELECT ... FOR UPDATE NOWAIT before. This will fail if the row is already locked, and you can exit gracefully by telling the user that this row is currently being locked by another session.

You also have to make sure that no transaction ends in an uncommited state. All sessions should commit or rollback their changes before exiting (don't use autocommit though).

Use JDBC PreparedStatement java docs

  Class.forName("org.apache.derby.jdbc.ClientDriver");
  Connection con = DriverManager.getConnection
  ("jdbc:derby://localhost:1527/testDb","name","pass");
  PreparedStatement updateemp = con.prepareStatement
  ("insert into emp values(?,?,?)");
  updateemp.setInt(1,23);
  updateemp.setString(2,"Roshan");
  updateemp.setString(3, "CEO");
  updateemp.executeUpdate();

I had the same problem you had, I don't know about others but I solved it by simply closing SQLplus! I didn't think that could solve it since some queries worked while sqlplus was running on the console, but I closed it and now every queries works and nothing hangs!

You may want to try this if you have our problem! exit Sqlplus on your console or any other database you are using, then try running your code again!

use PreparedStatement instead

http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/

If you statement then every time it server will check for syntax so it consumes time So go for PreparedStatement

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