Skip to content

Minimal service to demonstrate guice persistence with a servlet

License

pgorak/guice-servlet-db-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Guice Servlet DB example

Motivation

During my first steps in learning Google Guice, I was not able to find a full, working example of a servlet application that would use the Guice PersistService to interact with an underlying DB. After reading a few available articles and doing many, many experiments I was able to come up with a minimalistic application that I hope presents this use case well.

Overview

The project builds a dockerized Postgres DB with a toy table Employee in which the sole record is updated every time the PUT endpoint on the servlet is executed. The table and the record does not have any meaning - it is just there so that we can see the updates of the timestamp in the record.

I found that the top level service method called by the servlet must have the @Transactional annotation for this to work. Putting the annotation on a nested method (like updateTimestamp) instead will not result in DB update.

Other than that, the other pieces of this small web service are the minimum that is needed to make it work.

Installation and application start

  1. mvn clean install
  2. docker-compose -f docker-postgresql.yml
  3. java -jar guice-servlet-db-example-0.0.1-jar-with-dependencies.jar

Using the REST API

  • Use the rest API by executing curl -X PUT http://localhost:8080/employee
  • Note updates on the record in the DB
  • Note that the Entity Manager changes with each request

About

Minimal service to demonstrate guice persistence with a servlet

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages