7 Replies Latest reply on Oct 24, 2012 1:34 PM by wdfink

    How to define a datasource?

    brenden.t

      In my standalone.xml, I have the following configuration:

       

          <subsystem xmlns="urn:jboss:domain:datasources:1.0">
              <datasources>
                  <datasource jndi-name="java:/comp/env/jdbc/simpletth" pool-name="simpletth" enabled="true" use-java-context="true">
                      <connection-url>jdbc:mysql://localhost/simpletth</connection-url>
                      <driver>com.mysql.jdbc.Driver</driver>
                      <security>
                          <user-name>zzzz</user-name>
                          <password>xxxx</password>
                      </security>
                  </datasource>

       

      I don't know why that pastes as a table.  Sometimes these smart editors aren't so smart.  Anyway...

       

      Does anyone see a problem with that?  I'm trying to connect a persistence unit to this and it's giving me errors.  In particular I'm concerned about the JNDI name, jndi-name="java:/comp/env/jdbc/simpletth" . I think there's some new definitions for JNDI naming and I'm not sure how that line is being interpreted by the <datasource> tag.

       

      Can anyone point me at some good docs/tutorial for datasource definitions in JBoss?


        • 2. Re: How to define a datasource?
          brenden.t

          I appreciate you trying to help out, but I've been through those two sections of the manual/wiki several times now, and I can't get it to work.  There's nothing there about debugging when things go wrong either.

           

          Edit: for example, some older JBoss documentation indicates that <resource-ref> is needed to expose/access global JNDI names, as all JNDI names are local to an application by default.  Is this still the case?

          • 3. Re: How to define a datasource?
            erasmomarciano

            Step by step

             

            1) You download  the driver for your database (if you use mysql need to download mysql-connector-java-5.1.21-bin.jar)

             

            2)  copy your file jar (mysql-connector-java-5.1.21-bin.jar) in $JBOSS_HOME/modules/com/mysql/main

              

                 if you haven't this directory you have to make mkdir -p com/mysql/main

             

            3) You have to make this file module.xml in $JBOSS_HOME/modules/com/mysql/main

             

               

            <?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.21-bin.jar"/>

              </resources>

              <dependencies>

                <module name="javax.api"/>

               </dependencies>

            </module>

             

             

            4) Edit standalone.xml  and add this code

             

               You have to copy this code into the tag <datasources>

                

                    <datasource jta="true" jndi-name="java:/mydb" pool-name="my_pool" enabled="true" use-java-context="true" use-ccm="true">

                                <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url>

                                <driver>mysql</driver>

                                <security>

                                    <user-name>root</user-name>

                                    <password>root</password>

                                </security>

                                <validation>

                                    <check-valid-connection-sql>select 1</check-valid-connection-sql>

                                    <exception-sorter class-name="org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter"/>

                                </validation>

                                <statement>

                                    <prepared-statement-cache-size>100</prepared-statement-cache-size>

                                    <share-prepared-statements>true</share-prepared-statements>

                                </statement>

                            </datasource>

             

             

            5)  Add this code into the tag <drivers>

             

                                <driver name="mysql" module="com.mysql"/>

             

             

            6) Save to standalone.xml

             

            7) Start Jboss

             

             

            You can use the web-console for an test-connection on database

            1 of 1 people found this helpful
            • 4. Re: How to define a datasource?
              erasmomarciano

              If you are also interested for encrypt password for connecting on db you can check here

               

              https://community.jboss.org/wiki/CREATEDATASOURCEENCRYPTAS7WithVAULT

              • 5. Re: How to define a datasource?
                brenden.t

                Thanks for the How-To.  This is what I have already done:

                 

                1.  I've made a module directory for my MySQL connector:

                 

                [jboss@domU-12-31-39-0A-A0-29 main]$ pwd

                /usr/share/jboss-as/modules/com/mysql/main


                [jboss@domU-12-31-39-0A-A0-29 main]$ ls

                module.xml  mysql-connector-java-5.1.22-bin.jar


                [jboss@domU-12-31-39-0A-A0-29 main]$ cat module.xml

                <?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.22.jar"/>

                  </resources>

                  <dependencies>

                      <module name="javax.api"/>

                  </dependencies>

                </module>

                 

                [jboss@domU-12-31-39-0A-A0-29 main]$

                 

                2. I've had already edited my standalone.xml file for a new datasources:

                 

                        <subsystem xmlns="urn:jboss:domain:datasources:1.0">

                            <datasources>

                                <datasource jndi-name="java:/comp/env/jdbc/simpletth" pool-name="simpletth" enabled="true" use-java-context="true">

                                    <connection-url>jdbc:mysql://localhost/simpletth</connection-url>

                                    <driver>mysql</driver>

                                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>

                                    <security>

                                        <user-name>aaaa</user-name>

                                        <password>zzzz</password>

                                    </security>

                                </datasource>

                 

                3. I also added the standalone.xml file for a new driver:

                 

                            <drivers>

                                    <driver name="mysql" module="com.mysql">

                                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>

                                    </driver>

                 

                 

                Now I can see some things I should still look into.

                 

                A. I don't see a deployment message for the datasource.  I should probably look into that first.

                 

                B. I think there might be something up with the JNDI name I'm using.  I noticed that most comp/env names do not start with a slash (mine does).  JNDI might be wigging out when it tries to parse that.

                 

                C. That xa-datasource-class is pretty bogus, I just copied it from the internet.  I should probably remove it, now that your example shows that I can do without it.

                 

                 

                Thanks everyone for your help, hopefully I'm slowly converging on a right answer here.

                • 6. Re: How to define a datasource?
                  brenden.t

                  Well when I remove the / from the JNDI name, I get this:

                   

                   

                  Caused by: javax.xml.stream.XMLStreamException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[99,17]

                  Message: "JBAS010471: Jndi name have to start with java:/ or java:jboss/"

                   

                   

                  during startup.  I guess that explains why I had the slash in there.  Why is this different from every other JNDI name?  I've got a couple of books on JBoss and JPA and neither one uses slashes to start the JNDI name.  It's very confusing.

                   

                  I guess I'll go try to understand how JBoss parses JNDI names now.

                   

                   

                  Edit:  When I add the slash back in I get this during server startup:

                   

                  20:35:47,006 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]

                  20:35:47,241 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report

                  JBAS014775:    New missing/unsatisfied dependencies:

                        service jboss.jdbc-driver.mysql (missing) dependents: [service jboss.data-source.java:/comp/env/jdbc/simpletth]

                   

                  Kind of frustrating.

                   

                   

                  Edit 2: If I do something like the examples above, where the JNDI name is just a string with a slash in front, I STILL get an error.  Extremely frustrating.

                   

                  21:41:25,843 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]

                  21:41:26,082 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report

                  JBAS014775:    New missing/unsatisfied dependencies:

                        service jboss.jdbc-driver.mysql (missing) dependents: [service jboss.data-source.java:/simpletth]

                  • 7. Re: How to define a datasource?
                    wdfink

                    Your problem is a wrong module.xml

                    1.  I've made a module directory for my MySQL connector:

                     

                     

                    [jboss@domU-12-31-39-0A-A0-29 main]$ ls

                    module.xml  mysql-connector-java-5.1.22-bin.jar

                     

                    [jboss@domU-12-31-39-0A-A0-29 main]$ cat module.xml

                    <?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.22.jar"/>

                      </resources>

                      <dependencies>

                          <module name="javax.api"/>

                      </dependencies>

                    </module>

                     

                    JAR file is : mysql-connector-java-5.1.22-bin.jar

                    and module.xml :  <resource-root path="mysql-connector-java-5.1.22.jar"/>

                     

                    You should use the correct name!

                     

                    If I add the datasource and driver you pasted above I see:

                     

                    INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)

                    INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)

                    ....

                    INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:/comp/env/jdbc/simpletth]

                    INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:jboss/datasources/Examp