-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem using Hibernate with Tomcat DataSource
PostPosted: Tue Mar 23, 2004 7:06 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
The JDBC driver I use is "mysql-connector-java-3.1.1-alpha-bin.jar"
Below is my Tomcat context xml content

Code:
<Context path="/kkgo" docBase="C:/Projects/JAVA/eclipse/KKgo" reloadable="true">
   <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_kkgo_log." suffix=".txt" timestamp="true"/>
   <!--Resource name="jdbc/mysqlDB" auth="container" type="javax.sql.DataSource"/-->
   <Resource name="jdbc/mysqlDB" auth="container" type="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"/>
   <ResourceParams name="jdbc/mysqlDB">
      <parameter>
      <name>factory</name>
      <!--value>org.apache.commons.dbcp.BasicDataSourceFactory</value-->
      <value>com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory</value>
    </parameter>
      <parameter>
        <name>port</name>
        <value>3306</value>
    </parameter>
    <parameter>
        <name>serverName</name>
        <value>localhost</value>
    </parameter>
    <parameter>
        <name>databaseName</name>
        <value>kk</value>
    </parameter>
    <parameter>
        <name>explicitUrl</name>
        <value>true</value>
    </parameter>
   
    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>2</value>
    </parameter>

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <!--<parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>-->

    <!-- SQL Server dB username and password for dB connections  -->
    <parameter>
     <!--name>username</name-->
     <name>user</name>
     <value>root</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>et1926</value>
    </parameter>

    <!--parameter>
       <name>driverClassName</name>
       <value>com.mysql.jdbc.Driver</value>
    </parameter-->

    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
    <parameter>
      <name>url</name>
      <!--value>jdbc:mysql://127.0.0.1:3306/kk?useUnicode=true&amp;characterEncoding=UTF8&amp;autoReconnect=true</value-->
      <value>jdbc:mysql://localhost:3306/kk?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;max-connections=50&amp;min-connections=2&amp;inactivity-timeout=30&amp;wait-timeout=30</value>
    </parameter>

   </ResourceParams>
</Context>


I can use this datasource to execute qurey and update with no problem. but problem occured when I use Hibernate session to save entity.
Below is the stack trace

Code:
java.sql.SQLException: General error message from server: "Table 'kk.hibernate_unique_key' doesn't exist"
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2234)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1412)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1494)
   at com.mysql.jdbc.Connection.execSQL(Connection.java:1909)
   at com.mysql.jdbc.Connection.execSQL(Connection.java:1847)
   at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1711)
   at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1559)
   at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:93)
   at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:747)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:720)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1359)


The reason to cause this exception is a sql statement executed by hibernate
select next_hi from hibernate_unique_key for update

But when I use
Code:
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://127.0.0.1/KK?useUnicode=true&amp;characterEncoding=UTF8&amp;autoReconnect=true</property>
            <property name="connection.username">root</property>
            <property name="connection.password">et1926</property>

instead of using datasource
Code:
<property name="connection.datasource">java:comp/env/jdbc/mysqlDB</property>


This problem won't occured. Could Hibernate team tell me why?
Thanks a lot.


Top
 Profile  
 
 Post subject: Problem of Hiberante using datasource to access MySQL solved
PostPosted: Wed Mar 24, 2004 2:05 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
If you want to use datasource to access MySQL, the xdoclet script of hibernate id be sure to use "increment" instead of "native".
like this below:
Code:
/**
* @hibernate.id generator-class="increment" unsaved-value="null"
*/


Because the hilogenerator doesn't work when using datasource to access MySQL.


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