4
respostas

COMO CONFIGURAR TOMCAT 8 JDBC JNDI

Eu estou com um problema na minha aplicação! eu configurei para utilizar a parte de banco de dados como DataSource com c3p0. No meu eclipse esta configurado o tomcat7 e roda normal.

No ambiente de produçao é o Tomcat 8.5 e quando eu faço o deploy do meu war ele da o erro

javax.persistence.PersistenceException: Unable to build entity manager factory
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:81)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at br.com.bb.portal.main.filter.JPAFilter.init(JPAFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4572)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/Aplicacao]
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)
    ... 20 more
Caused by: javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/Aplicacao] is not bound in this Context. Unable to find [java:comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)

Eu rodei no tomcat 7 localmente para ver se dava erro, sem ser pelo eclipse e rodou normal. Acho que o problema esta na configuração do tomcat 8.5 mas eu ja tentei varias maneiras que encontrei na internet e nenhum funcionou até o momento.

Alguem poderia me ajudar?

4 respostas

Isso é um erro de jndi, nesse link, passaram um por um problema parecido e foi solucionado, dá uma olhada:

http://www.guj.com.br/t/jndi-datasource/182215

Caso siga as alterações recomendadas e o erro persistir, posta como está a configuração dos jdbc em ambos os tomcat's...

Esta diferente do que eu fiz.

Eu segui exatamente o que é ensinado nesse video.

E no video ele fala que quando colocar em produção teria que apenas colocar o conteúdo do context da aplicação no context do tomcat. Mas nao esta funcionando no Tomcat 8.5

web.xml da aplicação

<resource-ref>
    <description>DataSource Aplicacao</description>
    <res-ref-name>jdbc/Aplicacao</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/applicationContext-security.xml
    </param-value>
  </context-param>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

aplicationContext.xml da aplicação

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="AplicacaoDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
        <property name="jndiName">
            <value>java:comp/env/jdbc/Aplicacao</value>
        </property>
    </bean>     
</beans>

context.xml da aplicação

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource name="jdbc/Aplicacao" 
        auth="Container" 
        factory="org.apache.naming.factory.BeanFactory"
        type="com.mchange.v2.c3p0.ComboPooledDataSource"
        driverClass="com.mysql.jdbc.Driver" 
        jdbcUrl="jdbc:mysql://172.00.00.001:3306/teste?zeroDateTimeBehavior=convertToNull"
        user="teste"
        password="teste"
        minPoolSize="5"
        maxPoolSize="50"
        acquireIncrement="1" 
        idleConnectionTestPeriod="1000"
        maxStatements="50"
        maxIdleTimeExcessConnections="300"
        numHelperThreads="10"

        />
</Context>

persistence.xml da aplicação

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">

     <persistence-unit name="portalcsa" transaction-type="RESOURCE_LOCAL">
        <!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
        <non-jta-data-source>java:comp/env/jdbc/Aplicacao</non-jta-data-source>
        <properties>
  </properties>
    </persistence-unit>
</persistence>

No meu eclipse roda normal. Mas quando coloco no tomcat 8.5 ae da esse erro apresentado anteriormente.

Alguma sugestão? Ainda não conseguir resolver.

Você viu que no link que eu havia passado a solução foi feita no arquivo: server.xml né?

Passa o conteúdo desse arquivo também...