-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cannot open connection [tomcat JNDI + mysql 4.10]
PostPosted: Wed Jun 15, 2005 6:40 am 
Beginner
Beginner

Joined: Tue May 17, 2005 7:39 am
Posts: 27
Location: Rome, Italy
i am following the quickstart with tomcat and the example of the Cat in the hibernate reference docs, and i still have problems at runtime when my servlet recalls a simple method of HibernateUtil.java, that follows:

Code:
HibernateUtil.java

public static void connessione(){
       
       
        Session session = HibernateUtil.currentSession();
       
       
        Transaction tx= session.beginTransaction();
               
        Cat princess = new Cat();
        princess.setName("Princess");
        princess.setSex('F');
        princess.setWeight(7.4f);
               
        session.save(princess);
               
        tx.commit();
       
        HibernateUtil.closeSession();
       
    }



Hibernate version:
3.0


Full stack trace of any exception that occurs:
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.AbstractBatcher.openConnection(AbstractBatcher.java:427)
   org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:168)
   org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:103)
   org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
   org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:24)
   org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:231)
   org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1073)
   org.hibernate.examples.quickstart.HibernateUtil.connessione(HibernateUtil.java:63)
   prova.processRequest(prova.java:56)
   prova.doGet(prova.java:72)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)


Mapping documents:
hibernate.cfg.xml:
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="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.datasource">java:comp/env/jdbc/quickstart2</property>
        <property name="show_sql">false</property>
       
        <!-- Mapping files -->
        <mapping resource="org/hibernate/examples/quickstart/Cat.hbm.xml"/>

    </session-factory>

</hibernate-configuration>





i found useful editing the context.xml of my webapp, setting the resourceLink with the name of the connection provided by tomcat through JNDI
when i was setting the name with the path of the JDBC provided connection, it was saying "could not find datasource"
context.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/quickstart2">
  <Logger className="org.apache.catalina.logger.FileLogger" prefix="quickstart2." suffix=".log" timestamp="true"/>
 
  <ResourceLink name="java:comp/env/jdbc/quickstart2" global="java:comp/env/jdbc/quickstart2"
      type="javax.sql.DataSource"/>
</Context>





Cat.hbm.xml
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="org.hibernate.examples.quickstart.Cat" table="CAT">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
        <id name="id" type="string" unsaved-value="null" >
            <column name="CAT_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>

        <!-- A cat has to have a name, but it shouldn' be too long. -->
        <property name="name">
            <column name="NAME" length="16" not-null="true"/>
        </property>

        <property name="sex"/>

        <property name="weight"/>

    </class>

</hibernate-mapping>


Name and version of the database you are using:
mysql 4.10

(maybe there's an error in something like JTAFactory?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 6:43 am 
Beginner
Beginner

Joined: Tue May 17, 2005 7:39 am
Posts: 27
Location: Rome, Italy
server.xml section used to declare the resource


Code:
<Context path="/quickstart2" docBase="quickstart2">
           <Resource name="jdbc/quickstart2" scope="Shareable" type="javax.sql.DataSource"/>
           <ResourceParams name="jdbc/quickstart2">
               <parameter>
                   <name>factory</name>
                   <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
               </parameter>
      
               <!-- DBCP database connection settings -->
               <parameter>
                   <name>url</name>
                   <value>jdbc:mysql://localhost:3306/quickstart2</value>
               </parameter>
               <parameter>
                   <name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
               </parameter>
               <parameter>
                   <name>username</name>
                   <value>root</value>
               </parameter>
               <parameter>
                   <name>password</name>
                   <value>mox601</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:
PostPosted: Wed Jun 15, 2005 7:04 am 
Beginner
Beginner

Joined: Tue May 17, 2005 7:39 am
Posts: 27
Location: Rome, Italy
i found out that the error occurs in HibernateUtil.java connessione() method, when it calls "Transaction tx= session.beginTransaction();"


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