How can we configure database connection from JBOS

2019-03-21 02:30发布

问题:

Can we configure database connection from JBOSS? If it is possible, than is there any configuration file in JBOSS to configure database connection from JBOSS?

回答1:

You need two things:

  1. Make the JDBC driver available to your Applications Server
  2. Write a Data Source configuration

For #1, you can download the JAR containing JDBC driver and put it in the following directory:

$JBOSS_HOME/server/default/lib

Assuming that $JBOSS_HOME points to your JBoss installation, and you are using default installation.

For #2, you will find a lot of examples here:

$JBOSS_HOME/docs/examples/jca

There are examples for most of database products around. Here's PostgreSQL's:

<datasources>
  <local-tx-datasource>
    <jndi-name>PostgresDS</jndi-name>
    <connection-url>jdbc:postgresql://[servername]:[port]/[database name]</connection-url>
    <driver-class>org.postgresql.Driver</driver-class>
    <user-name>x</user-name>
    <password>y</password>
        <!-- sql to call when connection is created.  Can be anything, select 1 is valid for PostgreSQL
        <new-connection-sql>select 1</new-connection-sql>
        -->

        <!-- sql to call on an existing pooled connection when it is obtained from pool.  Can be anything, select 1 is valid for PostgreSQL
        <check-valid-connection-sql>select 1</check-valid-connection-sql>
        -->

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

</datasources>


回答2:

Well, this looks like a good guide for configuring a datasource for JBoss.

A JDBC driver is needed for your database (a jar file, refer to your db's documentation) and afterwards, configuration. You should be able to configure it using JBoss admin console.

After your datasource is configured, you can use JNDI or some other mechanism for obtaining it in your application. Then you use JDBC for actually interacting with your database. Another popular alternative is using JPA since JBoss already has Hibernate built in.



回答3:

Follow the following steps:

  1. Add my sql connector jar in main folder (eg. \modules\system\layers\base\com\mysql\main).

  2. Add a module.xml file to this folder consisting of the following config:

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

    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.servlet.api" optional="true"/>
    </dependencies>
</module>
  1. Now in stanalone.xml file add mysql driver under drivers tag as following:
<driver name="mysql" module="com.mysql">
    <driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
  1. start the jboss server from command prompt by going to location of the jboss/bin folder and executing standalone.bat

  2. Now in a browser, open localhost:8080, click on administration console and under create a datasource click datasource. Then click on add. Add the following details:

Name: MysqlDS5

JNDI Name : java:/mysql

click next and under "detected driver" select mysql.

click next

Connection url : jdbc:mysql://localhost:3306/sampledb

UserName:****

Password:****

click done, select the MysqlDS and click on enable.

click on test connection and it will successfully connect.