在PostgreSQL中更新数据后不要更改jsp上的数据 [英] Don't changed data on jsp after update data in postgresql

查看:63
本文介绍了在PostgreSQL中更新数据后不要更改jsp上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从db和servlet获取数据的类,用于将该数据发送到jsp.如果我在表中插入或删除行(使用pgAdmin),则更新了jsp上的数据(带有新数据),但是如果我更新了表中的现有日期,则不会在jsp上更新(仅在重新启动glassfish之后). 用于ORM的类:

I have class to get data from db and servlet for send this data to jsp. If i insert or delete row in the table (using pgAdmin), data on jsp is updated (with new data), but if i update existing date in table, it's not updated on the jsp (only after restart glassfish). Class using for 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;
}
}

servlet:

    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方法:

Method of Heading_processes for get data:

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的片段:

And fragment of 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>

UPD: 添加pesistance.xml:

UPD: Add 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>

推荐答案

这很可能是缓存问题.

This is most likely a caching issue.

请参阅以下文档:

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

默认情况下,EclipseLink使用共享对象缓存,该缓存缓存一个 为持久性单元读取并持久化的所有对象的子集.这 EclipseLink共享缓存不同于本地EntityManager缓存. 共享缓存在持久性单元的持续时间内存在 (EntityManagerFactory或服务器),并且由所有EntityManager共享 以及持久性单元的用户.本地EntityManager缓存为 不共享,并且仅在EntityManager期间存在,或者 交易.

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.

共享缓存的好处是,一旦读取了对象, 如果使用find操作再次读取它,则数据库不会 需要访问.同样,如果通过任何查询读取对象,它 不需要重建,并且它的关系也不需要 重新获取.

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.

共享缓存的局限性在于,如果数据库被更改 直接通过JDBC,或通过另一个应用程序或服务器, 共享缓存中的对象将过时.

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配置中添加以下内容并查看问题是否消失来快速验证这一点:

You can quickly verify this by adding the following to your JPA config and seeing if the problem goes away:

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

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

Whether or not you want to disable the cache permanently depends on your use case i.e. will other applications be updating these entities in the real world.

这篇关于在PostgreSQL中更新数据后不要更改jsp上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