通过 web 控制台向 wildfly 添加数据源

Adding a datasource to wildfly via web console

我已经搜索了好几天了,但找不到进一步的内容。 我正在尝试通过 Web 控制台将 MySQL 数据源分配给我的 Wildfly 9 服务器。 如果我添加资源,我会在测试资源时出错,因为 wildfly 9 中的错误创建了最大池大小为 0 的资源。 所以我自己编辑了它,一切似乎都很好。 现在,当我尝试在我的 web 应用程序中查找该资源时,它只是一个生成一些 json 数据的 classic servlet 应用程序,我收到一个错误。 javax.naming.NameNotFoundException。 资源的 jndi 名称是:java:/MySQLDS

这是通过 Web 控制台生成的 XML:

<datasource jta="true" jndi-name="java:/MySQLDS" pool-name="MySQLDS" enabled="true" use-ccm="true" statistics-enabled="false">
    <connection-url>jdbc:mysql://ip:3306/test</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver>mysql</driver>
    <pool>
        <min-pool-size>0</min-pool-size>
        <initial-pool-size>0</initial-pool-size>
        <max-pool-size>20</max-pool-size>
        <allow-multiple-users>false</allow-multiple-users>
    </pool>
    <security>
        <user-name>user</user-name>
        <password>password</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <validate-on-match>false</validate-on-match>
        <background-validation>true</background-validation>
        <use-fast-fail>false</use-fast-fail>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
    <timeout>
        <set-tx-query-timeout>false</set-tx-query-timeout>
        <blocking-timeout-millis>0</blocking-timeout-millis>
        <idle-timeout-minutes>0</idle-timeout-minutes>
        <query-timeout>0</query-timeout>
        <use-try-lock>0</use-try-lock>
        <allocation-retry>0</allocation-retry>
        <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
    </timeout>
    <statement>
        <share-prepared-statements>false</share-prepared-statements>
    </statement>
</datasource>

在我的应用程序中,我有一个 class 出于各种原因在哈希图中管理我的连接。

public class DBConnection {

    static {
        try {
            context = new InitialContext();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    private static InitialContext                       context;

    /**
     * JNDI Lookup cache
     */
    private static final HashMap<String, DataSource>    connectionCache = new HashMap<String, DataSource>();

    private DBConnection() {
    }

    public static Connection getConnection(String shema) {
        DataSource ds = connectionCache.get(shema);
        if (ds == null) {
            try {
                ds = (DataSource) context.lookup("java:/" + shema);
            } catch (NamingException e) {
                e.printStackTrace();
            }
            connectionCache.put(shema, ds);
        }
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

这会产生上述错误。

现在是奇怪的部分: 我从 Web 控制台中删除了资源,并通过我的 WEB-INF 文件夹中的 *-ds.xml 文件添加了完全相同的资源,并且它有效!

<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
    <datasource jndi-name="java:/MySQLDS" pool-name="MySQLPOOL">
        <connection-url>jdbc:mysql://ip:3306/test</connection-url>
        <driver>mysql</driver>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <security>
            <user-name>user</user-name>
            <password>password</password>
        </security>
    </datasource>
</datasources>

问题是如何通过 Web 控制台添加资源并在我的应用程序中找到? 如果我通过 xml 添加资源而不是通过 Web 控制台添加资源,为什么我能够找到资源?

这是我的数据源,它工作正常(通过网络控制台创建)。

           <datasource jta="true" jndi-name="java:/jdbc/NotesUsersDS" pool-name="NotesUsersDS" enabled="true" use-ccm="true" statistics-enabled="true">
                <connection-url>jdbc:mysql://localhost:3306/users?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf8&amp;amp;characterSetResults=utf8&amp;amp;connectionCollation=utf8_unicode_ci;useLegacyDatetimeCode=false</connection-url>
                <driver-class>com.mysql.jdbc.Driver</driver-class>
                <driver>mysql-connector-java-5.1.35.jar_com.mysql.jdbc.Driver_5_1</driver>
                <transaction-isolation>TRANSACTION_READ_UNCOMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>1</min-pool-size>
                    <initial-pool-size>0</initial-pool-size>
                    <max-pool-size>256</max-pool-size>
                    <allow-multiple-users>false</allow-multiple-users>
                </pool>
                <security>
                    <user-name>users_user</user-name>
                    <password>my_password</password>
                </security>
                <validation>
                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>true</background-validation>
                    <use-fast-fail>false</use-fast-fail>
                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                </validation>
                <timeout>
                    <set-tx-query-timeout>false</set-tx-query-timeout>
                    <blocking-timeout-millis>0</blocking-timeout-millis>
                    <idle-timeout-minutes>0</idle-timeout-minutes>
                    <query-timeout>0</query-timeout>
                    <use-try-lock>0</use-try-lock>
                    <allocation-retry>0</allocation-retry>
                    <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
                </timeout>
                <statement>
                    <track-statements>true</track-statements>
                    <prepared-statement-cache-size>128</prepared-statement-cache-size>
                    <share-prepared-statements>true</share-prepared-statements>
                </statement>
            </datasource>

好吧,现在我觉得自己智障了..

我只需要重新加载服务器配置。 我认为只需添加资源就足够了,您就可以开始了!

在 Wildfly 内部的一个页面上,它说它必须重新加载配置。所以我做到了,现在一切正常。

抱歉成为那个垃圾。