Jboss 7 EPA datasource configuration using oracle and spring boot

In this post, we are going to see Jboss 7 EPA Datasource configuration using oracle and spring boot. Here we are going to create a Spring boot application. As we know Spring boot provides embedded tomcat for deployment purpose but here we are going to use JBoss 7 for deployment. The JBoss configuration steps will be the same even for other databases.

We can define datasource in standalone.xml as following.

<datasources>
<datasource jndi-name=”java:jboss/datasources/BootWarDeployment” pool-name=”BootWarDeployment” enabled=”true” use-java-context=”true”>
<connection-url>jdbc:oracle:thin:@localhost:1521:XE</connection-url>
<driver>oracle</driver>
<security>
<user-name>SYSTEM</user-name>
<password>oracle1</password>
</security>
</datasource>
<drivers>
<driver name=”oracle” module=”com.oracle.jdbc”>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>

 

Let’s see an example where we will see how to modify standalone.xml and how to create module.xml which are needed for JNDI Datasource configuration.

Jboss 7 EPA Datasource configuration using oracle and spring boot example.

Prerequisites –

  • JDK 1.8
  • Oracle 10g
  • Eclipse
  • maven
  • JBoss 7 EPA

 

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – BootWarDeployment, ArtifactId – BootWarDeployment and name – BootWarDeployment) and click on finish. Keep packaging as the jar.

Open pom.xml  and modify the pom.xml with below code.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>BootWarDeployment</groupId>
  <artifactId>BootWarDeployment</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>BootWarDeployment</name>
  <packaging>war</packaging>
  
  <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>

     <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
                <executable>C:\Program Files\Java\jdk1.8.0_131\bin\javac.exe</executable>
            </configuration>
        </plugin>
        

</plugins>
</build>
</project>

 

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

Let maven download all necessary jar. Once it is done we will able to see the maven dependency folder which contains different jar files. Create classes and interfaces as below.

Jboss 7 EPA datasource configuration using oracle and spring boot


 

Book.java

package com.bootwar.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "book")
public class Book {
	@Id
	 @GeneratedValue(strategy = GenerationType.AUTO)
	 private int bookId;
	 
	 @Column(name="book_name")
	 private String bookName;

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	
	
 
}

 

BookService.java

package com.bootwar.service;

import com.bootwar.entity.Book;

public interface BookService {
	public Book findByBookId(int bookId);
}

 

BookRepository.java

package com.bootwar.repository;

import java.io.Serializable;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.bootwar.entity.Book;

@Repository
public interface BookRepository extends CrudRepository<Book,Serializable> {
	public Book findByBookId(int bookId);
}

 

BookServiceImpl.java

package com.bootwar.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootwar.entity.Book;
import com.bootwar.repository.BookRepository;
import com.bootwar.service.BookService;

@Service("bookServiceImpl")
public class BookServiceImpl implements BookService{
	
@Autowired
private BookRepository bookRepository;
	
	public Book findByBookId(int bookId) {
	Book book = bookRepository.findByBookId(bookId);
	return book;
	}
}

 

BookController.java

package com.bootwar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bootwar.entity.Book;
import com.bootwar.service.BookService;
 

 
@RestController
@RequestMapping(value = "/book")
public class BookController {
	
	@Autowired
	private BookService bookService;
	
	@RequestMapping(value = "/getBook/{bookId}",method = RequestMethod.GET)
    @ResponseBody
    public Book getPassengerDetails(@PathVariable int bookId) {
		Book bookResponse = bookService.findByBookId(bookId);
		
		return bookResponse;
	}
	
}

 

SpringMain.java

package com.bootwar.main;



import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.bootwar.*")
@SpringBootApplication
public class SpringMain extends SpringBootServletInitializer{
	
	@Override
    public SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringMain.class);
    }

	public static void main(String[] args) {
		System.out.println("bean2");
        SpringApplication.run(SpringMain.class, args);
    }
	
	
	
	
	
}

 

