JDBC Statement Order
I am working on a legacy jsp web application that uses JDBC and inline SQL Statements.
We are having an issue with incorrect data in the database and I am wondering if it is possi开发者_StackOverflow社区ble for two statements to be executed in a different order than the order that they are specified in the code. i.e. if we have two update statements that are run like the following example.
String sql1 = "update table set x = y";
String sql2 = "update table set z = a";
Statement statement = conn.createStatement();
db_wrapper.runSQL(sql1,statement);
db_wrapper.runSQL(sql2,statement);
Is it possible that under heavy load the second sql statement will be run before the first one by the database.
The install we are mostly having the problem with runs on an oracle database.
Also the code currently does not use transactions.
No, that thread will call Oracle driver in that order, there is no explicit reordering of method calls, nor reordering or queries in JDBC.
However :
- Are you sure your code is not buffering calls to the database and eventually executing it in different order or sending it to different connections? If it sends queries to different connections, it could happen that one of those connections, executing the second statement, terminate before the first one, and the first one fails.
- Are you checking the return value of your calls? Or the exception? Could it be that the first query simply fails for whatever reason which is not checked by your code? Maybe catch'd, logged somewhere obscure (like system out) and never taken seriously?
- The driver may act in strange way. While I don't know which version of Oracle you are working on, JDBC drivers are rather free to do what they want.
- Under heavy load, more than one thread will be executing the JSP, so unless the code is properly synchronized or the query properly scoped, a number of strange things could happen.
The data should appear in the order you are committing your update executions.
精彩评论