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.HibernateExc eption: 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.HibernateExc eption: No TransactionManagerLookup specified
at org.hibernate.context.JTAS essionCont ext.curren tSession(J TASessionC ontext.jav a:54)
at org.hibernate.impl.Session FactoryImp l.getCurre ntSession( SessionFac toryImpl.j ava:542)
at cms2.user.UserDAO.list(Use rDAO.java: 93)
at org.apache.jsp.listUser_js p._jspServ ice(listUs er_jsp.jav a:65)
at org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:97)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
at org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 32)
at org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:314)
at org.apache.jasper.servlet. JspServlet .service(J spServlet. java:264)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 802)
at org.apache.catalina.core.A pplication FilterChai n.internal DoFilter(A pplication FilterChai n.java:252 )
at org.apache.catalina.core.A pplication FilterChai n.doFilter (Applicati onFilterCh ain.java:1 73)
at org.apache.catalina.core.S tandardWra pperValve. invoke(Sta ndardWrapp erValve.ja va:213)
at org.apache.catalina.core.S tandardCon textValve. invoke(Sta ndardConte xtValve.ja va:178)
at org.apache.catalina.core.S tandardHos tValve.inv oke(Standa rdHostValv e.java:126 )
at org.apache.catalina.valves .ErrorRepo rtValve.in voke(Error ReportValv e.java:105 )
at org.apache.catalina.core.S tandardEng ineValve.i nvoke(Stan dardEngine Valve.java :107)
at org.apache.catalina.connec tor.Coyote Adapter.se rvice(Coyo teAdapter. java:148)
at org.apache.coyote.http11.H ttp11Proce ssor.proce ss(Http11P rocessor.j ava:869)
at org.apache.coyote.http11.H ttp11BaseP rotocol$Ht tp11Connec tionHandle r.processC onnection( Http11Base Protocol.j ava:664)
at org.apache.tomcat.util.net .PoolTcpEn dpoint.pro cessSocket (PoolTcpEn dpoint.jav a:527)
at org.apache.tomcat.util.net .LeaderFol lowerWorke rThread.ru nIt(Leader FollowerWo rkerThread .java:80)
at org.apache.tomcat.util.thr eads.Threa dPool$Cont rolRunnabl e.run(Thre adPool.jav a:684)
at java.lang.Thread.run(Threa d.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.Basic DataSource Factory</v alue>
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://etc</v alue>
</parameter>
<parameter>
<name>driverClassName</nam e>
<value>com.mysql.jdbc.Driv er</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.DataSo urce</res- type>
<res-auth>Container</res-a uth>
</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 .datasourc e">java:co mp/env/jdb c/db</prop erty>
<property name="dialect">org.hiberna te.dialect .MySQLDial ect</prope rty>
<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( ).buildSes sionFactor y();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initia l SessionFactory creation failed." + ex);
throw new ExceptionInInitializerErro r(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.getSessionFa ctory();
Session session = sf.getCurrentSession();
Query query = session.createQuery(queryS tring);
//Query query = HibernateUtil.getSessionFa ctory().ge tCurrentSe ssion().cr eateQuery( queryStrin g);
query.setMaxResults(paging .getNumRow s());
query.setFirstResult(pagin g.getNumRo ws() * paging.getPageOffset());
return query.list();
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
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.HibernateExc
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.HibernateExc
at org.hibernate.context.JTAS
at org.hibernate.impl.Session
at cms2.user.UserDAO.list(Use
at org.apache.jsp.listUser_js
at org.apache.jasper.runtime.
at javax.servlet.http.HttpSer
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.catalina.core.A
at org.apache.catalina.core.A
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.connec
at org.apache.coyote.http11.H
at org.apache.coyote.http11.H
at org.apache.tomcat.util.net
at org.apache.tomcat.util.net
at org.apache.tomcat.util.thr
at java.lang.Thread.run(Threa
context.xml
--------------------------
<?xml version='1.0' encoding='utf-8'?>
<Context path="caragent" debug="1" reloadable="true" crossContext="true" docBase='C:\JAVA\work\car-
<Resource auth="Container" name="jdbc/db" scope="Shareable" type="javax.sql.DataSource
<ResourceParams name="jdbc/db">
<parameter>
<name>factory</name>
<value>org.apache.commons.
</parameter>
<parameter>
<name>password</name>
<value>password</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://etc</v
</parameter>
<parameter>
<name>driverClassName</nam
<value>com.mysql.jdbc.Driv
</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
<res-type>javax.sql.DataSo
<res-auth>Container</res-a
</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
<property name="dialect">org.hiberna
<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(
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initia
throw new ExceptionInInitializerErro
}
}
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.getSessionFa
Session session = sf.getCurrentSession();
Query query = session.createQuery(queryS
//Query query = HibernateUtil.getSessionFa
query.setMaxResults(paging
query.setFirstResult(pagin
return query.list();
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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( ).buildSes sionFactor y();
session = sessionFactory.openSession ();
} catch (Throwable ex) {
System.err.println("Initia l SessionFactory creation failed." + ex);
throw new ExceptionInInitializerErro r(ex);
}
}
private HibernateUtil(){
}
public static synchronized Session getSession() {
if(sessionFactory == null){
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure( ).buildSes sionFactor y();
session = sessionFactory.openSession ();
} catch (Throwable ex) {
System.err.println("Initia l SessionFactory creation failed." + ex);
throw new ExceptionInInitializerErro r(ex);
}
}
return session;
}
}
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(
session = sessionFactory.openSession
} catch (Throwable ex) {
System.err.println("Initia
throw new ExceptionInInitializerErro
}
}
private HibernateUtil(){
}
public static synchronized Session getSession() {
if(sessionFactory == null){
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(
session = sessionFactory.openSession
} catch (Throwable ex) {
System.err.println("Initia
throw new ExceptionInInitializerErro
}
}
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.
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.
ASKER
that was really fast.
Another thing: Am I supposed to close the session?
best