Link to home
Start Free TrialLog in
Avatar of skydancer2
skydancer2

asked on

tomcat / hibernate / jndi configuration problem

Hi folks!
I am new to hibernate and have problems with the configuration.
tomcat 5.5.17, hibernate 3.2 and I recieve the error

org.hibernate.HibernateException: No TransactionManagerLookup specified

What could that mean?
Please help!

regards
Albert

ERROR MESSAGE
--------------------------------------------------------------------------------------------------------------------------------------------
20:38:02,296 ERROR [jsp]:253 - Servlet.service() for servlet jsp threw exception
org.hibernate.HibernateException: No TransactionManagerLookup specified
      at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:54)
      at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
      at cms2.user.UserDAO.list(UserDAO.java:93)
      at org.apache.jsp.listUser_jsp._jspService(listUser_jsp.java:65)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
      at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
      at java.lang.Thread.run(Thread.java:595)

context.xml
--------------------------------------------------------------------------------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<Context path="caragent" debug="1" reloadable="true" crossContext="true" docBase='C:\JAVA\work\car-agent'>
   <Resource auth="Container" name="jdbc/db" scope="Shareable" type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/db">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <parameter>
      <name>password</name>
      <value>password</value>
    </parameter>
    <parameter>
      <name>url</name>
      <value>jdbc:mysql://etc</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>com.mysql.jdbc.Driver</value>
    </parameter>
    <parameter>
      <name>username</name>
      <value>username</value>
    </parameter>
  </ResourceParams>
</Context>

web.xml resource-ref entry
--------------------------------------------------------------------------------------------------------------------------------------------
            <resource-ref>
                  <description>mySQL DataSource</description>
                  <res-ref-name>jdbc/db</res-ref-name>
                  <res-type>javax.sql.DataSource</res-type>
                  <res-auth>Container</res-auth>
            </resource-ref>

hibernate.cfg.xml
--------------------------------------------------------------------------------------------------------------------------------------------
<?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="hibernate.connection.datasource">java:comp/env/jdbc/db</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <mapping resource="User.hbm.xml" />

</session-factory>

</hibernate-configuration>


HibernateUtil.java
--------------------------------------------------------------------------------------------------------------------------------------------
package cms2.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
              sessionFactory = new Configuration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


METHOD from the UserDAO
--------------------------------------------------------------------------------------------------------------------------------------------
    public List list(Paging paging) {
        log.debug("Userlist");
        try {
            String queryString = "from User";
            SessionFactory sf = HibernateUtil.getSessionFactory();
            Session session = sf.getCurrentSession();
            Query query = session.createQuery(queryString);
            //Query query = HibernateUtil.getSessionFactory().getCurrentSession().createQuery(queryString);
            query.setMaxResults(paging.getNumRows());
            query.setFirstResult(paging.getNumRows() * paging.getPageOffset());

            return query.list();
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }  


ASKER CERTIFIED SOLUTION
Avatar of reach2piyush
reach2piyush
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skydancer2
skydancer2

ASKER

thanx  reach2piyush!

that was really fast.
Another thing: Am I supposed to close the session?

best
thanx again!

I decided to have my Hibernateutil.class with a private constructor, to enshure the Session Object ist passed only once.
Does that make sense?

regards
Albert

HibernateUtil.java
__________________________________________________________________________________________________
package cms2.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

      private static SessionFactory sessionFactory;
      private static Session session;
      static {
            try {
                  // Create the SessionFactory from hibernate.cfg.xml
                  sessionFactory = new Configuration().configure().buildSessionFactory();
                  session = sessionFactory.openSession();

            } catch (Throwable ex) {
                  System.err.println("Initial SessionFactory creation failed." + ex);
                  throw new ExceptionInInitializerError(ex);
            }
      }
      
      private HibernateUtil(){
      }
      
      public static synchronized Session getSession() {
            if(sessionFactory == null){
                  try {
                        // Create the SessionFactory from hibernate.cfg.xml
                        sessionFactory = new Configuration().configure().buildSessionFactory();
                        session = sessionFactory.openSession();

                  } catch (Throwable ex) {
                        System.err.println("Initial SessionFactory creation failed." + ex);
                        throw new ExceptionInInitializerError(ex);
                  }
            }
            return session;
      }
}
Glad to help :)

For Read Only Scenarios, you can use a single Session object & set the Flush Mode to NEVER, this will give you a better performance.

But for certain Update scenarios, it may cause problems, it will be better to open a new Session & close it along with the transaction.