Wildfly mysql instead of h2

2019-08-12 05:07发布

问题:

We are running a porject in Eclipse and use Wildfly as webserver. We have configurated wildfly and created a new mysql datasource that works. We have created a database and a new Connection in Eclipse to the database and created entities using the url provided by Wildfly. So Everything is good this far.

The problem we have now is that when we run the application and try to persist an entity we get an error saying "Caused by: org.h2.jdbc.JdbcSQLException: Table "USER" not found; SQL statement..."

After gooling it we found out that we have to alter the standalone.xml and did so.

         <datasource jta="false" jndi-name="java:jboss/fakebook" pool-name="mysql" enabled="true" use-ccm="false">
                <connection-url>jdbc:mysql://localhost:3306/fakebook</connection-url>
                <driver-class>com.mysql.jdbc.Driver</driver-class>
                <driver>mysql</driver>
                <security>
                    <user-name>root</user-name>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="com.mysql" module="mysql.mysql-connector-java">
                  <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                </driver>
                <driver name="mysql" module="org.mysql">
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                </driver>
            </drivers>

Restarted the server but got the same problem with the same exception. When we opened standalone it had changed back to default value:

                    <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="mysql" module="org.mysql">
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                </driver>

回答1:

In addition to making sure your standalone.xml doesn't get overwritten (as pointed out by 'shi') you should also add a MySQL module to wildfly:

  1. Go to %WILDFLY_HOME%\modules\system\layers\base\com
  2. Create folder sql\mysql\main\
  3. Place MySql connector jar to main folder
  4. Place the following module.xml file to main folder (change the name of the connector in resource root path according to the version you use):
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.sql.mysql">
  <resources>
     <resource-root path="mysql-connector-java-5.0.4-bin.jar"/>             
  </resources>
  <dependencies>
     <module name="javax.api"/>
  </dependencies>
</module>

Source material used: some blog



回答2:

I think you did not shutdown the server prior to your standalone.xml changes. Am I right? Wildfly will override the standalone.xml when you stop the server, so you have to stop the server prior to changing the configuration file! If you want to edit the datasource definitions within the running server instance and test it you could also use the Command Line Interface.



回答3:

After connection-url try to put this xml tag:

<connection-property name="DatabaseName">"name_of_database"</connection-property>

Maybe hibernate could not find the database.



标签: jboss wildfly