JBoss EAP configures a PostgreSQL data source for an application project

Source: Internet
Author: User
Tags postgresql module jboss


First, preface


Customers are planning to migrate their business to the cloud, and these days are testing the replacement of the database by Oracle with open source Postgrsql. Why Choose PostgreSQL? Because this is the least-modified scenario for application code modification, there is no one!



The PostgreSQL database's brief information is: Server Loacalhost, Port 5432, database projadm, username/password: projadm/proj2013.



Before starting, in https://jdbc.postgresql.org/ download.html downloads the JDBC driver package that matches the JBoss and JDK versions, storing a directory (preferably without Chinese characters or special characters, since the subsequent use of JBOSS-CLI is too bad for copy and paste), assuming D:\PostgreSQL\ Postgresql-42.1.4.jar.


Second, JBoss EAP configuration


1) Set the environment variable Jboss_home



If more than one JBoss is installed on the computer, this step is not negligible, otherwise the next step changes the configuration of the JBoss instance that the environment variable refers to, and most likely it is not the desired application item. assumed to be D:\JBOSS-EAP-7.0.



2) Open a CMD window and start JBoss (assuming standalone mode):



CD D:\JBOSS-EAP-7.0\bin



Standalone.bat



3) also open a CMD window and add the PostgreSQL data source related settings on the JBOSS-CLI command line:



CD D:\JBOSS-EAP-7.0\bin



Jboss-cli.bat



After entering the JBOSS-CLI command line mode, execute:


#Connect jboss instance
connect
# Add PostgreSQL module
module add --name = com.postgresql --resources = D: \ PostgreSQL \ postgresql-42.1.4.jar --dependencies = javax.api, javax.transaction.api
# Add jdbc-driver driver description
/ subsystem = datasources / jdbc-driver = postgresql: add (driver-name = postgresql, driver-module-name = com.postgresql, driver-xa-datasource-class-name = org.postgresql.xa.PGXADataSource)
# Add jndi description
data-source add --name = PostgresDS --jndi-name = java: jboss / PostgresDS --driver-name = postgresql --connection-url = jdbc: postgresql: // localhost: 5432 / projadm --user-name = projadm --password = proj2013 --validate-on-match = true --background-validation = false --valid-connection-checker-class-name = org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker- -exception-sorter-class-name = org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter
#Delete the Oracle data source (OracleDS)
xa-data-source remove --name = OracleDS 
Third, inspection


Opening D:\JBOSS-EAP-7.0\standalone\configuration\standalone.xml with a text editor will find descriptive information about the Jndi and driver of PostgreSQL, which is roughly the following:


 
 
<profile>
…
<subsystem xmlns="urn:jboss:domain:datasources:4.0">
    <datasources>
        <datasource jndi-name="java:jboss/PostgresDS" pool-name="PostgresDS">
          <connection-url>jdbc:postgresql://localhost:5432/projadm</connection-url>
          <driver>postgresql</driver>
          <security>
              <user-name>projadm</user-name>
              <password>proj2013</password>
          </security>
          <validation>
              …
          </validation>
       </datasource>
       <drivers>
          …
          <driver name="postgresql" module="com.postgresql">
          <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
          </driver>
       </drivers>
    </datasources>
 </subsystem>        
…
</profile>


In addition, the D:\JBoss-EAP-7.0\modules\com directory has a multiple-out PostgreSQL folder, under which the main subfolder contains the driver package Postgresql-42.1.4.jar and the Module.xml configuration file, which is roughly the following:


    <resources>
        <resource-root path="postgresql-42.1.4.jar"/>
    </resources>

    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
Iv. Application Changes


This section will vary greatly depending on the framework you are using, listing only a few of the items involved.


    • MAVEN engineering modifies Pom to increase dependency on driver files:
 <dependency>  
        <groupId>org.postgresql</groupId>  
        <artifactId>postgresql</artifactId>  
        <version>${postgresql.version}</version>  
    </dependency>  
    • In some cases, it may be necessary to manually copy the driver files Postgresql-42.1.4.jar to the appropriate LIB directory;
    • The JPA project modifies the Persitense.xml to replace the original Oracle-related sections:
 
<persistence-unit name="jpaUnit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- <jta-data-source>java:jboss/datasources/HSOracleDS</jta-data-source> -->
    <jta-data-source>java:jboss/PostgresDS</jta-data-source>
    <properties>
        <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect" /> -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
    …
    • Other configurations involved in the application, specific analysis of the project;
    • In Java code, except for some obvious things (such as using the NVL function, the Decode function, the sysdate), the more troubling is the type matching. Because PostgreSQL requires strict type matching, unlike Oracle, which implicitly converts the implicit conversions between decimal and varchar, decimal and Boolean, you need to modify the Java code or adjust the table field type.


JBoss EAP configures a PostgreSQL data source for an application project


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.