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

孤街醉人 提交于 2019-12-12 00:00:36

问题


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 {

    }
    }

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

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

回答1:


This is most likely a caching issue.

See the following documentation:

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.

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.



来源:https://stackoverflow.com/questions/28724671/dont-changed-data-on-jsp-after-update-data-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!