I have a Java running process that inserts data into a SqlServer DB table every 3s, using a PreparedStatement executeBatch()
. (Process A)
// Get connection here
con.setAutoCommit(false);
// Create PreparedStatement here
ps = con.prepareStatement(stmt);
// Add to batch with loop
ps.executeBatch();
con.commit();
Also, every 5s another Java process is reading the new inserted data from that same table. (Process B)
// Get connection here
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
// Create PreparedStatement here
ps = con.prepareStatement(stmt);
rs = ps.executeQuery();
rs.setFetchSize(10000);
// While ResultSet has data, add it and return it
The main issue here is:
- Process A is inserting about 300 rows
- Process B sometimes is able to read the data while not all the rows in the batch were persisted (kinda in the middle of the batch), so it would return 80 rows and after another select right away I'll get the whole 300 rows.
Any ideas to what could I be possibly be improved?