facebook

Hibernate Tutorial Problem: JDBC Driver not found

  1. MyEclipse Archived
  2.  > 
  3. Documentation
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #217334 Reply

    symantec
    Member

    Dear 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

    #217341 Reply

    symantec
    Member

    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.

    Then another problem appears:(, when ever i try to submit, I encounter Hibernate Exception: Session is closed!

    Can some one help on this 🙂

    Thanks

    #217406 Reply

    vbfischer
    Member

    @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!
    Thanks

    Sounds 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.zip

    Otherwise, 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;
    }

    #217416 Reply

    symantec
    Member

    Thanks 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

    #217417 Reply

    vbfischer
    Member

    Strange. 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?

    #217423 Reply

    symantec
    Member

    Hi 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

    #217424 Reply

    symantec
    Member

    Also, i am using the generated hibernate.cfg with JDBC driver (not using datasouce)

    #217493 Reply

    vbfischer
    Member

    @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?

    #229123 Reply

    LaLiLuna
    Member

    I 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.

    #231159 Reply

    I 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

    #231684 Reply

    TMoney
    Member

    I 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?

    #237592 Reply

    stoneskin
    Member

    This message has not been recovered.

    #244988 Reply

    hibernate: [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.OracleDriver

    Cause : “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

    #245109 Reply

    Brian Fernandes
    Moderator

    Thanh,

    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.

Viewing 14 posts - 1 through 14 (of 14 total)
Reply To: Hibernate Tutorial Problem: JDBC Driver not found

You must be logged in to post in the forum log in