-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cannot open a simple Session !
PostPosted: Tue Jun 29, 2004 8:33 am 
Newbie

Joined: Tue Jun 29, 2004 7:49 am
Posts: 3
I followed the Hibernate tutorial (with the Cat) but got an error when i open a Session.

Here is the Servlet I use to test Hibernate :


package test;

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

import net.sf.hibernate.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class HibernateServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>hibernateServlet</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");

/* try {
Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/dm");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("insert into CAT values('1', 'Felix', 'M', 10)");
conn.commit();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
*/
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();
}
catch (HibernateException he) {
he.printStackTrace();
}
finally {
try { HibernateUtil.closeSession(); } catch (HibernateException he) { }
}
}

//Clean up resources
public void destroy() {
}
}

As you can see the DataSource has been tested and the Database access is ok. But when I want to use Hibernate through the HibernateUtil.currentSession() call I got the following exception :

StandardWrapperValve[hibernateServlet]: Servlet.service() for servlet hibernateServlet threw exception

javax.servlet.ServletException: Servlet execution threw an exception

javax.servlet.ServletException: Servlet execution threw an exception

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)

at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)

at java.lang.Thread.run(Thread.java:534)

Which I think has nothing to do with Hibernate !
When debugging the HibernateUtil class :


package test;

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

public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException ex) {
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session)session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session)session.get();
session.set(null);
if (s != null)
s.close();
}
}

The static block is executed perfectly but the instruction that throws an exception is the one in red ! But what is strange is that I don't manage to catch the HibernateException not even the Exception (I tested it) !

Here is my hibernate.cfg.xml file :


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/dm</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>

and the output of Hibernate :

- Hibernate 2.1.4

- hibernate.properties not found

- using CGLIB reflection optimizer

- configuring from resource: /hibernate.cfg.xml

- Configuration resource: /hibernate.cfg.xml

- Mapping resource: Cat.hbm.xml

- Mapping class: test.Cat -> CAT

- Configured SessionFactory: null

- processing one-to-many association mappings

- processing one-to-one association property references

- processing foreign key constraints

- Using dialect: net.sf.hibernate.dialect.Oracle9Dialect

- Use outer join fetching: true

- JNDI InitialContext properties:{}

- Using datasource: java:comp/env/jdbc/dm

- No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)

- Use scrollable result sets: true

- Use JDBC3 getGeneratedKeys(): false

- Optimize cache for minimal puts: false

- Query language substitutions: {}

- cache provider: net.sf.ehcache.hibernate.Provider

- instantiating and configuring caches

- building session factory

- no JNDI name configured

I'm using the following tools :
Hibernate 2.1.4
JDK 1.4.2
Oracle 9i
Tomcat 4.0.6
JBuilderX

I would appreciate the help of the Hibernate community members


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Code:
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException ex) {
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}


Replace the "catch (HibernateException ex)" with "catch (Throwable ex)" and log the exception. Then, throw a ExceptionInInitializerError() instead of a RuntimeException. The code in the current tutorial (you copied that) swallows exceptions.
[/code]

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 9:21 am 
Newbie

Joined: Tue Jun 29, 2004 7:49 am
Posts: 3
Thanks for your quick response but after replacing what you told me, the error is still there. Here is my code :

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


When debbuging, the call to the line in red works fine and do not throw any exception, it is the call to sessionFactory.openSession() that throws "something" that I cannot catch in my call :

Code:
    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();
    }
    catch (Exception he) {
      System.out.println("Hello");
      he.printStackTrace();
    }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 9:24 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You have a nested exception (probably a missing library or something) that stops the static initializer. You haven't shown this exception yet, only the Servlet exception wrapper. The code you have is fine.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 10:53 am 
Newbie

Joined: Tue Jun 29, 2004 7:49 am
Posts: 3
Thanks a lot, I finnaly made it work !
I was missing only one library : jta.jar, but in your lib/Readme.txt it is written that this file is needed only on standalone applications. And since I'm running Tomcat I hadn't included it.
The reason is maybe that I'm using the Tomcat version bundled with JBuilderX : Tomcat 4.0.6 LE which comes witout without jta.jar !
One last question :
Do I need to copy this jta.jar in my WEB-INF/lib directory when going on my production server (WebLogic 8.1) or is this a jar that should already be in the server classpath ?

Again thank you for all and of course I'll send you a congratulation message if Hibernate suits my needs and more ;-) which I'm almost sure !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 11:02 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Weblogic has JTA, I'm sure.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.