AppConfiguration.java


package com.bootwar.config;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories(basePackages = "com.bootwar.repository")
@EntityScan("com.bootwar.entity")
public class AppConfiguration {
	@Bean
	public DataSource dataSource() {
		DataSource dataSource = null; 
		System.out.println("value of datasource"+dataSource);
		try {
	   
	    Context initialContex = new InitialContext();
	    System.out.println("value of datasource"+dataSource);
	    
	    dataSource = (DataSource)(initialContex.lookup("java:jboss/datasources/BootWarDeployment"));
	    
	    System.out.println("value of datasource"+dataSource);
	    
	    if(dataSource != null) {
	    	dataSource.getConnection();
		    
	    }
	    
		}catch(Exception e) {
			e.printStackTrace();
		}
	   
	    return dataSource;
	}
}

 

We are done with the coding part. Now give the build use mvn clean install. In target folder we should have war file with name BootWarDeployment.war.

Jboss 7 EPA datasource configuration using oracle and spring boot

 

 

We have BootWarDeployment.war now. Let’s deploy the war file using JBoss.

Download JBoss and modify the standalone.xml. Here we are going to configure datasource. we will provide database details.


standalone.xml.

 

<subsystem xmlns=”urn:jboss:domain:bean-validation:1.0″/>
<subsystem xmlns=”urn:jboss:domain:core-management:1.0″/>
<subsystem xmlns=”urn:jboss:domain:datasources:5.0″>
<datasources>
<datasource jndi-name=”java:jboss/datasources/BootWarDeployment” pool-name=”BootWarDeployment” enabled=”true” use-java-context=”true”>
<connection-url>jdbc:oracle:thin:@localhost:1521:XE</connection-url>
<driver>oracle</driver>
<security>
<user-name>SYSTEM</user-name>
<password>oracle1</password>
</security>
</datasource>
<drivers>
<driver name=”oracle” module=”com.oracle.jdbc”>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

These changes we need to do in standalone.xml. If you want to change port you need change as below.

 

<socket-binding-group name=”standard-sockets” default-interface=”public” port-offset=”${jboss.socket.binding.port-offset:0}”>
<socket-binding name=”management-http” interface=”management” port=”${jboss.management.http.port:1980}”/>
<socket-binding name=”management-https” interface=”management” port=”${jboss.management.https.port:7793}”/>
<socket-binding name=”ajp” port=”${jboss.ajp.port:2009}”/>
<socket-binding name=”http” port=”${jboss.http.port:7780}”/>
<socket-binding name=”https” port=”${jboss.https.port:5443}”/>
<socket-binding name=”txn-recovery-environment” port=”4712″/>
<socket-binding name=”txn-status-manager” port=”4713″/>
<outbound-socket-binding name=”mail-smtp”>
<remote-destination host=”localhost” port=”25″/>
</outbound-socket-binding>
</socket-binding-group>

 

We are almost done. Now we need to define module.xml. This is a very important step. we need to create some directory structure.

\jboss-eap-7.1.0\jboss-eap-7.1\modules\com\oracle\jdbc\main

Go inside modules folder and create folders com\oracle\jdbc\main and put ojdbc6.jar and also module.xml.

module.xml

<module xmlns="urn:jboss:module:1.1" name="com.oracle.jdbc">

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

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

 

Put the BootWarDeployment.war inside deployment folder(\jboss-eap-7.1\standalone\deployments).

 

Go inside bin folder and run standalone.bat file and deploy the war file.

 

Jboss 7 EPA datasource configuration using oracle and spring boot

 

Insert some record in book table as we have implemented only get operation and use the postman to test the rest URI.

 

Jboss 7 EPA datasource configuration using oracle and spring boot

 

That’s all about Jboss 7 EPA Datasource configuration using oracle and spring boot. If you feel any problem to run this application please leave a message in the comment box.

Check Datasource configuration docs here.

You may like –

 

JBoss Deployment docs.