在 postgresql 中更新数据后不要更改 jsp 上的数据

Don't changed data on jsp after update data in postgresql

我有 class 从 db 和 servlet 获取数据以将此数据发送到 jsp。如果我在 table 中插入或删除行(使用 pgAdmin),jsp 上的数据会更新(使用新数据),但如果我更新 table 中的现有日期,则不会更新jsp(仅在重新启动 glassfish 后)。 Class 用于 ORM:

package db_classes;
@Entity
public class heading {
private Integer id;
private String name;
private Long themeCount;
private Collection<topic> topicsById;

@Id
@Column(name = "id")
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

@Basic
@Column(name = "name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Basic
@Column(name = "theme_count")
public Long getThemeCount() {
    return themeCount;
}

public void setThemeCount(Long themeCount) {
    this.themeCount = themeCount;
}

@OneToMany(mappedBy = "headingByIdHeading")
public Collection<topic> getTopicsById() {
    return topicsById;
}

public void setTopicsById(Collection<topic> topicsById) {
    this.topicsById = topicsById;
}
}

小服务程序:

    package controllers;

/**
 * Created by Otani on 25.02.2015.
 */
@WebServlet(name = "Heading_parser")
@Stateful
public class Heading_parser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Heading_processes heading_processes = new Heading_processes();
        getServletContext().setAttribute("headings",heading_processes.getAllHeading());
   request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
    }

    @Override
    public void init() throws ServletException {

    }
    }

Heading_processes获取数据的方法:

public List<heading> getAllHeading() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        try {
            Query query = entityManager.createQuery("SELECT h FROM heading h");
            entityManager.getTransaction().commit();
            return query.getResultList();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        } finally {
            entityManager.close();
        }
        return null;
    }

和index.jsp的片段:

<table class="table-border">
    <tbody>

    <c:forEach var = "heading" items = "${headings}">
        <tr>
            <td class="msg-img"><img src="image/message.png" width="32" height="32" alt="theme"></td>
            <td><a href="showtopic.jsp?topic?id=${heading.id}" title=${heading.name}>${heading.name}</a></td>
            <td class="count">${heading.themeCount} Тем <br> Сообщений:</td>
        </tr>
    </c:forEach>

    </tbody>
</table>

更新: 添加 pesistance.xml:

 <persistence-unit name="forum">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>db_classes.heading</class>
        <class>db_classes.message</class>
        <class>db_classes.topic</class>
        <class>db_classes.uncensoredWords</class>
        <class>db_classes.users</class>
        <properties>
            <property name="eclipselink.jdbc.url" value="jdbc:postgresql://localhost:5432/forum"/>
            <property name="eclipselink.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="eclipselink.jdbc.user" value="****"/>
            <property name="eclipselink.jdbc.password" value="*****"/>
        </properties>
    </persistence-unit>
</persistence>

这很可能是缓存问题。

请参阅以下文档:

https://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching

By default EclipseLink uses a shared object cache, that caches a subset of all objects read and persisted for the persistence unit. The EclipseLink shared cache differs from the local EntityManager cache. The shared cache exists for the duration of the persistence unit (EntityManagerFactory, or server) and is shared by all EntityManagers and users of the persistence unit. The local EntityManager cache is not shared, and only exists for the duration of the EntityManager or transaction.

The benefit of the shared cache, is that once an object has been read, if it is read again using the find operation, the database does not need to be accessed. Also if the object is read through any Query, it will not need to be rebuilt, and its relationships will not need to be re-fetched.

The limitation of the shared cache, is that if the database is changed directly through JDBC, or by another application or server, the objects in the shared cache will be stale.

您可以通过将以下内容添加到您的 JPA 配置并查看问题是否消失来快速验证这一点:

<property name="eclipselink.cache.shared.default" value="false"/>

是否要永久禁用缓存取决于您的用例,即其他应用程序是否会在现实世界中更新这些实体。