首页 > 解决方案 > 可以在 Restful Web 服务中使用 JDBC 保存点吗?

问题描述

我的 Web 应用程序中有一个网格,其中数据是使用 jQuery API 直接从 Web 服务获取的。要求是

  1. 允许用户对其数据进行一些更改
  2. 如果他们想撤消
  3. 最后提交他们的更改

为了实现这一点,我使用了 JDBC 保存点并将它们保存在一个堆栈中,这个堆栈是为了允许撤消操作。我在更新语句之前设置了一个保存点,在撤消时我希望它回滚到最后一个保存点并且提交很简单。

我首先在桌面应用程序上对其进行了测试,它运行良好,但在基于 Web 的应用程序中它不起作用。

数据库连接类

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;
}

我的更新功能

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);

}

如果这里没有保存点语句,那么更新后我可以在我的网页上看到更改的记录,这些记录尚未保存在数据库中,因为自动提交设置为 false。这个保存点语句的问题是网格不显示更改的记录。在我看来,记录似乎没有更新,或者它们自动回滚到这个保存点。

我的逻辑有问题吗?或者这些保存点不适用于restful api?

标签: restjdbcweb-applicationssavepoints

解决方案


有一个愚蠢的错误,我什至没有调查,这条线返回 null 并且没有进一步处理。控制台也没有显示任何异常。

userSavePoints.get(userKey)

我只需要初始化它来解决我的问题。


推荐阅读