JBoss.org Community Documentation

B.3. Creating a DataSource for the External Database

JBoss AS connects to relational databases via datasources. These datasource definitions can be found in the jboss-as/server/production/deploy directory. The datasource definitions are deployable just like WAR and EAR files. The datasource files can be recognized by looking for the XML files that end in *-ds.xml.

The datasource definition files for all supported external databases can be found in the jboss-as/docs/examples/jca directory.

  • MySQL: mysql-ds.xml

  • PostgreSQL: postgres-ds.xml

  • Oracle: oracle-ds.xml

  • DB2: db2-ds.xml

  • Sybase: sybase-ds.xml

  • MS SQL Server: mssql-ds.xml

The following code snippet shows the mysql-ds.xml file as an example. All the other *-ds.xml files are very similiar. You will need to change the connection-url, as well as the user-name / password, to fit your own database server installation.


<datasources>
  <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/jboss</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>jbossuser</user-name>
    <password>jbosspass</password>
    <exception-sorter-class-name>
			org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
		</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    <valid-connection-checker-class-name>
			org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker
		</valid-connection-checker-class-name>
    -->
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - 
		MySQLValidConnectionChecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>

</datasources>

    

Once you customized the *-ds.xml file to connect to your external database, you need to copy it to the jboss-as/server/production/deploy directory. The database connection is now available through the JNDI name specified in the *-ds.xml file.