How to connect to a DataSource from a remote client?

WildFly and JBoss EAP 6/7 no longer support remote JNDI lookup of datasource objects. If you attempt to lookup a Datasource object from a remote client, the following error will be thrown:

ERROR: org.jboss.remoting3.MessageCancelledException
Exception in thread "main" org.jboss.naming.remote.protocol.NamingIOException: Failed to lookup [Root exception is java.io.NotSerializableException: org.jboss.jca.core.connectionmanager.pool.strategy.OnePool]

As an alternative, it is recommend using an EJB as facade in accessing the data source, and defining a proper contract for how the data should be accessed and managed.

JBoss 5 users

If you are running JBoss As 5 this option is still available, although discouraged. To allow it, you need to tell JBoss NOT to bind the Datasource under the “java:/”
namespace.
As a matter of fact this restricts the lookup to the same VM as the JBoss server.

Simply use’ tag <use-java-context>false</use-java-context> in your -ds.xml file

This is a sample Datasource file for mysql configured to accept remote client access

 <datasources>
      <local-tx-datasource>
        <jndi-name>MySqlDS</jndi-name>
        <use-java-context>false</use-java-context>
        <connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb
        </connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <user-name>x</user-name>
        <password>y</password>

          <metadata>
             <type-mapping>mySQL</type-mapping>
          </metadata>
      </local-tx-datasource>

    </datasources>


Recommended Articles

Retrieve WildFly/JBoss EAP Datasources Using CLI and Programmatically - Expert Guide

Learn how to fetch WildFly/JBoss EAP datasources using CLI and programmatically. Includes JMX API examples.

H2 Console Vulnerability: Remote Class Loading via JNDI and Mitigation

Learn how H2 Console's vulnerability can be exploited through remote class loading. Fixing the issue with updated versions or secure configurations.

Connect to H2 Database from Java: Embedded and Remote Examples

Learn how to connect to an H2 Database in Java with embedded and remote examples. Includes tips for beginners.

JBoss Datasource High Availability Configuration Made Easy

Learn how to configure high availability in JBoss/ WildFly datasources, including multi-data source and Oracle RAC, with step-by-step examples.