• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

My JSP still shows old data

Attached is my JSP. It displays the number of records retrieved from database (DB). When I manually add or delete records in the DB, and then when I refresh the page, the number displayed on the JSP is still the same. When I update any other stuff on the JSP, refresh the project in MyEclipse, and then refresh the page in the web browser, the changes are reflected but the number of records still show the old one.

If I restart Tomcat and then refresh the page, then the number of records displayed is correct.

Attached are also the Testtable.hbm.xml and hibernate.cfg.xml.

May I know how to make my JSP retrieve the correct number of records everytime I refresh the page? Thanks!
<%@ page import="com.*" %>

<%
	TesttableDAO tttDAO = new TesttableDAO();
	Object[] allRecords = tttDAO.findAll().toArray();
%>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
	<%=allRecords.length%> records found.
  </body>
</html>

Open in new window

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/testdb
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">12qwaszx</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="myeclipse.connection.profile">
			MySQL Connector/J
		</property>
		<mapping resource="com/Testtable.hbm.xml" />

	</session-factory>

</hibernate-configuration>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.Testtable" table="testtable" catalog="testdb">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="90" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" length="90" />
        </property>
    </class>
</hibernate-mapping>

Open in new window

0
killdurst
Asked:
killdurst
  • 2
  • 2
1 Solution
 
cmalakarCommented:
make sure, you always use a new transaction in your findAll() with hibernate.
0
 
killdurstAuthor Commented:
Below is the findAll() method that MyEclipse generated for me.

I'm not sure how to modify that to include the Transaction object, so I modified the code in my JSP as below.

After the modifications, whenever I refresh the JSP, it will retrieve the latest data, so basically my problem has been solved already.

But is this the recommended way of doing things? How do I modify my findAll() method to include the Transaction object?

In all of my previous Hibernate projects, I only need to use the Transaction object whenever I add, edit or delete records only. I didn't need to modify the findAll() methods that MyEclipse generated for me. See below on how I use the Transaction object to add/edit/delete.
public List findAll() {
		log.debug("finding all Testtable instances");
		try {
			String queryString = "from Testtable";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

Open in new window

<%@ page import="com.*" %>
<%@ page import="org.hibernate.*" %>

<%
	TesttableDAO tttDAO = new TesttableDAO();
	Transaction tx = tttDAO.getSession().beginTransaction();
	Object[] allRecords = tttDAO.findAll().toArray();
	tx.commit();
	tttDAO.getSession().close();
%>

Open in new window

Transaction tx = xxxDAO.getSession().beginTransaction();
xxxDAO.save(thisxxx);
tx.commit();
xxxDAO.getSession().close();

Open in new window

0
 
cmalakarCommented:
>> How do I modify my findAll() method to include the Transaction object?

shown below

>>In all of my previous Hibernate projects, I only need to use the Transaction object whenever I add, edit or delete records only.

Because you are updating the database from external to hibernate, and if hibernate has to read it always from db, (and not from cache), you must use a new transaction every time.
public List findAll() {
		log.debug("finding all Testtable instances");
                List list = null;
		try {
			String queryString = "from Testtable";
                        Session session = getSession();
                        Transaction tx = session.beginTransaction();
			Query queryObject = session.createQuery(queryString);
			list queryObject.list();
                        tx.commit();
                        return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

Open in new window

0
 
killdurstAuthor Commented:
Outstanding answer. Very fast response.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now