- This topic has 13 replies, 8 voices, and was last updated 18 years, 11 months ago by Brian Fernandes.
-
AuthorPosts
-
symantecMemberDear all,
I tried to follow the Hibernate and Database tutorial, I correctly set up the database connection and able to view all the table information. I did upload the Mysql-connector.jar file as required. However, when I try to run the web application, i got:
net.sf.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.driver
Can someone please help me on this ?
Thank you
symantecMemberIt turns out i have to go
Windows->Preferences->MyEclipse->Tomcat X-> Paths-> Append to classpath, then append the location of mysql-connector.jar file
Problem solved.Then another problem appears:(, when ever i try to submit, I encounter Hibernate Exception: Session is closed!
Can some one help on this 🙂
Thanks
vbfischerMember@symantec wrote:
It turns out i have to go
Windows->Preferences->MyEclipse->Tomcat X-> Paths-> Append to classpath, then append the location of mysql-connector.jar file
Problem solved.This isn’t required. But the JDBC driver DOES have to be in the classpath. Normally, in Eclipse, this is done by:
Project -> Properties -> Java Build Path -> Libraries Tab.
Click Add External Jar and find your JDBC driver (mysql-connector.jar in your case
Then another problem appears:(, when ever i try to submit, I encounter Hibernate Exception: Session is closed!
ThanksSounds like your Session has not been opened. Did you compare your source with that of the example source found here:
http://www.myeclipseide.com/images/tutorials/Hibernate/BasicDB.zipOtherwise, attach your code and full stack trace.
Is this line of code found before you get your exception:
session = SessionFactory.currentSession();If so, make sure your SessionFactory.java has this method:
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();if (session == null || session.isOpen() == false) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println(“%%%% Error Creating SessionFactory %%%%”);
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}return session;
}
symantecMemberThanks for the kind reply. I copy and paste the souce code into my project, so there should be no typo. here is the full stack trace
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: net.sf.hibernate.HibernateException: Session is closed
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)root cause
java.lang.RuntimeException: net.sf.hibernate.HibernateException: Session is closed
com.siliconmemory.hibernate.VipService.addVipdata(VipService.java:260)
com.siliconmemory.action.AddVipdata.execute(AddVipdata.java:52)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)It look likes when I try to addVipdata, the problem occurs. (Note, I can display the page correctly at the very first time after I restart tomcat, but after submit, same error occurs, some time it complaints the getVipDatalist method)
Did I miss something very basic here?
Thanks
vbfischerMemberStrange. The SessionFactory class is using the ThreadLocal pattern to hold the session, and it appears to check to see if the session is either null or not open. If not, it creates a new one and opens it.
Try putting a breakpoint in the currentSession() method of the SessionFactory and make sure its being called correctly. Also, how are you defining your datasource in Tomcat? OR are you using the generated hibernate.cfg?
symantecMemberHi Vbfischer, thanks for the replay. I tried to set a breakpoint in the SessionFactory, it is very strange, session are correctly created.
The error occurs at ” Query query =session.createQuery(“select Vipdata from com.siliconmemory.hibernate.Vipdata Vipdata”);”
The first time this line runs well, the second time the query cannot be created because the session is closed!Thanks
symantecMemberAlso, i am using the generated hibernate.cfg with JDBC driver (not using datasouce)
vbfischerMember@symantec wrote:
The error occurs at ” Query query =session.createQuery(“select Vipdata from com.siliconmemory.hibernate.Vipdata Vipdata”);”
The first time this line runs well, the second time the query cannot be created because the session is closed!Thanks
Try putting a breakpoint there. See if session is still valid. Also, have you enabled logging to see whats going on?
LaLiLunaMemberI do not like the implementation of the Hibernate Session Factory of MyEclipse. You can run easily into this kind of problems. I put some changes in my tutorial. Actually it is only a check to verify if the session is opened. A session can be != null and not open, which is the reason for this problem.
http://www.laliluna.de/struts-hibernate-integration-tutorial-en.html
Kind Regards
Sebastian Hennebrueder
—–
http://www.laliluna.de/tutorials.html
Tutorials for Java, Struts, JavaServer Faces, JSP, Hibernate, EJB and more.
Bjarne PedersenMemberI had the same problem, but with your Hibernate Session Factory is solved.
Now im gonna check your website to see if you have some more advanced tutorial. I need to learn someting about many-to-many etc.MaxiCom
v/Bjarne Pedersen
TMoneyMemberI was having the same “hibernate session is closed” error. I removed..
session.close();
Line from the get and update service functions and now im ok. IS thisa bug in the tutorial code or am I getting around the problem the wrong way?
stoneskinMemberThis message has not been recovered.
thanh.oracle@gmail.comMemberhibernate: [schemaupdate]
BUILD FAILED
D:\j2ee\realMethods\aib\workspace\e-shop-simple\build.xml:367:
Schema text failed: JDBC Driver class not found: oracle.jdbc.driver.OracleDriverCause : “db.jars” do not contain path to JDBC driver
Buil.xml
<property name=”rm.home” value=”D:\j2ee\realMethods”/>
<property name=”j2ee.home” value=”${rm.home}/lib/j2ee”/>
<property name=”db.lib” value=”${rm.home}/lib/sqlserver”/>
<property name=”db.jars” value=”${db.lib}/classes12.jar;${db.lib}/msbase.jar;${db.lib}/mssqlserver.jar”/>
<property name=”deploy.dir” value=”eshop”/>Fix : Copy JDBC driver to “db.jars”
Include in “db.jars” JDBC driver
Brian FernandesModeratorThanh,
I’m sorry I really don’t understand what your problem is – what steps did you take that brought you to this point, could you be a little more descriptive?
Does the fix you specified work for you?Best,
Brian. -
AuthorPosts