Thursday, November 3, 2016

Add another datasource to your WildFly.

To create my own database and deploy in on my WildFly serve I will use PostgreSQL sql system.
I already installed Postgres Server, Pgadmin to manage your database and StackBuilder - all in one from official download page.
I installed two servers: one on the 5432 port and one on the 5433. the last one I started to use and added there new database cats, there I creater a schema list where I put the table cats.
There are two variants how to include database to wildfly server: as deployment or as module. I will add my datasource as a module.

To do it I create a folder named postgres in %WILDFLY_HOME%\modules\system\layers\base\org wherewe create a new folder main and file module.xml inside this folder. Also download jdbs postgres driver
In module.xml I configure access to our driver:
1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <module xmlns="urn:jboss:module:1.1" name="org.postgres">  
3:    <resources>  
4:      <resource-root path="postgresql-9.4.1211.jar"/>  
5:    </resources>  
6:    <dependencies>  
7:      <module name="javax.api"/>  
8:      <module name="javax.transaction.api"/>  
9:      <module name="javax.servlet.api" optional="true"/>  
10:    </dependencies>  
11:  </module>  


Then I modify standalone.xml which I can find in %WILDFLY_HOME%\standalone\configuration.
There, between the tags 
 <datasources> </datasources>  
we describe our custom datasource:
1:      <subsystem xmlns="urn:jboss:domain:datasources:4.0">  
2:        <datasources>  
3:          <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">  
4:            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>  
5:            <driver>h2</driver>  
6:            <security>  
7:              <user-name>sa</user-name>  
8:              <password>sa</password>  
9:            </security>  
10:          </datasource>  
11:          <datasource jta="false" jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">  
12:            <connection-url>jdbc:postgresql://localhost:5433/cats</connection-url>  
13:            <connection-property name="cats">dictionary</connection-property>  
14:            <driver>postgres</driver>  
15:            <security>  
16:                 <user-name>postgres</user-name>  
17:                 <password>postgres</password>  
18:            </security>  
19:          </datasource>  
20:          <drivers>  
21:            <driver name="postgres" module="org.postgres">  
22:                 <datasource-class>org.postgresql.Driver</datasource-class>  
23:                 <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>  
24:            </driver>  
25:                 <driver name="h2" module="com.h2database.h2">  
26:             <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>  
27:            </driver>  
28:          </drivers>  
29:        </datasources>  
30:      </subsystem>  

Here we see both: ExampleDS and our custom PostgreDS. There I combined datasources separately from drivers declarations and those declarations I put at the end of the datasources div as you can see.
Connection URL is where our datasource is. Connection-property we need since wildfly 10.1.0, here we set up the name of our database and the name of our application.

Now if we  start our server
Open your command prompt:

      a. Push WIN ΓΏ button on your keyboard, in appeared window type "cmd", then ENTER.
     or

      b. Go "Start" -> Search -> type "cmd" -> choose "Command prompt".
then type
%WILDFLY_HOME%\bin\standalone.bat
we will find our new datasource:
Next time I will try to lookup data from this datasource to my servlett and update datasource from application.

No comments:

Post a Comment