Name [name] is not bound in this Context. Unable to find [jdbc]

Asked

Viewed 3,056 times

0

In the expectation of improving the performance of BD access I started to implement a pool of connections within my Web Application. But every time I will give a lookup on the application I think the following error occurs

javax.naming.Namenotfoundexception: Name [jdbc/bag] 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:166) at org.apache.naming.SelectorContext.lookup(Selectorcontext.java:157) at javax.naming.Initialcontext.lookup(Initialcontext.java:417) At filter.Filtro.doFilter(Filter.java:52) at org.apache.Catalina.core.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:239) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:206 .... (continue)

When searching several Qea and tutorials I ended up implementing the files of Tomcat as follows

xml server.

<GlobalNamingResources>
    <Resource name="jdbc/bolsa" auth="Container" type="javax.sql.DataSource"
      maxTotal="100" maxIdle="30" maxWaitMillis="10000"
      username="root" password="" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/bolsa"/>
</GlobalNamingResources>

xml context.

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/zk_login">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <ResourceLink global="jdbc/bolsa" name="jdbc/bolsa" type="javax.sql.DataSource" />

    <Resource name="jdbc/bolsa" auth="Container" type="javax.sql.DataSource"
              maxTotal="100" maxIdle="30" maxWaitMillis="10000"
              username="root" password="" driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/bolsa"/>
</Context>

Already inside of mine sistema I the persistence.xml was as follows

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="bolsa" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <non-jta-data-source>java:comp/env/jdbc/bolsa</non-jta-data-source>
        <class>bean.Permissao</class>
        <class>bean.Usuario</class>
        <class>bean.Pagina</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>


        <properties>

            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.show_sql" value="true"/>
            <!-- dados da conexao -->
            <!--  propriedades do hibernate -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

and the web xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
    <description>MySQL Test App</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/bolsa</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

How can I set up my server or if my call is wrong and how can I fix this problem.

Unfortunately I’ve looked at several tutorials and video but none of it worked :/

  • If you take the tag <Resource ..... /> context.xml does not work?

  • Unfortunately the same mistake happens.

1 answer

1


I solved my problem and had to solve several other problems together. Then I will show the solution of all step by step.

Version change for Hibernate

I was using the version hibernate-core-4.3.0 and hibernate-entitymanager-4.3.0. However as my goal was to use Connection Pool, I had to update the version of Hibernate since there were problems to implement Data Source.

So I switched to the version hibernate-core-5.2.11 and hibernate-entitymanager-5.2.11. With this some commands have been modified. The library ejb became decrepated (outdated) from version 4.3, so you have to use the library jpa as seen in that post

The Provider also modified and went from HibernatePersistence for HibernatePersistenceProvider.

So that persistence.xml was as follows:

<persistence-unit name="bolsa" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <non-jta-data-source>java:comp/env/jdbc/bolsa</non-jta-data-source>
    ...
</persistence-unit>

Connection pool (Datasource) using Tomcat

Although I have modified too much file from Tomcat, the only thing that was necessary was the context.xml, that remained:

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

    <!-- <ResourceLink global="jdbc/bolsa" name="jdbc/bolsa" type="javax.sql.DataSource" /> -->

    <Resource name="jdbc/bolsa" auth="Container" type="javax.sql.DataSource"
              maxTotal="100" maxIdle="30" maxWaitMillis="10000"
              username="root" password="" driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/bolsa"/>
</Context>

With this I can capture the Data source through the command:

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

Just don’t forget to put java:comp/env/ before the Datasource name.

Browser other questions tagged

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