11
respostas

TOMCAT para WILDFLY

Um sistema que estava funcionando no TOMCAT, migramos para o wildfly. Subiu sem problemas. Mas queremos conectar ao banco de dados pelo arquivo persistence.xml. O que não foi possível.

Estamos utilizando esta dependência do jpa / spring e o arquivo de configuração para fazer a conexão com o banco de dados.

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring-data-jpa.version}</version>
        </dependency>

Arquivo de configuração:

package br.com.netsoft.config;

    import java.util.Properties;

    import javax.persistence.EntityManagerFactory;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    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.annotation.EnableTransactionManagement;

    @EnableJpaRepositories(basePackageClasses = WebConfig.class)
    @EnableTransactionManagement
    @Configuration
    public class JPAConfig {

        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
            JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
            factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setUsername("postgres");
            dataSource.setPassword("63Netsis417");
            dataSource.setUrl("jdbc:postgresql://localhost:5432/prefeitura");
            dataSource.setDriverClassName("org.postgresql.Driver");
            factoryBean.setDataSource(dataSource);
            Properties props = new Properties();
            props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            props.setProperty("hibernate.show_sql", "true");
            props.setProperty("show_sql", "true");
            props.setProperty("hibernate.format_sql", "true");
            props.setProperty("hibernate.hbm2ddl.auto", "update");
            factoryBean.setJpaProperties(props);
            factoryBean.setPackagesToScan("br.com.netsoft");
            return factoryBean;
        }

        @Bean
        public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
            return new JpaTransactionManager(emf);
        }
    }

O que fazer para mudar ?

11 respostas

Tenta fazer assim:

@EnableJpaRepositories(basePackageClasses = WebConfig.class)
@EnableTransactionManagement
@Configuration
public class JPAConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);

//buscando datasource do app server com JNDI
JndiTemplate jndi = new JndiTemplate();
DataSource dataSource = (DataSource) jndi .lookup("java:/prefeitura");

       factoryBean.setDataSource(dataSource);

        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        props.setProperty("hibernate.show_sql", "true");
        props.setProperty("show_sql", "true");
        props.setProperty("hibernate.format_sql", "true");
        props.setProperty("hibernate.hbm2ddl.auto", "update");
        factoryBean.setJpaProperties(props);
        factoryBean.setPackagesToScan("br.com.netsoft");
        return factoryBean;
    }

    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
    }
}

Só pra contexto, está ligado com essa dúvida: https://cursos.alura.com.br/forum/topico-migrando-de-tomcat-para-widfly-53753

Pelo que entendi, se funcionar, se eu alterar o arquivo satandalone.xml, automaticamente irá mudar a conexão.

Deu este erro, entendi que não achou o "java:/prefeitura".

https://gist.github.com/anonymous/45100870a5778cee945fabd0c606b000

Eu tenho que recriar o arquivo persistence.xml. ?

Não precisa de persistence.xml desse jeito e a configuração vai vir do servidor de aplicação sim.

Muda pra:

java:comp/env/prefeitura

Deixa então:

java:comp/prefeitura

Acho que deu o mesmo erro

https://gist.github.com/anonymous/8a39aa1a23231c4f7d83a1929c40fcd6

Mais uma tentativa:

java:/prefeitura

:)

Putz, faz sentido ser esse último, que é o que tava no log, inclusive:

Local JNDI

The Java EE platform specification defines the following JNDI contexts:

java:comp - The namespace is scoped to the current component (i.e. EJB)

java:module - Scoped to the current module

java:app - Scoped to the current application

java:global - Scoped to the application server

In addition to the standard namespaces, WildFly also provides the following two global namespaces:

java:jboss

java:/

Referência: https://docs.jboss.org/author/display/WFLY10/JNDI+Reference

Alguma ajuda ?