Saturday, October 25, 2014

Datasource configuration with Spring Boot

For configuring a datasource using Spring Boot we have couple of options. With the help of a simple example project, which is available on my github profile, I will walk you through these options. The example contains two simple entities Portal and Page with a 1:N bidirectional relationship.

The example with the help of Spring Data JPA and REST modules, creates and exposes CRUD operations for these entities using plain HTTP REST semantics.

In development mode it is very convenient to use an in-memory database. By simply including H2 as a maven dependency, Spring Boot uses it as an in-memory database, no connection URL needs to be provided. This example uses the Spring Data JPA interface to populate the database with some example data.

Using the @Profile("default") this bean will be just initialised in the default Spring profile (when no other profile is specified). We could have created an explicit profile for development mode also, but this example is just using the default for this.
Next we just need to start the application. With the following glue code we can start it as a standalone application (java -jar). It will also work if we drop the produced war file in a Servlet 3.0 servlet container.

Setting to debug level the org.hibernate.SQL logger, we can see the that database schema was created and populated during application startup.

In a production environment however is unlikely that we need to create the database schema and populate the database. We just need to tell to Spring Boot where and how to connect to the database. Since we would like to keep the ability to work with the app in development mode, the solution is to use another Spring profile for the production setup. Creating the application-production.properties configuration with the below properties (using mysql in this example) we can instruct Spring Boot to switch to production mode by setting the spring.profiles.active to production

As you can see the jdbc driver class name is not needed, Spring Boot can infer it from the database URL. Have a look at the DriverClassNameProvider for the details.
With the next release of Spring Boot (in 1.2.0-SNAPSHOT is already available) it will be possible to reference the datasource via JNDI, which is very common in traditional deployment setups where the datasources are configured inside the application server. Of course we can do this already with Spring using JndiDataSourceLookup but it got easier using Spring Boot, without using infrastructure beans. In order to demonstrate this, I have deployed the application into JBoss Wildfly, and created another Spring profile called jboss with the following configuration

Using the default standalone.xml as the initial configuration the datasource connecting to MySQL can be added like in the example below.

We also need to create a module.xml in the JBOSS_WILDFLY_HOME/modules/com/mysql/main folder with the following configuration. The mysql jdbc driver needs to be put into this folder also.

Next, by dropping the created war file into JBOSS_WILDFLY_HOME/standalone/deployments folder and executing the following command we can startup this simple application in JBoss.

Accessing the http://localhost:8080/datasource-configuration-1.0-SNAPSHOT/ you will see that the data is loaded from MySQL end exposed with Spring Data REST. If you would like to try it out this example, have a look at my github profile.

10 comments:

Angus said...
This comment has been removed by the author.
Angus said...

Nice post. Thank you for taking the time to write this up. And thank you for changing the layout. The other version of the bookshelf kept pulling my focus making your blog to read.

enriquezreneblog said...
This comment has been removed by the author.
enriquezreneblog said...

Hi, I have deployed on wildly however when I'm trying to call the rest services I have the 404 error

Whats is a mapping, I'm trying

http://localhost:8080/war-file-name/pages

Pawel Co Faddey said...

Great Article
Projects for ECE Final Year Students Final Year Project Centers in Chennai

Tony Chew said...

Tetapi ada sejarahwan lainnya ada yang yakini kalau orang pertama yang membuat permainan itu bernama Keung Tai Kung di era 12 SM.
asikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna
bocoran sgp
data hk

Unknown said...

Are you looking for a business loan, personal loans, mortgage loans, car loans, student loans, unsecured consolidation loans,project funding etc ... Or simply refuse loan from a bank or financial institution for one or more reasons? We are the right solutions for credit! We offer loans to businesses and individuals with low and affordable interest rate of 2%. So if you are Interested in an urgent and secured loan. For more information kindly email us today Via: elegantloanfirm@hotmail.com.



PoL said...

Do you want a fresh way of publishing your articles on the internet? If so, you should consider using our news website templates since are was designed and developed specifically to meet that need.

periyannan said...

Amazing article
Internship providing companies in chennai | Where to do internship | internship opportunities in Chennai | internship offer letter | What internship should i do | How internship works | how many internships should i do ? | internship and inplant training difference | internship guidelines for students | why internship is necessary

Mahi Agarwal said...

Hi, their colleagues, nice paragraph and nice arguments commented here, I am really enjoying by these.

Cusat University Exam Time Table 2021
Datta Meghe University Exam Time Table 2021
JRRSU Exam Time Table 2021