Adding a datasource to wildfly via web console

1.4k views Asked by At

I have searched for days now and cant get any further. I am trying to assign a MySQL datasource to my Wildfly 9 server via web console. If I add the resource I'll get an error in testing the resource due a bug in wildfly 9 which creates the resource with a max-pool-size of 0. So i edited it by myself and everything seems fine. Now when i try to lookup that resource in my webapp, Its just a classic servlet application which produces some json data, i get an error. javax.naming.NameNotFoundException. The jndi-name of the resource is : java:/MySQLDS

This is the XML generated via web console:

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

In my app I have a class which manages my connections in a hashmap for various reasons.

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

This produces the error above.

Now the strange part: I removed the resource from web console and added exactly the same resource via a *-ds.xml file in my WEB-INF folder and it works!

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

The question is how can I add resources via web console and find in my app? And why i am able to find the resource if i add the resource via xml but not via web console?

2

There are 2 answers

3
Maurice Mohlek On BEST ANSWER

Okay, now I feel retarded..

I simply had to reload the server configuration. I though it would be enough to just add the resource and you are ready to go!

On a single page deep inside Wildfly it said it has to reload the configuration. So I did and now everything works.

Sorry for being that dump.

0
serg.nechaev On

This is my datasource, it works fine (created via web console).

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