Hi people...
I have implemented an application which it parses an rss online and then it saves it into oracle DB through Hibernate. I am facind a problem which i cannot explain. In the first time the user is entering the rss url to a jsp then the rss is parsed the wanted values are saved into the DB and then displayed back through a jsp again. With not ending the application and going back to re-enter a new url the application throughs an exception before commiting the transaction at the Hibernate part.
The code to the hibernate has as follows...
Code:
try
{
System.out.println("in the beggining");
session = sf.openSession();
trans = session.beginTransaction();
System.out.println("in the middle");
Rss rcv = new Rss();
Item riv = new Item();
riv.setRss(rcv);
System.out.println("be4 saving");
session.save(rcv);
System.out.println("saved rcv");
session.save(riv);
System.out.println("saved riv");
System.out.println("The trans is : "+trans.wasCommitted());
session.update(riv);
trans.commit();
System.out.println("after commit");
System.out.println("THE SESSION: "+session.isOpen());
session.close();
System.out.println("THE SESSION: "+session.isOpen());
}
catch(Exception exc)
{
try{if(trans != null) trans.rollback();}catch(HibernateException h){System.out.println("MMMPPPOOOXXXAAA "+ h);}
exc.printStackTrace();
}
I debug and i came to the above conclution, failing at commit. I cant explain why because it works in the beggining. I also find out that if i dont redirect for displaying the results i can re-enter values into the DB without the exception occuring. The exception is the following...
net.sf.hibernate.HibernateException: identifier of an instance of com.persistence.Item altered from 2190 to 2191
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2631)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2454)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2447)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2249)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2228)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.HibernateFactory.saveValues(HibernateFactory.java:104)
at com.persistence.Item.setMakeCon(Item.java:85)
at com.RssParser.endElement(RssParser.java:147)
at oracle.xml.parser.v2.NonValidatingParser.parseElement(NonValidatingParser.java:1310)
at oracle.xml.parser.v2.ValidatingParser.parseRootElement(ValidatingParser.java:144)
at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:295)
at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:201)
at oracle.xml.jaxp.JXSAXParser.parse(JXSAXParser.java:292)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:255)
at com.RssParser.connect(RssParser.java:227)
at com.ConfigServlet.doPost(ConfigServlet.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:719)
at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:376)
at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:870)
at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:451)
at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:218)
at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:119)
at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
at java.lang.Thread.run(Thread.java:595)
and the code for the JSP page which displays the results is:
Code:
<%@ page contentType="text/html; charset=utf-8" language="java"
import="com.persistence.*,net.sf.hibernate.*, com.*, java.util.*" %>
<%
Session ses = null;
Transaction myTransaction = null;
SessionFactory sf = HibernateFactory.getSessionFactory();
System.out.println("THE SESSION: "+sf.toString());
ses = sf.openSession();
try
{
//getSessionFactory();
System.out.println("THE SESSION: "+ses.isOpen());
myTransaction = ses.beginTransaction();
Criteria query =
ses.createCriteria(Rss.class);
myTransaction.commit();
} catch (Exception e) {
//error = e.getMessage(); e.printStackTrace();
System.out.println("THIS FAIL: "+ e);
try{ myTransaction.rollback(); }
catch (Exception e2) {;}
}
%>
<HTML>
<HEAD>
<TITLE>List Teams</TITLE>
</HEAD>
<BODY>
<%
//Query q =HibernateFactory.session.createQuery("Select Rss from Rss as Rss");
Query q =ses.createQuery("Select Rss from Rss as Rss");
try{
//ses.close();
for (Iterator it = q.iterate(); it.hasNext();) {
Rss rsv = (Rss) it.next(); %>
<table border="1">
<tr>
<td> <strong> Channel Title </strong></td>
<td> <strong> Channel URL </strong></td>
<td> <strong> Main Link </strong></td>
<td> <strong> Last Date Built </strong></td>
<td> <strong> Item Title </strong></td>
<td> <strong> Item Link </strong></td>
<td> <strong> Item Description </strong></td>
</tr>
<tr>
<td> <%=rsv.getChannelTitle()%> </td>
<td> <%= rsv.getChannelUrl()%> </td>
<td> <%= rsv.getMainLink()%> </td>
<td> <%= rsv.getLastDate()%> </td>
<% Iterator t = rsv.getItem().iterator();
while (t.hasNext()) {
Item riv =(Item)t.next();
%>
<td> <%= riv.getTitle() %> </td>
<td> <%= riv.getItemLink()%> </td>
<td> <%= riv.getItemDescription() %></td>
</tr>
</table>
<%}
}
} catch(Exception e){
System.out.println("AOUTS: "+ e);
}
finally
{
try{ses.close();}
catch (Exception e) {
System.out.println("AOUTS: "+ e);}
}
%>
<p align="right"><a href="RssList.jsp">Add Another RSS.XML File </a> </p>
</BODY>
</HTML>
The following is what is being used to call the saveValues(). It is located inside the Item class The string is being assigned by the RssParser().
Code:
public void setMakeCon(String verify){
this.verify = verify;
if (verify != null) {
HibernateFactory.saveValues();
}else if(verify == null){
System.out.println("The VERIFY FROM item was NULL");
}
}
Do you have any ideas why this happens? I am desperate for some help.
Thx in advance