-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Quickstart Servlet Problem
PostPosted: Tue Aug 03, 2004 2:10 am 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
I have a problem with compiling the servlet I created from the quickstart.

Here is my simple servlet code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/** Simple servlet used to test server.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* &copy; 2003 Marty Hall; may be freely used or adapted.
*/

public class HelloServlet1a extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");

try {
Session session = HibernateUtil.currentSession();

Transaction tx= session.beginTransaction();

Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);

session.save(princess);
tx.commit();

HibernateUtil.closeSession();

}

catch (HibernateException e1) {
e1.printStackTrace();
}
}
}

The rest of the xml files and everything else I copied directly from the quickstart.

Here is part of the error message that is seen when I do: http://localhost:8080/quickstart/servlet/HelloServlet1a

exception

javax.servlet.ServletException: Invoker service() exception
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:477)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)


root cause

java.lang.NoClassDefFoundError
HelloServlet1a.doGet(HelloServlet1a.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

Is this a problem with my database pool connections? Any help would be appreciated like identifying the problem and telling how to fix it.

Thank you very much,
Kenneth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 5:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use this in HibernateUtil:
Code:
    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 3:01 pm 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
Okay so I replaced in my HibernateUtil code with this code:

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}


Heh I didn't put the log.error() line but I figured printStackTrace was the same thing.

Now I got the same error message I got before like the exception part is the same. But under the root cause section the first line is different cuz it printed an error in the HibernateUtil code now as you can see, though the rest of the lines are the same:

exception

javax.servlet.ServletException: Invoker service() exception
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:477)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)


root cause

java.lang.ExceptionInInitializerError
HibernateUtil.<clinit>(HibernateUtil.java:14)
HelloServlet1a.doGet(HelloServlet1a.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)



So do you have any idea what the problem is now that there is a new error message in HibernateUtil? I appreciate all help.

Thank you very much,
Kenneth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 3:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You need the root cause for the java.lang.ExceptionInInitializerError - catch and log it in your servlet or replace throw new ExceptionInInitializerError(ex); with throw new ExceptionInInitializerError(ex.getMessage());

PS: I hate that stupid Java 1.4. Exception nesting


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 7:52 pm 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
Okay, here is what I added:

In my servlet HelloServlet1a I added this code on top:

import net.sf.hibernate.exception.NestableException;

I also added a catch for the NestableException at the end:

catch (NestableException ne) {
out.println("NestableException: " + ne.getMessage());
ne.printStackTrace(out);
}


Though the above code I don't think made much of a difference. I also added your suggestion of ex.getMessage() so now my HibernateUtil Code has this:

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex.getMessage());
}
}


When I do my: http://localhost:8080/quickstart/servlet/HelloServlet1a
There is a slight change in the error message for the root cause. It says error reading resource: Cat.hbm.xml . Here is the error message. It looks the same except for that line:

exception

javax.servlet.ServletException: Invoker service() exception
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:477)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)


root cause

java.lang.ExceptionInInitializerError: Error reading resource: Cat.hbm.xml
HibernateUtil.<clinit>(HibernateUtil.java:14)
HelloServlet1a.doGet(HelloServlet1a.java:41)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)



So for reference here is my Cat.hbm.xml code though I copied it from the quickstart:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>

<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>

<property name="sex"/>

<property name="weight"/>

</class>

</hibernate-mapping>


Any more suggestions? If you want me to include my Tomcat/conf/server.xml code or the hibernate.properties file or the hibernate.cfg.xml code as well I could include it in my next reply. I am using MySQL with no password, though I could put one in, but I don't think it makes much of a difference.

Thank you very much,
Kenneth Louie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You will never find the root cause of this issue if you don't log the Throwable in the static initializer properly. This is usually a no-brainer, and I can't tell you why it doesn't work for you.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 3:13 pm 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
It works, at least like that first part of the Cat stuff. I will probably have questions later on too. Thanx for replying


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 8:58 pm 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
Actually I have another question. I read somewhere that in MySQL the default storage engine is MyISAM. I heard that MyISAM does not support transactions. However, when I used my servlet that had the code for Transactions like:
try {
Session session = HibernateUtil.currentSession();

Transaction tx= session.beginTransaction();

Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);

session.save(princess);
tx.commit();

HibernateUtil.closeSession();

}

So was this a fluke my servlet worked? If so, do I need code in my servlet to specify like that I should use the InnoDB storage engine? To change storage engines is all I need to do is to put ALTER Table in my mysql command lines, and I don't have to use any java code in my servlet right?

Thank you,
Kenneth Louie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 11:08 pm 
Newbie

Joined: Tue Aug 03, 2004 1:27 am
Posts: 10
I believe that MySQL 4.0.2d i thnik uses MyISAM as the default. Please correct me if I'm wrong. My above question still applies though and I would appreciate an answer very much.

Thank you,
Kenneth Louie


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.