1.
download and copy the database driver libraries
For Oralce, search ojdbc from Oracle, https://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html
For MS SQL Server, https://www.microsoft.com/en-us/download/details.aspx?id=56615
For MySQL, https://dev.mysql.com/downloads/connector/j/
For PostgreSQL, https://jdbc.postgresql.org/download.html
2.
install and configure the database drivers in Wildfly/JBoss system modules
For Oracle, copy ojdbc?.jar (e.g. ojdbc7.jar) to
%JBOSS_HOME%/modules/system/layers/base/com/oracle/main (create folders when necessary). Create a configuration file, module.xml in the same folder with the driver file.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="
com.oracle">
<resources>
<resource-root path="
ojdbc5.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
For MS SQL Server, copy mssql-jdbc-x.x.x.jre?.jar (e.g. mssql-jdbc-6.2.2.jre7) into
%JBOSS_HOME%/modules/system/layers/base/com/microsoft/sqlserver/main. Create a configuration file, module.xml in the same folder with the driver file.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="
com.microsoft.sqlserver">
<resources>
<resource-root path="
mssql-jdbc-6.2.2.jre7.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.xml.bind.api"/>
</dependencies>
</module>
For MySQL, copy mysql-connector-java-x.x.x.jar (e.g. mysql-connector-java-5.1.44.jar) into
%JBOSS_HOME%/modules/system/layers/base/com/mysql/main. Create a configuration file, module.xml in the same folder with the driver file.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="
com.mysql">
<resources>
<resource-root path="
mysql-connector-java-5.1.44.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
For PostgreSQL, copy postgresql-x.x.x.jre?.jar (e.g. postgresql-9.4.1209.jre6.jar) into
%JBOSS_HOME%/modules/system/layers/base/org/postgresql/main. Create a configuration file, module.xml in the same folder with the driver file.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="
org.postgresql">
<resources>
<resource-root path="
postgresql-9.4.1209.jre6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
3. Configure the database drivers and data sources in Wild/JBoss Application Server.
Open and update the Wildfly/JBoss configuration file (e.g. standalone.xml) by adding the database driver and data source information like the following: (Note: make sure the driver name, module name and etc. match with the configuration previously mentioned)
<subsystem xmlns="urn:jboss:domain:datasources:2.0">
<datasources>
... ...
<!-- // Connection Pool of Oracle. java:/jdbc/myoracle is used in applications -->
<datasource jndi-name="
java:/jdbc/myoracle" pool-name="
myOraclePool" enabled="true" use-java-context="true">
<connection-url>
jdbc:oracle:thin:@server_name:1521:SID</connection-url>
<driver>
oracle_driver</driver>
<security>
<user-name>
db_user_name</user-name>
<password>
db_user_password</password>
</security>
</datasource>
<!-- // Connection Pool for MS SQL Server Express -->
<datasource jndi-name="
java:/jdbc/MsSQLSvrDS" pool-name="
MsSQLPool" enabled="true" use-java-context="true">
<connection-url>
jdbc:sqlserver://server_name\SQLEXPRESS:54126;databaseName=TestDB;integratedSecurity=true;</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
<driver>
sqlserver_driver</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>10</max-pool-size>
</pool>
<security>
<user-name>
db_user_name</user-name>
<password>
db_user_password</password>
</security>
</datasource>
<!-- // Connection Pool for MySQL -->
<datasource jndi-name="
java:/jdbc/MySQLDS" pool-name="
MySQLDS" enabled="true" use-java-context="true">
<connection-url>
jdbc:mysql://server_name:3306/my_test_db</connection-url>
<driver>
mysql_driver</driver>
<pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
</pool>
<security>
<user-name>
db_user_name</user-name>
<password>
db_user_password</password>
</security>
</datasource>
<!-- // Connection Pool for MySQL -->
<datasource jta="false" jndi-name="
java:/jdbc/PostgreSQLDS" pool-name="
PostgreSQLDS" enabled="true" use-ccm="false">
<connection-url>
jdbc:postgresql://server_name:5432/my_test_db</connection-url>
<driver-class>
org.postgresql.Driver</driver-class>
<driver>
postgresql_driver</driver>
<pool>
<min-pool-size>1</min-pool-size>
<max-pool-size>5</max-pool-size>
</pool>
<security>
<user-name>
db_user_name</user-name>
<password>
db_user_password</password>
</security>
</datasource>
<drivers>
... ...
<!-- // Oracle Driver -->
<driver name="
oracle_driver" module="
com.oracle">
<driver-class>
oracle.jdbc.OracleDriver</driver-class>
</driver>
<!-- // MS SQL Server Driver -->
<driver name="
sqlserver_driver" module="
com.microsoft.sqlserver">
<driver-class>
com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
</driver>
<!-- // MySQL Driver -->
<driver name="
mysql_driver" module="
com.mysql">
<driver-class>
com.mysql.jdbc.Driver</driver-class>
</driver>
<!-- // PostgreSQL Driver -->
<driver name="
postgresql_driver" module="
org.postgresql">
<driver-class>
org.postgresql.Driver</driver-class>
</driver>
</drivers>
</datasources>
</subsystem>
4.
Start the Wildfly/JBoss application server and verify the database connectivities from the server logs.
5.
Use the connection pool for JPA.
Find and update persistence.xml as following in the application folder,
/src/main/resources/META-INF/persistence.xml.
<persistence-unit name="
Persistence_Unit_Name">
<!-- referenced in application //-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>
PostgreSQLDS</jta-data-source>
<!-- // for example of PostgreSQL datasource defined in Wildfly/JBoss -->
<properties>
<property name="hibernate.dialect" value="
org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="false" /> <!-- Debugging purpose //-->
<property name="hibernate.generate_statistics" value="false"/> <!-- Debugging purpose //-->
</properties>
</persistence-unit>
If not use Connection Pool defined in Wildfly/JBoss application server, need to specify the database connection information in preperties as well.
<persistence-unit name="
Persistence_Unit_Name" transaction-type="RESOURCE_LOCAL"> <!-- referenced in application //-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="
jdbc:postgresql://server_name:5432/my_test_db" />
<property name="javax.persistence.jdbc.user" value="
db_user_name" />
<property name="javax.persistence.jdbc.password" value="
db_user_password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
</properties>
</persistence-unit>