本文介绍了更新后,Spring MVC将无法显示正确的数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,将 MySQL 数据库中的值放入 ModelAndView对象

In my controller, put a value from a MySQL database into a ModelAndView object

有一个单独的程序更新表, MVC 应该抓取该值,因此没有表格来更新该表。

There is a separate program that updates the table and the MVC is supposed to grab that value so there are no forms to update that table.

表格更新时,以及当我点击浏览器刷新时,值不会更新这页纸。

When the table is updated, and when I hit refresh on the browser, the values will not update on the page.

控制器

@SuppressWarnings("unchecked")
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
public ModelAndView defaultPage(@ModelAttribute("user") User user) {
    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
            .getContext().getAuthentication().getAuthorities();
    ModelAndView view = new ModelAndView("/hello");
    // Redirects to admin page if user has admin role
    if (authorities.toString().contains("ROLE_ADMIN")) {
        return new ModelAndView("redirect:/admin");
    }
    /////
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String userName = auth.getName();
    User userInfo = userDAO.getUserInfo(userName);
    System.out.println("Here's the thing " + userInfo.getLastname() + " " + userInfo.getUserDetails());
    Details details = userDAO.getDetailsInfo(userInfo.getUserDetails().getLastname(),
            userInfo.getUserDetails().getPostcode());
    Plugs plugs = userDAO.getPlugInfo(details.getMacAddress()); 
    String json = plugs.getJson();
    JSONObject obj = new JSONObject(json); //this is the value that is not updating
    String name = obj.getJSONObject("meta").getJSONObject("locate").getString("value");
    System.out.println(name); 
    view.addObject("json", obj);
    return view;
}

我知道这很受鄙视,但我把这个值放在 Javascript。

I know this is pretty looked down upon, but I putting that value in Javascript.

喜欢这样:

<c:set var="json" value="${json}"/> 

var __data__ = ${json};

为什么MVC在数据库更新时不会显示正确的值?

Why won't the MVC display the correct value when the database is updated?

我希望它在刷新时更新


推荐答案

绝对 PersistenceContextType.EXTENDED 范围是您确定问题的根本原因。原因如下:

Definitely PersistenceContextType.EXTENDED scope is the root cause for your problem as you identified. The reason below:

使用 PersistenceContextType.TRANSACTION 范围,Spring框架负责管理注入的生命周期 entitymanager TRANSACTION 范围的生命周期与基础事务相关联,并且在提交/回滚事务时 entityManager 由spring框架关闭。

With PersistenceContextType.TRANSACTION scope, Spring framework takes responsibility of managing the life cycle of the injected the entitymanager. The life of TRANSACTION scope is tied to the underlying transaction and upon commit/rollback of the transaction the entityManager is closed by the spring framework.

但是使用 PersistenceContextType.EXTENDED 范围,Spring框架只负责注入 entitymanager EXTENDED 作用域实体管理器的生命周期与用户事务无关,它可以跨越多个事务。完成后,由应用程序/用户明确关闭 entityManager 。如果不是它将永远保持打开(或直到Spring容器关闭)。

But with PersistenceContextType.EXTENDED scope, Spring framework takes responsibility of only injecting the entitymanager. The life cycle of EXTENDED scoped entity manager is not tied to the user transaction and it can span multiple transactions. It is up to the application/user to explicitly close the entityManager once we are done with it. If not it will remain open forever (or till Spring container is closed).

我在你的 userDAO 中假设你没有显式关闭 entityManager 。而且'userDAO'可能是一个单身人士。因此,只有一次注入的 entityManager 被用于多个调用(或http请求)

I presume in your userDAO you are not closing entityManager explicitly. And also 'userDAO' could be a singleton. So the same entityManager that got injected only once is being used across multiple calls (or http requests)

这个 entityManager'永远保持打开状态,因此当您尝试访问任何对象(User / Plugin / UserDetails) entityManager`检查其第一级缓存时,它将检查其第一级它在其中找到的缓存(第一次加载)并从第一级缓存(过时)返回此对象,而不是在数据库中找到数据。

With this the entityManager' remains open forever and so when you try to access any object (User/Plugin/UserDetails) theentityManager` checks its first level cache, it will check its first level cache which it finds in it (that got loaded for the first time) and returns this object form its first level cache (which is stale) instead of hitting the database for the data.

显然,使用 TRANSACTION 范围,只要事务完成(或方法退出),Spring框架就会关闭 entityManager )。这导致为每个请求创建一个 entityManager (在您的情况下是Web请求)命中具有更新数据的数据库。

And obviously with TRANSACTION scope the entityManager is closed by the Spring framework whenever transaction is completed (or method exits). This results in creating an entityManager for each request (in your case web request) hits the the database which has the updated data.

查看此链接是否有帮助。

See if this link helps. http://forum.spring.io/forum/other-spring-related/ejb/18445-injection-of-persistencecontext-with-persistencecontexttype-extended

这篇关于更新后,Spring MVC将无法显示正确的数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:46