Showing posts with label datasource. Show all posts
Showing posts with label datasource. Show all posts

Monday, 29 May 2017

Getting started with SpringBoot!

Before starting SpringBoot, Let me tell you, How did I find Spring Boot.

I have started my career with Play Framework. Play is popular for its simplicity. Play almost required no configuration. It does not need to write any XML, yes, I repeat No XML.

Play by default provides below things:

1. DataSource - Hikaricp
2. Logger - SLF4J with LogBack
3. Dependency Injection -  Google Juice
4. Build tool - SBT (Simple Build Tool)

The play comes with Scala and Java. It supports Non-blocking IO.

After exploring play enough, I have started working on Spring rest (Jersy) with Angular. For play developer, it's difficult to write boiler code for Spring. Spring required a lot of boiler code.
I have started exploring seed projects for Spring, I found few useful (To avoid writing boiler code).
While watching one of the Devoxx videos, I came to know about SpringBoot. I started Exploring Spring Boot example. I was very Happy With SpringBoot. It required almost Zero Boiler code.





1. No Need of Tomcat.
2. No Need to write XMLs

Within a few minutes, I was able to create a simple SpringBoot application with HelloWorld rest API. It's almost like working with Play framework again.

SpringBoot comes with Maven, Gradle. I choose Gradle, as, I was already familiar with Maven.

To understand SpringBoot, I have developed the REST API for the retail manager.

The problem statement is as Below:

A basic Spring Boot Application for the retail manager. Shops are stored in the Java in the memory map. Nearby shops can be retrieved by providing latitude and longitude of a customer. The distance between customer and shop is calculated by Haversine formula! The latitude and longitude of a shop are calculated using Google map API.


I have pushed all the code to Github: https://github.com/prashant7090/retail-manager

Let's understand springBoot. SoringBoot provides annotations to specify controller, model, service layer.

@SpringBootApplication
@ComponentScan("com.retailmanager")
public class BootRetailManager {
    public static void main(String[] args) {
        SpringApplication.run(BootRetailManager.class, args);
    }


This is the starting point of SpringBoot.  @SpringBootApplication annotation loads components required for springBoot. @ComponentScan("com.retailmanager") - annotation by default scans spring components in child package, or, we can specify package.

@Controller
@RequestMapping("/shop")

@Controller annotation to specify controller.   @RequestMapping("/shop")   used to create route for the request.

@Autowired
GoogleApiUrlBuilder googleApiUrlBuilder;

@Autowired annotation to do dependency injection.

@Component
public class ShopDaoImpl implements ShopDao

 @Component annotation to indicate this is the class is an auto scan component. @ComponentScan  find out the bean (annotated with @Component) and register it in Spring container.

We have different annotations like @Service, @RestController etc, I have not used those. Rest of code is preety simple. (DAO layer).

Happy Learning!





Saturday, 28 January 2017

DataSource - Connection Cook

Datasource:  Datasource is connection cook which provides us the connection through the Connection pool. It's up to us to order n number of connections.

Why DataSource:
Without Datasource, we left with a driver manager. we face following problem with driver manager:
  • Take care of open & close the connection.
  • Can not create a connection pool
  • Hard to balance connection load.
Datasource provides us n number of connection, Connections are a reserve in the pool, so whenever someone required, it takes from the pool. When it close the connection is returned to the pool. We can externalize the properties of a Datasource. It help us If we want to keep different configuration depending upon a server. The Database vendor provides the implementation for Datasource. i.e Mysql, Oracle database provides the classes required build a Datasource. 

The most know DataSource in apache DBCP.

Below is the Spring configuration in for DBCP.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/databaseName" />
<property name="username" value="databaseUserName" />
<property name="password" value="databasePassword" />
<property name="defaultAutoCommit" value="true" />
</bean>

Below is the maven dependency for DBCP:

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>

Play Framework internally use Hikaricp DataSource: 

Hikaricp is faster than DBCP: Below is the performance statistics from HikariCp GitHub page:




Below is the Spring Configuration of Hikaricp for Mysql:
  
<bean id="HikariCpConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="dataSourceClassName"                     value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
    
 <property name="dataSourceProperties">
      <props>
        <prop key="serverName"> localhost </prop>
        <prop key="port">3306</prop>
        <prop key="databaseName">databasename</prop>
        <prop key="user">databaseUserName</prop>
        <prop key="password">databaseUserPasswod</prop>
      </props>
    </property>
  </bean>

Below is the maven dependency required for HikariCp:

    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>2.5.1</version>
    </dependency>

You Can choose latest from search.maven.org.