-->
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.  [ 13 posts ] 
Author Message
 Post subject: Exception using Hibernate and Tomcat: Cannot open connection
PostPosted: Tue Oct 04, 2005 2:36 pm 
Newbie

Joined: Wed Sep 28, 2005 9:58 am
Posts: 5
Hi,

First of all, I'd like to say I am a newbie using Hibernate.

I was following the road map proposed in Hibernate.org, but I am having many problems trying to make Hibernate work with Tomcat. I managed to complete chapter 2, which made me believe that the problem is not with the database configuration.

The problem is: I am getting a GenericJDBCException: Cannot open connection when I call session.beginTransaction(). I've tried searching this everywhere in the Internet and I could'nt find a solution.

More details:

Hibernate version:
I am using Hibernate 3.0.

Tomcat version:
I am using Tomcat 5.5.9.

Name and version of the database:
SQL Server 2000

web.xml:
Code:
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <display-name>Quickstart</display-name>
    <description>
      A quickstart to servlets.
    </description>

      <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/quickstart</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
   
    <!-- Define servlets that are included in the example application -->

    <servlet>
        <servlet-name>Teste</servlet-name>
        <servlet-class>Teste</servlet-class>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>Teste</servlet-name>
        <url-pattern>/Teste</url-pattern>
    </servlet-mapping>
   

</web-app>


hibernate.cfg.xml
Code:
<?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">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/quickstart</property>       
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
            
        <!-- Mapping files -->
        <mapping resource="Cat.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Code between sessionFactory.openSession() and session.close():

Code:
    Transaction tx = s.beginTransaction(); //the error happens here
       
    Cat aCat = new Cat();
    aCat.setName("Floquinho");
    aCat.setSex('M');
    aCat.setWeight(7.4f);
       
    s.save(aCat);
       
    tx.commit();



The Exception I am getting:

Code:
org.hibernate.exception.GenericJDBCException: Cannot open connection
   org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
   org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
   org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:301)
   org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:110)
   org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:137)
   org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
   org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:24)
   org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
   org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
   Principal.grava(Principal.java:12)
   Teste.doGet(Teste.java:24)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)



Thanks,
Luis Felipe


Top
 Profile  
 
 Post subject: Another file
PostPosted: Tue Oct 04, 2005 2:40 pm 
Newbie

Joined: Wed Sep 28, 2005 9:58 am
Posts: 5
quickstart.xml inside tomcat/conf/Catalina/localhost

Code:
<Context path="/quickstart" docBase="quickstart">
    <Resource name="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/quickstart">
        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>

        <!-- DBCP database connection settings -->
        <parameter>
            <name>url</name>
            <value>jdbc:jtds:sqlserver://192.168.0.211:1433/teste2</value>
        </parameter>
        <parameter>
            <name>driverClassName</name>
            <value>net.sourceforge.jtds.jdbc.Driver</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>sa</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value></value>
        </parameter>

        <!-- DBCP connection pooling options -->
        <parameter>
            <name>maxWait</name>
            <value>3000</value>
        </parameter>
        <parameter>
            <name>maxIdle</name>
            <value>100</value>
        </parameter>
        <parameter>
            <name>maxActive</name>
            <value>10</value>
        </parameter>
    </ResourceParams>
</Context>


Top
 Profile  
 
 Post subject: It's working
PostPosted: Thu Oct 06, 2005 1:47 pm 
Newbie

Joined: Wed Sep 28, 2005 9:58 am
Posts: 5
It's finally working, but I actually don't know what the error was...

The fact is that I tried to use Tomcat's Administration tool to configure my Data Source resource and it worked well (using the same configurations).

Thanks for those who read my post and tried to find the error.

Luís


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 2:01 pm 
Newbie

Joined: Wed Sep 28, 2005 9:58 am
Posts: 5
Just to make myself a little more clear, the problem wasn't due to Hibernate. The problem was with the configuration of the Data Source in Tomcat.


Top
 Profile  
 
 Post subject: help
PostPosted: Mon Oct 17, 2005 11:36 am 
Newbie

Joined: Mon Oct 17, 2005 11:32 am
Posts: 8
Location: India
hi,
can u please tell me where to find the administration tool for tomcat.. i am using tomcat 5.5.12

thnks
Amit


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 11:59 am 
Newbie

Joined: Wed Sep 28, 2005 9:58 am
Posts: 5
You can find the Administration Tool for version 5.5.12 at:
http://tomcat.apache.org/download-55.cgi

