Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What mysql driver do I use with spring/hibernate?

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

What should I be using? Is the connectorJ the one I should use?

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:mysql://localhost/blah"/>



<property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect

I'm using Maven so if I can get the driver from maven that would be ideal.

Running my app in tomcat I get the error:

Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL 
like image 232
Blankman Avatar asked Nov 28 '22 04:11

Blankman


2 Answers

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

No, they are not. The driverclassname is referring to, well, the driver class name which is the class from a given JDBC driver that implements java.sql.Driver. The driver class name is driver specific.

When using MySQL's JDBC driver aka MySQL Connector/J, this class is com.mysql.jdbc.Driver as explained in the MySQL Connector/J documentation:

20.3.4.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver. (...)

And actually, they even provide instructions to use their driver with Spring. See the section 20.3.5.2.4. Using Connector/J with Spring.

The hibernate.dialect is different, this configuration property is used to define the classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database. Again this is explained in the Hibernate documentation:

3.4. Optional configuration properties

(...) The classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database.

e.g. full.classname.of.Dialect

In most cases Hibernate will actually be able to choose the correct org.hibernate.dialect.Dialect implementation based on the JDBC metadata returned by the JDBC driver.

For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables (this would be my recommendation) or org.hibernate.dialect.MySQL5Dialect if you're not. See the section 3.4.1. SQL Dialects for a (non exhaustive) list.

Last point, the Maven part that you didn't even mention in your question... The MySQL JDBC driver is available in the Maven central repository and you should use a repository search engine (as I already suggested). For example, the following query:

http://www.jarvana.com/jarvana/search?search_type=project&project=mysql

allows to find the maven coordinates of the ultimate version in two clicks:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.13</version>
</dependency>

PS: I don't mean to be rude and I'm glad to help but you should really try to leverage the documentation of the products or frameworks you're using. What you're asking in this question is well documented (as I showed) and can be found easily. Learning to find basic information by yourself is a fundamental skill for a software developer in my opinion.

like image 52
Pascal Thivent Avatar answered Dec 08 '22 10:12

Pascal Thivent


regarding maven mysql definition, here's one that seems to work.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.12</version>
</dependency>
like image 33
tim_wonil Avatar answered Dec 08 '22 09:12

tim_wonil