1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[JBoss] Why am I getting "No database selected" error when using Hibernate with MySQL in...

Discussão em 'StackOverflow' iniciado por Stack, Janeiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to deploy a Java application to WildFly that uses Hibernate with a MySQL database. However, when I run my application, I get the following error in the logs:

    alter table exam_table
    drop
    foreign key FKgp6h92beial8lknuhckmukak8" via JDBC [No database selected]


    persistence.xml:

    <persistence xmlns="https://jakarta.ee/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence
    https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
    version="3.0">

    <persistence-unit name="control" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

    <!-- Entity classes -->
    <class>com.to.Speciality</class>
    <class>com.to.Exam</class>
    <class>com.to.Student</class>
    <class>com.to.Session</class>

    <!-- Hibernate Provider Properties -->
    <properties>
    <!-- JDBC Configuration (commented out as we use JTA) -->
    <!-- <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /> -->
    <!-- <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/examdb" /> -->
    <!-- <property name="jakarta.persistence.jdbc.user" value="root" /> -->
    <!-- <property name="jakarta.persistence.jdbc.password" value="mahdy" /> -->

    <!-- Hibernate Provider Properties -->
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="create" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.use_sql_comments" value="false" />

    <!-- Connection Pooling (Optional) -->
    <property name="hibernate.connection.pool_size" value="10" />
    </properties>
    </persistence-unit>
    </persistence>


    standalone.xml:

    <subsystem xmlns="urn:jboss:domain:datasources:7.2">
    <datasources>
    <datasource jndi-name="java:jboss/datasources/MySQLDS" pool-name="MySQLDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:mysql://localhost:3306/examdb?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false</connection-url>
    <driver>com.mysql</driver>
    <pool>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size>
    </pool>
    <security user-name="root" password="pwd"/>
    <validation>
    <check-valid-connection-sql>USE examdb; SELECT 1</check-valid-connection-sql>
    <background-validation>true</background-validation>
    <background-validation-millis>10000</background-validation-millis>
    </validation>
    </datasource>
    <drivers>
    <driver name="com.mysql" module="com.mysql">
    <driver-class>com.mysql.cj.jdbc.Driver</driver-class>
    <datasource-class>com.mysql.cj.jdbc.MysqlDataSource</datasource-class>
    </driver>
    </drivers>
    </datasources>
    </subsystem>


    Issue: When I try to deploy my .war file to WildFly, I encounter the following error:

    GenerationTarget encountered exception accepting command : Error executing DDL "
    alter table exam_table
    drop
    foreign key FKgp6h92beial8lknuhckmukak8" via JDBC [No database selected]


    From my understanding, it seems like Hibernate is trying to execute a DDL command, but it is not able to find the database. I've verified that examdb exists in MySQL and that the database URL is correctly set in standalone.xml.

    What I've Tried:

    I have confirmed that the MySQL database examdb exists. The connection URL in standalone.xml is correctly set to jdbc:mysql://localhost:3306/examdb. I’ve made sure that my JTA data source (java:jboss/datasources/MySQLDS) is correctly referenced in persistence.xml. The MySQL JDBC driver is properly configured in WildFly as a module.

    Question:

    What could be causing the "No database selected" error? How can I fix this issue and ensure Hibernate can correctly select the database for its DDL operations?

    Continue reading...

Compartilhe esta Página