Spring Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Persistence top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Overview

In this article, we'll create a Spring application using Hibernate/JPA with a JNDI data source.

If you want to read discover Spring and Hibernate basics, check out this article.

2. Defining a Datasource

2.1. System

Since we're using a JNDI data source, we won't define it in our application, We'll define it in our application container. In this example, we're going to use 8.5.4 version of Tomcat.

We'll also need a database server. In this case, we'll use 9.5.4 version of PostgreSQL.

You should be able to replicate the same steps using any other Java application container and a database of your choice (as long as you have proper JDBC jars for it!).

2.2. Declaring Datasource

We'll declare our datasource in <tomcat_home>/conf/server.xml file inside the <GlobalNamingResources> element.

Assuming that the database server is running on the same machine as the application container, and that the intended database is named postgres, and that the username is baeldung with password pass1234, a resource would look like this:

<Resource name="jdbc/BaeldungDatabase" auth="Container"
  type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
  username="baeldung" password="pass1234" maxActive="20" maxIdle="10" maxWait="-1"/>

Take note that we've named our resource jdbc/BaeldungDatabase. This will be the name to be used when referencing this datasource.

We've also had to specify its type and database driver's class name. For it to work, you must also place the corresponding jar in <tomcat_home>/lib/ (in this case, PostgreSQL's JDBC jar).

Remaining configuration parameters are:

  • auth=”Container” – means that the container will be signing on to the resource manager on behalf of the application
  • maxActive, maxIdle, and maxWait – are pool connection's configuration parameters

We must also define a ResourceLink inside the <Context> element in <tomcat_home>/conf/context.xml, which would look like:

<ResourceLink name="jdbc/BaeldungDatabase" 
  global="jdbc/BaeldungDatabase" type="javax.sql.DataSource"/>

Note that we are using the name we defined in our Resource in server.xml.

3. Using the Resource

3.1. Setting the Application

We're going to define a simple Spring MVC + JPA + Hibernate application using pure Java config now.

We'll start by defining the Spring context's configuration. Keep in mind that we are focusing on JNDI here and assuming that you already know the basic of Spring's configuration.

3.2. Spring Context Configuration

Let's start with a common Spring MVC's context configuration:

@ComponentScan({ "com.baeldung.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver
          = new InternalResourceViewResolver();
        return viewResolver;

Now let's define the root context, JPA and Hibernate config:

@PropertySource({ "classpath:persistence-jndi.properties" })
@ComponentScan({ "com.baeldung.persistence" })
@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao")
public class PersistenceJNDIConfig {

    private Environment env;

    public PersistenceJNDIConfig() {

    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em
          = new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        return em;

    public DataSource dataSource() {
        try {
            return (DataSource) new JndiTemplate()
        } catch (NamingException e) {
            throw new IllegalArgumentException(
              "Error looking up JNDI datasource", e);

    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        return transactionManager;

    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();

    Properties additionalProperties() {
        Properties hibernateProperties = new Properties();
          "hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
          "hibernate.dialect", env.getProperty("hibernate.dialect"));
          "hibernate.cache.use_second_level_cache", "false");
        return hibernateProperties;

Note that all configuration is the same as the one used in the Spring 4 and JPA with Hibernate article with an addition of dataSource bean creation.

In order to create a dataSource bean we need to look for the JNDI resource defined as:


We will store this in properties file under the jdbc.url key among other properties:



In the jdbc.url property we are defining a root name to look for: java:comp/env/ and then the same name we used in server.xml: jdbc/BaeldungDatabase.

Additionally, we are defining a Hibernate dialect and setting the hdm2dll to update.

Let's set up our Initializer and we are ready to go. Since our application container is Servlet 3.0+ ready, we can stick to the Java config only:

public class WebInitializer
  extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { PersistenceJNDIConfig.class };

    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { SpringWebConfig.class };

    protected String[] getServletMappings() {
        return new String[] { "/" };

Our Servlet 3.0+ container will pick this class up and run it automatically.

4. JPA Configuration

4.1. Model, DAO, and Service

We're going to use a simple model with the @Entity annotation with a generated id and a name:

public class Foo {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private long id;
    @Column(name = "NAME")
    private String name;

    //default getters and setters

Let's define a simple repository like this:

public class FooDao {

    private EntityManager entityManager;

    public List<Foo> findAll() {
        return entityManager
          .createQuery("from " + Foo.class.getName()).getResultList();


And lastly, let's create a simple service:

public class FooService {

    private FooDao dao;

    public List<Foo> findAll() {
        return dao.findAll();


4.2. Displaying Results

In order to display results, we need two more things: a Controller and a View.

Let's build a simple Controller responding with a list of all Foo entries to HTTP GET requests:

public class MainController {

    private FooService fooService;

    public String main(Model model) {
        model.addAttribute("foos", fooService.findAll());
        return "index";


Finally, let's create a simple View showcasing all received items:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
        <title>Baeldung - Spring JNA JNDI</title>
        <c:forEach var="foo" items="${foos}">
                <c:out value="${foo.name}" />

Now, if we deploy our application to our application server, we can go to http://localhost:8080/<app_name> and see something like:

6. Conclusion

In this article, we created an example Spring application with a JPA + Hibernate setup working with a JNDI data source.

The full project can be found on GitHub.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Persistence bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Comments are closed on this article!