krzemian
krzemian

Reputation: 398

Can't connect to MySQL in Java EE/JBoss application

While deploying my app I'm getting the following errors (I've also included subjectively significant warnings):

03:36:42,113 WARN  [org.jboss.resource.connectionmanager.JBossManagedConnectionPool] Throwable while attempting to get a new connection: null: org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}))
(...)
Caused by: org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar})
(..)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}

03:36:42,122 WARN  [org.hibernate.cfg.SettingsFactory] Could not obtain connection to query metadata: org.jboss.util.NestedSQLException: Unable to get managed connection for MP01Local; - nested throwable: (javax.resource.ResourceException: Unable to get managed connection for MP01Local)
(...)
Caused by: javax.resource.ResourceException: Unable to get managed connection for MP01Local
(...)
Caused by: org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}))
(...)
Caused by: org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar})
(...)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}

03:36:42,143 WARN  [org.jboss.resource.connectionmanager.JBossManagedConnectionPool] Throwable while attempting to get a new connection: null: org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}))
(...)
Caused by: org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar})
(...)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}

03:36:42,147 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] schema export unsuccessful: org.jboss.util.NestedSQLException: Unable to get managed connection for MP01Local; - nested throwable: (javax.resource.ResourceException: Unable to get managed connection for MP01Local)
(...)
Caused by: javax.resource.ResourceException: Unable to get managed connection for MP01Local
(...)
Caused by: org.jboss.resource.JBossResourceException: Could not create connection; - nested throwable: (org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}))
(...)
Caused by: org.jboss.resource.JBossResourceException: Failed to register driver for: com.mysql.jdbc.Driver; - nested throwable: (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar})
(...)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@66a8c396{vfs:///D:/MobilnyPodroznik/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.x_Runtime_Server1345902635716/deploy/MP01.jar}

As for the whole console output: http://pastebin.com/de2Br9xL

Basically, it seems as if my JDBC driver is not visible, but I have placed the .jar both in the server's default/lib folder and in project's build path, thus I'm clueless now.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
       version="2.0">
  <persistence-unit name="mp" transaction-type="JTA">
    <jta-data-source>java:/MP01Local</jta-data-source>

           <properties>
       <!--s
       <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
       -->
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
   </properties>
  </persistence-unit>
</persistence>

mysqllocal-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>

   <local-tx-datasource>
      <jndi-name>MP01Local</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/mp</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>login</user-name>
      <password>pass</password>
   </local-tx-datasource>

</datasources>

I have placed the DS XML file in default/deploy path of the JBoss server and mysql-connector-java-5.0.8-bin in default/lib, as indicated in the JBoss manual.

I'm using

My project build path

  1. jre6
  2. JBoss 6.x Runtime
  3. MySQL JDBC Driver 5.0.8, including mysql-connector-java-5.0.8-bin.jar [I have created it in Data Source Explorer)

Upvotes: 0

Views: 5319

Answers (2)

user1593705
user1593705

Reputation:

JDBC driver have to be placed in the CLASSPATH of your application server.

Upvotes: 1

krzemian
krzemian

Reputation: 398

All right, I've repeated all the steps from the beginning as a fresh morning man and it worked. Still don't know why, but here're a few things that might've solved the problem. Hope this helps to anyone who's struggling to deploy his first Java EE + JPA application.

Here're the steps I've followed:

  • Downloaded mysql-connector-java-5.0.8-bin.jar from http://www.mysql.com/downloads/connector/j/5.0.html#downloads
  • Placed it in jboss-6.0.0.Final\server\default\lib
  • Created a new Connection Profile in Eclipse using Data Source Explorer (I don't know if that step is even necessary). In the wizard I've also created a new Driver Definition using the 5.0.8 connector. The ping succeeded and my database is visible from within the Data Source Explorer.
  • Generated -ds.xml file from the CP mentioned in 3) using File->New->Other...->JBoss Tools Web->JBoss Datasource (-ds.xml) (using AS5 format) and placed it in jboss-6.0.0.Final\server\default\deploy\Mysql_local-ds.xml as well as project's META-INF\Mysql_local-ds.xml (corrected: when I placed it in both places simultaneously, I received ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] Error installing to Real: (...) Caused by: java.lang.IllegalStateException: jboss.jca:name=MP01Datasource2,service=ManagedConnectionFactory is already installed.)
  • I've added the db's jar to build path usign [rmb on project]->Build Path->Configure Build Path...->Add Library->Connectivity Driver Definition and chose the one I've defined in 3)

I have also deleted some old .jar deployments from my other test projects that got stuck in the workspace.metadata.plugins\org.jboss.ide.eclipse.as.core\JBoss_6.x_Runtime_Server1345902635716\deploy folder (supposedly due to incorrect shutdown), but I sincerely doubt that was what helped.

I suppose the main cause might've been that as I was swapping the drivers and Driver Definitions (I've been moving from 5.1.sth to 5.0.8) I forgot to swap them in the pj's build path as well and involuntarily left the old one.

My current persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
       version="2.0">
  <persistence-unit name="mp" transaction-type="JTA">
    <jta-data-source>java:/MP01Datasource2</jta-data-source>
           <properties>
       <!--s
       <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
       -->
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
   </properties>
  </persistence-unit>
</persistence>

Mysql_local-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>

   <local-tx-datasource>
      <jndi-name>MP01Datasource2</jndi-name>
      <use-java-context>true</use-java-context>
      <connection-url>jdbc:mysql://localhost:3306/mp</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>x</user-name>
      <password>y</password>
   </local-tx-datasource>

</datasources>

Upvotes: 1

Related Questions