TomEE deploy failed datasource not found

2019-09-09 09:57发布

问题:

I want to deploy a war file to TomEE but fails with:

Caused by:

javax.naming.NameNotFoundException: 
      Name openejb/Resource/application_name/mysql_ds" not found.

If I restart the server, the deploy goes fine but only once, then the same error encounters.

I have defined datasource in WEB-INF/resources.xml file

<tomee>
    <Resource id="mysql_ds" type="javax.sql.DataSource">
        JdbcDriver  = com.mysql.jdbc.Driver
        JdbcUrl     = jdbc:mysql://IP:3306/db?serverTimezone=UTC&amp;autoReconnect=true
        UserName    = user
        Password    = password
        JtaManaged  = true
    </Resource>
</tomee>

Also i should mention that there is another cloned application(dev mode) with same configuration and it works fine.

回答1:

You can either you use WEB-INF/resources.xml to define one or more datasources or the tomee.xml file inside the <tomee-home>/conf folder, as noted in the corresponding section of the TomEE project documentation:

A DataSource can be declared via xml in the /conf/tomee.xml file or in a WEB-INF/resources.xml file

However, the syntax for resources.xmlis slightly different from the container-wide definition. For a resources.xml bundled with your web-application it should be formulated as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <Resource id="mysql_ds" type="javax.sql.DataSource">
        JdbcDriver  = com.mysql.jdbc.Driver
        JdbcUrl     = jdbc:mysql://IP:3306/db?serverTimezone=UTC&amp;autoReconnect=true
        UserName    = user
        Password    = password
        JtaManaged  = true
    </Resource>
</resources>

Note well, that the tag <resources> is different from <tomee> given in your question. This should work for a default TomEE environment. See also: comment by rmannibucau.

Hope, it helps.