0
votes

im trying to create a new datasource and "lookup" from java code. First i created folder, jboss-as-7.1.0.Final\modules\com\mysql\main and copy to there 2 files. mysql-connector-java-5.1.14-bin and module.xml My module.xml contains

 <?xml version="1.0" encoding="UTF-8"?>

<module xmlns="urn:jboss:module:1.0" name="com.mysql">
  <resources>
    <resource-root path="mysql-connector-java-5.1.14-bin.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
  </dependencies>
</module>

Second i added in file standalone.xml,this..

 <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://127.0.0.1:3306/myTable</connection-url>
                <driver>mysql</driver>
                <security>
                    <user-name>root</user-name>
                    <password>root</password>
                </security>
            </datasource>
            <drivers>
                <driver name="mysql" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                </driver>

And last, i tried to call it from java code like this:

DataSource ds = (DataSource) ctx.lookup("jboss/datasources/MySqlDS");

            connection = ds.getConnection();

But got this error..

javax.naming.NameNotFoundException: jboss/datasources/MySqlDS -- service jboss.naming.context.java.jboss.datasources.MySqlDS

Anyway,when i started my jboss server i got and error,too New missing/unsatisfied dependencies: service jboss.jdbc-driver.mysql (missing) dependents: [service jboss.data-source.java:/MySqlDS]

2

2 Answers

0
votes

Please try using

DataSource ds = (DataSource) ctx.lookup("java:jboss/datasources/MySqlDS");
0
votes

It's probably happened because the Data Source is not configured properly. I had a similar issue but when I configured it using JBoss Admin Console, it worked. Also try to check if you have .jar extension in your module.xml.

Try following this link step by step. It works perfectly.