Pages

Tuesday, November 15, 2016

How to configure DataSource in WildFly 8.2

          WildFly, formerly known as JBoss Application server. It is a flexible, lightweight, managed application run-time that helps you to build applications.
          Wildfly has its own new identity, because of its new Web Application server that is Undertow. Whereas JBoss Web server designed for medium and large application based on Tomcat.

  • It is flexible because undertow has ability to add as much or as little as functionality you need. 
  • It is lightweight because it's core jar coming in at under 1Mb. It is lightweight at runtime too, with a simple embedded server using less than 4Mb of heap space. 
  • And last its managed application at runtime because of its powerful administration. All configurations are not limited file edits, it has very good user friendly administration console through which many configurations are done easily on screen.

Here in this tutorial we will see basic installation steps of WildFly and ways to create datasource.

Installation steps of wildFly:
  1. Extract the zip archive to a directory on your computer. You will see following folder structure:
  2.  Get the stable version of the WildFly Application Server 8.2.
  3. Use the script <WildFly directory>\bin\standalone.bat to start the WildFly server.
  4. After startup if you get below message on console
        JAVA_HOME is not set. Unexpected results may occur.
        Set JAVA_HOME to the directory of your local JDK to avoid this message.

    Then write following line into <WildFly directory>\bin\standalone.conf.bat file
    set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_67"Here instead of C drive write down full path of your directory where jdk is installed.
  5. After server startup hit the following URL http://localhost:8080  
  6. Open the link Administration Console and follow the instructions to add a new management user. After creating a user revisit the Administration Console.

Three Ways to add datasource into WildFly

1. Steps to add DataSource in Wildfly using admin console
  1. Before creating datasource we have to deploy driver to use for connecting to database.
    For that go to admin console http://localhost:9990/console
    Then go to Deployments > and click on Add


    Here we will add MySQL driver.
  2. Upload MYSQL driver jar file. Which can be downloaded from here.

  3. give suitable name to connector deployment and click on enable to enable the deployment.

  4. Here you can see deployment in list
  5. Now proceed to create new DataSource.
    Go to Configurations > Datasources > Click on Add

  6. Give suitable name to Datasource which can be easily find on console.
    And Define JNDI name appending with prefix java:/ and then click next.

  7. Select the driver which you have deployed and enabled. Click on next.
  8. On connection parameter screen enter details and click on Test Connection.

  9. You will get message on successful Test Connection.


2. Steps to add datasource manually.

  1. Enter into the path <WildFly directory>/modules/system/layer/base
    Where you can see this folder structure
  2. Then create directories com/mysql/driver/main
    After that copy driver library into main folder

  3. Create module.xml with following content into the same folder
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.3" name="com.mysql.driver">
       <resources>
          <resource-root path="mysql-connector-java-5.1.40-bin.jar" />
       </resources>
       <dependencies>
          <module name="javax.api" />
          <module name="javax.transaction.api" />
       </dependencies>
    </module>

    Here you can see module.xml
  4. Register datasource into standalone.xml
    • Go to  <WildFly directory>/standalone/configuration/
    • Then open xml file and add new datasource as shown below into existing subsystem.
      <?xml version="1.0" encoding="UTF-8"?>
      <subsystem xmlns="urn:jboss:domain:datasources:2.0">
      <datasources>
       <datasource jndi-name="java:jboss/datasources/MySqlDS" 
       pool-name="MySqlDS" enabled="true" use-java-context="true">
          <connection-url>jdbc:mysql://localhost:3306/myapp 
          </connection-url>
               <driver>mysql</driver>
               <security>
                  <user-name>myapp</user-name>
                  <password>myapp</password>
               </security>
            </datasource>
            <drivers>
              <driver name="mysql" module="com.mysql.driver">
       <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
       </xa-datasource-class>
              </driver>
            </drivers>
      </datasources>
      </subsystem> 

3. How to create DS file into wildly?

  • Go to <WildFly directory>/standalone/deployments
  • Create datasource xml with following content

<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
      <datasource jndi-name="APP_DS" pool-name="APP_DS" enabled="true" use-ccm="false">
        <connection-url>jdbc:mysql://localhost:3306/DB_NAME</connection-url>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <driver>mysql</driver>
      
        <!-- sql to call when connection is created -->
        <new-connection-sql>SELECT 1</new-connection-sql>
      
        <pool>
            <min-pool-size>5</min-pool-size>
            <max-pool-size>50</max-pool-size>
        </pool>
      
        <security>
          <user-name>username</user-name>
          <password>password</password>
        </security>
      
        <!-- sql to call on an existing pooled connection when it is obtained from pool -->
        <validation>
        <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
        </validation>
      
        <timeout>
        <blocking-timeout-millis>300000</blocking-timeout-millis>
        <idle-timeout-minutes>5</idle-timeout-minutes>
        </timeout>
        <statement>
        <track-statements>true</track-statements>
        </statement>      
      </datasource>
    </datasources>


No comments:

Post a Comment