Skip to main content

How to configure Microsoft SQL Server in EAP/Wildfly



Wednesday, December 11, 2019
9:19 AM
  1. Download the appropriate jdbc driver from https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
  2. Extract the driver package to the modules folder under EAP
    1. Here is from where my EAP is running C:\Users\admin\EAP-7.0.0\
    2. Create a folder named "sqlserver" under C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft
    3. Copy the extracted driver jar file (for example mssql-jdbc-6.4.0.jre8.jar) to C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft\main
  3. Now create a modules.xml file in  C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft\main with the following settings
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.microsoft.sqlserver">
    <resources>
        <resource-root path="mssql-jdbc-6.4.0.jre8.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>
  1. Now, you need to tell EAP/Wildfly about the new driver and you can do that by modifying the configuration xml.
    1. By default the configuration xml is "standalone.xml" which you can find in the folder C:\Users\admin\EAP-7.0.0\standalone\configuration
    2. Go to "drivers" node under "<subsystem xmlns="urn:jboss:domain:datasources:4.0">" and add the new driver as shown below (You can see the default h2 driver under this section)
<driver name="sqlserver" module="com.microsoft.sqlserver">
       <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
 </driver>
  1. Restart EAP/Wildfly server and you can see the below line in log and this confirms that the JBOSS server detected the new driver
    1. WFLYJCA0004: Deploying JDBC-compliant driver class com.microsoft.sqlserver.jdbc.SQLServerDriver (version 6.4)
  2. Now let us see how to configure a SQL server as a data source in EAP/Wildfly


    1. Connect to JBOSS administration console (For eg: http://localhost:9990/console/App.html#home)
    2. Go to Configuration -> Subsystems -> Datasources -> Non-XA and Click on Add
    3. Give a Name and JNDI name your data source
    4. Specify the driver details as shown in Image 2
    5. Provide the connection settings as shown in image 3. In case of local machine use 'localhost', otherwise use the server where SQL server is installed.
    6. Make to provide valid user name and password and also make sure the database is created in SQL server
    7. Once this is done, click on view the data source and test the connection and make sure it succeeds. If there is a connection problem try restarting the JBOSS server
  1. Follow the below steps to configure the persistence.xml
    1. Adding the following entry into perisistence.xml
<persistence-unit name="SQLDB" transaction-type="JTA">
        <jta-data-source>java:/SQLDB</jta-data-source>
        <properties>
                    <property name="javax.persistence.jdbc.driver" value="sqlserver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://WIN-FSSP8DPSML0:1433;DatabaseName=SQLDB"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
</properties>
    </persistence-unit>
  1. And this is how to inject the PersistenceUnit
@PersistenceContext(unitName="SQLDB")
 private EntityManager entityManager;

Comments

Popular posts from this blog

Base64 Encoding

The base-64 encoding converts a series of arbitrary bytes into a longer sequence of common text characters that are all legal header field values. Base-64 encoding takes a sequence of 8-bit bytes, breaks the sequence into 6-bit pieces, and assigns each 6-bit piece to one of 64 characters comprising the base-64 alphabet. Base 64–encoded strings are about 33% larger than the original values. For example “Ow!” -> “T3ch” 1. The string “Ow!” is broken into 3 8-bit bytes (0x4F, 0x77, 0x21). 2. The 3 bytes create the 24-bit binary value 010011110111011100100001. 3. These bits are segmented into the 6-bit sequences 010011, 110111, 01110,100001.

Unicode and UTF8 Encoding

Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. Unicode officially encodes 1,114,112 characters, from 0x000000 to 0x10FFFF. (The idea that Unicode is a 16-bit encoding is completely wrong.) For maximum compatibility, individual Unicode values are usually passed around as 32-bit integers (4 bytes per character), even though this is more than necessary. The consensus is that storing four bytes per character is wasteful, so a variety of representations have sprung up for Unicode characters. The most interesting one for C programmers is called UTF-8. UTF-8 is a "multi-byte" encoding scheme, meaning that it requires a variable number of bytes to represent a single Unicode value. Given a so-called "UTF-8 sequence", you can convert it to a Unicode value that refers to a character. http://www.cprogramming.com/tutorial/unicode.html There are 3 types of encoding in unicode, UT