My question: I’m wondering why the results from a query from a prepared statement should be inconsistent per run.
Warning weird stuff ahead.
I’m on an Oracle database. I’m using the ojdbc6.jar driver. I have a query which returns about 1300 rows (it changes over time because there’s a where clause which depends on sysdate). When I run it in Toad (SQL Developer tool) and Netbeans Services tool (Another interface with the database) it works great. My results are consistant. But when I run the query in java code my results are highly inconsistent. In an effort to try and see how frequent each result count returned I put it into a for loop and iterated 10 times. There is a relatively consistent row count within each run, but not across runs. My output and code are below. Let me know whether any more information is helpful.
When an attempt works (sorta…):
Got real queries (1308)
Got real queries (1308)
Got real queries (1308)
Got real queries (1307)
Got real queries (1307)
Got real queries (1317)
Got real queries (1317)
Got real queries (1317)
Got real queries (1317)
Got real queries (1315)
When an attempt does not work:
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
My code:
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
List<String> realQueries = new ArrayList<>();
String sqlAllRealQueries = "select V.SQL_FULLTEXT" + StringHelper.newline
+ "from v$sql v" + StringHelper.newline
+ "where V.PARSING_SCHEMA_NAME = 'DSS1_PTC'" + StringHelper.newline
+ "and to_number(replace(replace(replace(v.last_load_time,'-',''), '/',''), ':','')) > to_number(to_char(sysdate - (24/24),'yyyymmddHH24mmss'))" + StringHelper.newline
+ "order by V.LAST_LOAD_TIME desc";
try (ResultSet rs = getInstance().executeQuery(sqlAllRealQueries)) {
while (rs.next()) {
realQueries.add(rs.getString(1));
}
}
System.out.println("Got real queries (" + realQueries.size() + ")");
}
System.exit(0);
}
public ResultSet executeQuery(String connection, String query, QueryParameter... params) throws DataException, SQLException {
Connection conn = ConnectionPool.getInstance().get(connection);
PreparedStatement pstmt = conn.prepareStatement(query);
for (int i = 1; i <= params.length; i++) {
//... stuff to fill in the '?'s of the query (in this case, there are none).
}
}
ResultSet rs = pstmt.executeQuery();
ConnectionPool.getInstance().release(conn);
return rs;
}
You have small bug in date format ‘yyyymmddHH24mmss’, should be ‘yyyymmddHH24miss’ (“mi” instead of “mm”)
mm means month, minutes are “mi”.