Look for section 5.5.12, subsection Binary Distributions and then download Administration Web Application.

Unzip the downloaded file and copy it in the Tomcat Installation directory.

You have to add a new role and a new user to CATALINA_HOME/conf/tomcat-users.xml. After you do that, your file should look like the following:

Code:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
  <user username="admin" password="password" roles="admin,manager"/>
</tomcat-users>


In order to access it, go to http://localhost:8080 and click on the link Tomcat Administration.

Best regards,
Luis


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:30 pm 
Newbie

Joined: Mon Oct 17, 2005 11:32 am
Posts: 8
Location: India
thanks a lot...

rgds
Amit


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 10:12 pm 
Newbie

Joined: Sat Nov 26, 2005 10:10 pm
Posts: 4
Location: Edmonton, AB, Canada
Amit, were you able to solve your problem with Tomcat 5.5.12 ?
I'm also using 5.5.12 and am having the same problem.

-Rob


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 2:06 am 
Newbie

Joined: Mon Oct 17, 2005 11:32 am
Posts: 8
Location: India
tgs753 wrote:
Amit, were you able to solve your problem with Tomcat 5.5.12 ?
I'm also using 5.5.12 and am having the same problem.

-Rob


yes, using the admin manager tool for configuring database as jndi service
works for me fine

rgds
Amit


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 2:39 am 
Newbie

Joined: Sat Nov 26, 2005 10:10 pm
Posts: 4
Location: Edmonton, AB, Canada
Ok. I'm glad someone had figured it out with Tomcat 5.5.12. :)

I downloaded the admin.zip, un-zipped it and I copied the whole admin folder into C:\Tomcat\webapps . I then re-started Tomcat and tried to use the admin app, but Tomcat is still saying that "admin is not installed and you should download it and install it."

Is this how you installed the admin app?
Did you copy the admin folder into a different place ?

Could you post how and where you installed admin ?
There doesn't seem to be a lot of messages about the Tomcat 5.5.12 admin app and how to install it properly.

Thank you.

Rob


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 4:40 am 
Newbie

Joined: Sat Nov 26, 2005 10:10 pm
Posts: 4
Location: Edmonton, AB, Canada
I managed to install the admin app and created a new DataSource

However, I'm still having the error:
org.hibernate.exception.GenericJDBCException: Cannot open connection

-Hibernate 3
-Tomcat 5.5.12

My DataSource that I set up with Tomcat's admin app looks like this:

Code:
name: jdbc/test
url: jdbc:mysql://localhost:3306/test?autoReconnect=true
driver: com.mysql.jdbc.Driver
username: test
password: ******


Note: It is possible to connect to the database from mysql's command line.

my hibernate.cfg.xml file looks like this:

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
      <property name="connection.datasource">java:comp/env/jdbc/test</property>
      <property name="show_sql">false</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

   <!-- Mapping files -->
   <mapping resource="/BaseHelloWorldObject.hbm.xml" />

</session-factory>

</hibernate-configuration>


BaseHelloWorldObject.hbm.xml looks like this:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.mypackage.HelloWorld.pojo.BaseHelloWorldObject" table="hello_world">
        <id name="id" column="id">
           <generator class="native"/>
        </id>
      <property name="name" type="string" column="name"/>       
      <property name="message" type="string" column="message"/>
        <property name="messageDate" type="timestamp" column="message_date"/>       
    </class>

</hibernate-mapping>


HibernateUtil.java looks like this:

Code:
public class HibernateUtil
{

    public 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 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();
            // Store it in the ThreadLocal variable
            session.set(s);
        }

        return s;
    }

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


And the error is thrown at:

Code:
        Session session = HibernateUtil.currentSession();
        System.out.println("got session");
        System.out.println("getting transaction");
        Transaction tx = session.beginTransaction(); <-- ERROR


The error is:

Code:
org.hibernate.exception.GenericJDBCException: Cannot open connection
   org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
   org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
   org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:301)
   org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:110)
   org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:137)
   org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
   org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:24)
   org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
   org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
   com.mypackage.HelloWorld.action.HelloWorldAction.test(HelloWorldAction.java:30)
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 7:32 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/114.html
http://hibernate.org/42.html
http://hibernate.org/43.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 5:04 pm 
Newbie

Joined: Sat Nov 26, 2005 10:10 pm
Posts: 4
Location: Edmonton, AB, Canada
Thanks Christian.
I ditched JNDI and set Hibernate to manage the connection pool using c3p0.

Rob


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