有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我已经找了好几天了,再也找不到了。 我试图通过web控制台将MySQL数据源分配给我的Wildfly 9服务器。 如果我添加资源,我将在测试资源时出错,因为wildfly 9中存在一个错误,该错误创建了最大池大小为0的资源。 所以我自己编辑了它,一切似乎都很好。 现在,当我试图在我的webapp中查找该资源时,它只是一个生成一些json数据的经典servlet应用程序,我得到了一个错误。 javax。命名。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>

在我的应用程序中,我有一个类,出于各种原因在hashmap中管理我的连接

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控制台中删除了该资源,并通过*-ds添加了完全相同的资源。xml文件在我的WEB-INF文件夹中,它可以工作

<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>

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


共 (2) 个答案

  1. # 1 楼答案

    好吧,现在我觉得自己很迟钝

    我只需要重新加载服务器配置。 我觉得只要添加资源就足够了,你就可以开始了

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

    对不起,我是个垃圾

  2. # 2 楼答案

    这是我的数据源,运行良好(通过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>