0

There is a grid in my web application where data is obtained directly from a webservice using jQuery API. Requirement is to

  1. allow users to do some changes to their data
  2. undo if they want to
  3. finally commit their changes

To implement this I used JDBC Savepoints and keeping them in a stack, this stack is to allow undo operations. I am setting a savepoint before update statement, on undo I want it to rollback to last savepoint and commit is simple.

I have tested it on a desktop application first and it worked fine but here in Web based application it is not working.

Database connection class

private static Map<String, Connection> userConnections;

public static Connection getConnection(configsJson) {
  ... //getdata from json
  if (userConnections == null) {
    userConnections = new HashMap<>();
  } else {
    userConnection = userConnections.get(userKey);
  }

  if (userConnection == null) {
    userConnection = DriverManager.getConnection("jdbc:oracle:thin:@" + sourceIP + ":" + sourcePort + ":" + sourceInstance, sourceUser, sourcePass);
    userConnection.setAutoCommit(false);
    userConnections.put(userKey, userConnection);
  }
  return userConnection;
}

My update function

private static Map<String, Stack<Savepoint>> userSavePoints = new HashMap<>();

@ResponseBody
@RequestMapping(value = "/webservice/updateRecord", method = RequestMethod.POST)
public int updateRecord(HttpServletRequest request, @RequestParam String configs) throws SQLException, JSONException, ParseException {
    Connection con = DBService.getConnection(configJson);
    ...
    userSavePoints.get(userKey).push(con.setSavepoint());
    return statement.executeUpdate(sql);

}

If there is no savepoint statement here then after update I can see the changed records on my web page, these records are not yet saved in database since autocommit is set to false. Issue is with this savepoint statement the grid doesn't show the changed records. Which to me it seems as if the records haven't updated or they auto rolled back to this savepoint.

Is there something wrong with my logic? or these savepoints doesn't work with restful api?

Muneeb Mirza
  • 810
  • 1
  • 17
  • 35
  • 1
    It might work, but it doesn't scale well, nor does it work well when you have multiple server instances for your application (the session would need to 'stick' to the specific server that handled the initial request, and failover is a pain). If this needs to scale well, then consider looking for alternative means for achieving undo. – Mark Rotteveel Jun 13 '19 at 12:55
  • @MarkRotteveel, thanks for identifying this issue but in our scenario this webservice will be a very small application which will reside inside client's system and this will never scale. It will remain a small application. Thanks :) – Muneeb Mirza Jun 13 '19 at 13:06

1 Answers1

0

There was a dumb mistake that I didn't even look into, this line was returning null and was not proceeding further. The console didn't display any exception as well.

userSavePoints.get(userKey)

I just had to initialize this to solve my problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Muneeb Mirza
  • 810
  • 1
  • 17
  • 35