java – Doubt regarding JPA management with Spring on JEE server


I'm starting with Spring MVC and in the book I'm reading the configuration and connection management is all done by Spring, that is, Spring opens and manages the connections with the database and makes it available as if I had it in a JEE server ( @PersistenceContext and @Transactional ).

The point is that this is all done in a web container like tomcat or jetty, my question is how to manage this issue in a complete JEE server like Wildfly, because I want the connection to be created by Wildfly ( Datasource created in wildfly) and not for spring

How does this integration work, I don't do any Spring connection configuration and just leave it up to the server, or do I have to configure something in Spring so that it can get the datasource created by wildfly and it (spring) do the management? My doubt is also because there is a Spring filter that keeps the connections open throughout the request ( ) and how will it manage this if the connections are managed by the JEE server (wildfly)?

I'm using Spring 4.2 and configuring everything via annotations, here's an excerpt from the current configuration that works in a web container:

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

public class JPAConfiguration {

    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan(new String[] { "" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        return em;

    public DataSource dataSource(Environment environment){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername( "root" );
        dataSource.setPassword( "123456" );
        return dataSource;

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

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

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("", "update");
        properties.setProperty("hibernate.show_sql", "true");
        return properties;



It is possible to lookup the DataSource with JNDI.

There are two ways to do it programmatically.

public DataSource dataSource() {
    JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    return lookup.getDataSource("jdni");


public DataSource dataSource() {
    DataSource ds = null;
    try {
        Context ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("jdbc/nomeBanco");
    } catch (NamingException e) {
    return ds;

If you're going to use JndiDataSourceLookup , take a look at the setResourceRef method, since the container is Java EE.

Scroll to Top