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&characterEncoding=UTF8&autoReconnect=true</value-->
<value>jdbc:mysql://localhost:3306/kk?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&max-connections=50&min-connections=2&inactivity-timeout=30&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 updateBut 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&characterEncoding=UTF8&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.