Tomcat 8 + Mysql + Hibernate > Name [jdbc/toca_data] is not bound in this Context. Unable to find [jdbc]

Asked

Viewed 1,187 times

4

I’m having serious problems with an application, I’m trying to set up and nothing.

As I am beginner in the web part, I will say first what I understood:

Tomcat needs settings (xml) to work with Hibernate.

In the case of the TOMCAT folder you need to change :

xml context.

 <?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>

xml server.

<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>

These are the files I changed inside Tomcat, I also added the driver inside the LIB folder inside the Tomcat.

In the project, within WEB-INF/LIB I put all JARS of Hibernate, and 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>

After making these settings, I tried running Tomcat and can’t start, my database is MYSQL and the name of Schema is TOCA_DADOS

How to configure my database on Tomcat?

Error that Tomcat generates:

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 answer

1

I seem to be missing the data source in web.xml, It won’t work if you don’t put the Driver in the Tomcat Tomcat/lib lib folder, paste with.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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.