1

I have successfully configured a JDBC datasource in my JBoss standalone.xml but I am also trying to figure out if it's possible to define the datasource in the web.xml itself, in a standard, container-agnostic way as this answer suggests.

However I am having trouble translating the elements from the JBoss's standalone.xml into what I would need to put in my application's web.xml.

Here's the excerpt from my JBoss standalone.xml:

<datasource jta="false" jndi-name="java:/comp/env/jdbc/sybase/axafusers" pool-name="axafusers" enabled="true" use-ccm="false">
    <connection-url>jdbc:jtds:sybase://localhost:12501/axafusers</connection-url>
    <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
    <driver>sybasejtds</driver>
    <security>
        <user-name>username</user-name>
        <password>secret</password>
    </security>
    <validation>
        <validate-on-match>false</validate-on-match>
        <background-validation>false</background-validation>
    </validation>
    <statement>
        <share-prepared-statements>false</share-prepared-statements>
    </statement>
</datasource>

Defining the data source in the server as above works. However, the approach to define the data source in the web.xml fails as shown below.

Here's what I have placed in my web.xml:

<data-source>
    <name>java:/comp/env/jdbc/sybase/axafusers</name>
    <class-name>net.sourceforge.jtds.jdbcx.JtdsDataSource</class-name>
    <server-name>localhost</server-name>
    <port-number>12501</port-number>
    <database-name>axafusers</database-name>
    <user>username</user>
    <password>secret</password>
    <transactional>false</transactional>
</data-source>

Despite my attempt to accurately copy the various parameters in their corresponding elements (the most error-prone part seems to be translating the connection-url) I am consistently getting Could not create connection messages that are ultimately caused by the following:

Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) [rt.jar:1.8.0_111] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) [rt.jar:1.8.0_111] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) [rt.jar:1.8.0_111] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) [rt.jar:1.8.0_111] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) [rt.jar:1.8.0_111] at java.net.Socket.connect(Socket.java:589) [rt.jar:1.8.0_111] at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288) [jtds-1.3.1.jar:1.3.1] at net.sourceforge.jtds.jdbc.SharedSocket.(SharedSocket.java:251) [jtds-1.3.1.jar:1.3.1] at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:331) [jtds-1.3.1.jar:1.3.1]

Community
  • 1
  • 1
Marcus Junius Brutus
  • 23,022
  • 30
  • 155
  • 282

2 Answers2

0

This exception usually occurs when there is no service listening on the port you are trying to connect to. A couple of things could be happening:

  • You have not started your server.
  • Your server is not waiting to accept connections.
  • You are trying to connect to the wrong port number.

  • Client and Server, either or both of them are not in the network.

  • Server is not running

  • The server is running but not listening on the port, a client is trying to connect.

  • Firewall is not permitted for host-port combination

  • Incorrect protocol in Connection String

Anup Dey
  • 790
  • 4
  • 6
  • Defining the datasource in the server works so it can be none of these things unless I have failed to translate the fields / elements from one format to the other. – Marcus Junius Brutus Dec 31 '16 at 14:58
  • @MarcusJuniusBrutus : don't forget to mark the answer as accepted and upvote it IF it fits for your question! – Anup Dey Jan 07 '17 at 07:50
0

A JDBC datasource must be mapped to a in jboss-web.xml before it can be used in web.xml. For example, this datasource:

src/main/webapp/WEB-INF/jboss-web.xml:

<jboss-web>
       <resource-ref>
        <res-ref-name>jdbc/datasource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:jboss/datasources/ExampleDS</jndi-name>
    </resource-ref>
</jboss-web>

src/main/webapp/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name>JndiDsTest</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <resource-ref>
  <res-ref-name>jdbc/datasource</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
</web-app>

You can invoke the datasource by using simple code snippet:

DataSource ds = null;

Context ctx = null;


String strDSName = "java:comp/env/jdbc/datasource";

ctx = new InitialContext();

ds = (javax.sql.DataSource) ctx.lookup(strDSName);
Anup Dey
  • 790
  • 4
  • 6