user454232
user454232

Reputation: 871

JPA+mysql No suitable driver found when deployed to JBoss 7.1.1

I know this has been asked so many times but still I couldn't find a solution to it. I am using JPA + Hibernate provider + MySQL in my spring mvc project. When I deploy it to Tomcat, it executes with no problem. Then when I move it to JBoss 7.1.1. it throws me No suitable Driver found exception.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

So I narrow this down to JBoss.

This is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="personDB">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.springapp.modlels.OfficeEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="9ijn)OKM"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

and this is my calling code

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("personDB");
        EntityManager mgr = emf.createEntityManager();
        mgr.getTransaction().begin();
        OfficeEntity officeEntity = new OfficeEntity();
        officeEntity.setOfficeName("test");
        mgr.persist(officeEntity);
        mgr.getTransaction().commit();

It looks like the JBoss cannot find the suitable driver when getTransaction() get called. And my driver is

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.1.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

UPDATED:David Levesque suggested me to install the JDBC driver to JBoss. This is what i did in \modules\com\mysql\main, I copied mysql-connector-java-5.1.21.jar here and created the module.xml,

<module xmlns="urn:jboss:module:1.0" name="com.mysql">
  <resources>
    <resource-root path="mysql-connector-java-5.1.21.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
  </dependencies>
</module>

I also modified the \standalone\configuration\standalone.xml

<datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <datasource jta="false" jndi-name="java:/mysqldb" pool-name="my_mysl" enabled="true" use-ccm="false">
                    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                    <driver>mysql</driver>
                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>root</user-name>
                        <password>9ijn)OKM</password>
                    </security>
                    <validation>
                        <validate-on-match>false</validate-on-match>
                        <background-validation>false</background-validation>
                    </validation>
                    <statement>
                        <prepared-statement-cache-size>32</prepared-statement-cache-size>
                        <share-prepared-statements>false</share-prepared-statements>
                    </statement>
                </datasource>
                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                    <driver name="mysql" module="com.mysql"/>
                </drivers>
            </datasources>

And I also tested the datasource in JBoss console and it connects succesfully. But I am sorry, by adding this, it still shows me the same exception.

Please help, Thank you.

Upvotes: 0

Views: 1831

Answers (1)

Ujwal Daware
Ujwal Daware

Reputation: 391

Your hibernate configuration is fine for jboss, but i think you missed the one part to configure hibernate with jboss.

1) So please add the dependencies <module name="com.mysql"/> in the module.xml in the

jboss-as-7.1.1.Final\modules\org\hibernate\main\module.xml 

2) Create the correct data source in your standalone.xml

<datasource jta="false" jndi-name="java:jboss/datasources/mysqldb" pool-name="my_mysl" enabled="true" use-ccm="false">
                    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                    <driver>mysql</driver>
                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>root</user-name>
                        <password>9ijn)OKM</password>
                    </security>
                    <validation>
                        <validate-on-match>false</validate-on-match>
                        <background-validation>false</background-validation>
                    </validation>
                    <statement>
                        <prepared-statement-cache-size>32</prepared-statement-cache-size>
                        <share-prepared-statements>false</share-prepared-statements>
                    </statement>
                </datasource>

3) Remove the following properties from persistence.xml

<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="9ijn)OKM"/>

Except these your configuration is right but please correct your peaces of code with above changes. Your error will resolve...

For the reference, you can follow this link Configure hibernate in jboss 7.1.1

Thank you...

Upvotes: 0

Related Questions