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

时间:2015-02-25 16:43:38

标签: postgresql jsp servlets jpa eclipselink

我有类从db和servlet获取数据,以便将此数据发送到jsp。如果我在表中插入或删除行(使用pgAdmin),则更新jsp上的数据(使用新数据),但如果我更新表中的现有日期,则不会在jsp上更新(仅在重新启动glassfish之后)。 用于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方法:

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>

UPD: 添加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>

1 个答案:

答案 0 :(得分:1)

这很可能是一个缓存问题。

请参阅以下文档:

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

  

默认情况下,EclipseLink使用缓存a的共享对象缓存   持久性单元读取和保留的所有对象的子集。该   EclipseLink共享缓存与本地EntityManager缓存不同。   共享缓存在持久性单元的持续时间内存在   (EntityManagerFactory或服务器)并由所有EntityManagers共享   和持久性单元的用户。本地EntityManager缓存是   不共享,仅在EntityManager或者持续时间内存在   事务。

     

共享缓存的好处是,一旦读取了一个对象,   如果使用find操作再次读取,则数据库不会   需要被访问。此外,如果通过任何Query读取对象,它   不需要重建,它的关系也不需要   重新提取。

     

共享缓存的限制是,如果数据库已更改   直接通过JDBC,或由另一个应用程序或服务器,   共享缓存中的对象将过时。

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

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

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