简体   繁体   中英

Wildfly 12 error in datasource

I'm trying to create a simple datasource in Wildfly 12 in order to connect a mysql database to a Java WEB Application.

I have tried 2 options:

  1. Modify the standalone-full.xml to add the datasource like this:

     <datasource jndi-name="java:jboss/datasources/MyDS" pool-name="MyDS" enabled="true" use-java-context="true"> <connection-url>jdbc:mysql://localhost:3306/MyDatabaseName</connection-url> <driver>mysql</driver> <pool> <min-pool-size>2</min-pool-size> <max-pool-size>5</max-pool-size> </pool> <security> <user-name>myuser</user-name> <password>mypassword</password> </security> </datasource> <drivers> <driver name="mysql" module="com.mysql"> <driver-class>com.mysql.jdbc.Driver</driver-class> </driver> </drivers> 
    1. Add the datasource using the admin console

I'm creating a Connection using the datasource without problems:

Context initCtx = new InitialContext(), envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource) envCtx.lookup("jdbc/MyDS");
        this.connection = ds.getConnection();

The Java Connection works without errors but when I create a servlet to query my database, I got an exception like this:

"Table "user" not found; SQL statement: Select * from user where username = 'myusername'"

I have run some tests and I could notice that Java is connected to the database but not to the specific schema, I run a query like "SELECT DATABASE() FROM DUAL" and the result is "TEST". So I guess that the database name param in the URL connection is not working properly.

How can I solve this problem? I haven't found any additional param to specify the database name in the datasource.

Thanks for your time.

Well, after having tried some modifications, I realized that I had to put the next code in the web.xml file:

<resource-ref>
    <res-ref-name>jdbc/MyDS/<res-ref-name>
    <jndi-name>jdbc:mysql://localhost:3306/MyDatabaseName</jndi-name>
</resource-ref>

This solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM