4

Estou com sérios problemas com uma aplicação, to tentando configurar e nada.

Como sou iniciante na parte web, vou dizer primeiro o que entendi:

Tomcat precisa de configurações (xml) para funcionar com Hibernate.

No caso na pasta do TOMCAT é preciso alterar :

context.xml

 <?xml version="1.0" encoding="UTF-8"?>

  <Context>     <!-- Default set of monitored resources. If one of these
     changes, the -->   <!-- web application will be reloaded. -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
        <ResourceLink name="jdbc/toca_dados"
         global="jdbc/toca_dados"
         type="javax.sql.DataSource" />
 </Context>

server.xml

<GlobalNamingResources>      <Resource
 auth="Container" description="User database that can be updated and
 saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 name="UserDatabase"        pathname="conf/tomcat-users.xml"
 type="org.apache.catalina.UserDatabase" />
 <Resource name="jdbc/toca_dados" auth="Container" type="javax.sql.DataSource"      maxActive="100" maxIdle="30"
 maxWait="10000" username="root"        password="123"
 driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/TOCA_DADOS" />
 </GlobalNamingResources>

Estes são os arquivos que alterei dentro do TomCat, também adicionei o driver dentro da pasta LIB dentro do tomcat.

No projeto, dentro de WEB-INF/LIB coloquei todos JARS do Hibernate, e o persistence.xml:

<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="CLIENT_SQL" transaction-type="RESOURCE_LOCAL">
             <non-jta-data-source>java:/comp/env/jdbc/toca_dados</non-jta-data-source>
                 <properties>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
                 <property name="connection.autocommit" value="false"/>
                 <property name="hibernate.hbm2ddl.auto" value="create"/>
                 <property name="hibernate.show_sql" value="true"/>
             </properties>
         </persistence-unit>
         </persistence>

Após fazer essas configurações, tentei rodar o tomcat e não consigo iniciar, meu banco de dados é MYSQL e o nome do Schema é TOCA_DADOS

Como configurar meu banco de dados no tomcat?

Erro que o tomcat gera:

javax.naming.NameNotFoundException: Name [jdbc/toca_dados] is not bound in this Context. Unable to find [jdbc].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at javax.naming.InitialContext.lookup(InitialContext.java:421)
    at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
    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)
    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.tocaDaPizzaControle.singleton.EntityManagerFactorySingleton.getInstance(EntityManagerFactorySingleton.java:12)
    at br.com.tocaDaPizzaControle.controller.LoginController.<init>(LoginController.java:23)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:121)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 Resposta 1

1

Parece me que falta registar o data source no web.xml, alem disso não irá funcionar se não se não colocares o Driver à pasta lib do Tomcat tomcat/lib, colar com.mysql.jdbc.Driver.x.x.x.jar

In web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <!-- must identify the data source, to persistence module can get him -->
    <resource-ref>
            <res-ref-name>jdbc/toca_datos</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

In apache-tomcat-x.x.x\conf\context.xml

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

     <!-- Data source configuration : define the data source -->
      <Resource name="jdbc/toca_datos" 
                   auth="Container" 
                   type="javax.sql.DataSource"
                   maxTotal="100" 
                   maxIdle="30" 
                   maxWaitMillis="10000"
                   username="your-username" 
                   password="your-password" 
                   driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/toca_datos_DataBase"/>
</Context>

And in persistence xml

 <non-jta-data-source>java:comp/env/jdbc/toca_datos</non-jta-data-source>

i have some tuorial about this subject, you can see him: https://github.com/jlmc/marketplace/blob/master/docs/DATA%20SOURCE%20configuration%20in%20Tomcat.md

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .