javax.naming.NameNotFoundException: Name is not bound in this Context. Unable to find

前端 未结 4 458
抹茶落季
抹茶落季 2020-12-09 10:19

I\'m trying to find out why my web application throws a

javax.naming.NameNotFoundException: Name [flexeraDS] is not bound in this Context. Unable to find [f         


        
相关标签:
4条回答
  • 2020-12-09 10:39

    Ok found out the Tomcat file server.xml must be configured as well for the data source to work. So just add:

    <Resource 
    auth="Container" 
    driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
    maxActive="20" 
    maxIdle="10" 
    maxWait="-1" 
    name="ds/flexeraDS" 
    type="javax.sql.DataSource" 
    url="jdbc:derby:flexeraDB;create=true" 
      />
    
    0 讨论(0)
  • 2020-12-09 10:42

    ugh, just to iterate over my own case, which gave out approximately the same error - in the Resource declaration (server.xml) make sure to NOT omit driverClassName, and that e.g. for Oracle it is "oracle.jdbc.OracleDriver", and that the right JAR file (e.g. ojdbc14.jar) exists in %CATALINA_HOME%/lib

    0 讨论(0)
  • 2020-12-09 10:52

    You can also add

    <Resource 
    auth="Container" 
    driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
    maxActive="20" 
    maxIdle="10" 
    maxWait="-1" 
    name="ds/flexeraDS" 
    type="javax.sql.DataSource" 
    url="jdbc:derby:flexeraDB;create=true" 
    /> 
    

    under META-INF/context.xml file (This will be only at application level).

    0 讨论(0)
  • 2020-12-09 10:56

    In Tomcat 8.0.44 I did this: create the JNDI on Tomcat's server.xml between the tag "GlobalNamingResources" For example:

    <GlobalNamingResources>
        <!-- Editable user database that can also be used by
             UserDatabaseRealm to authenticate users
        -->
      <!-- Other previus resouces -->
        <Resource auth="Container" driverClassName="org.postgresql.Driver" global="jdbc/your_jndi" 
        maxActive="100" maxIdle="20" maxWait="1000" minIdle="5" name="jdbc/your_jndi" password="your_password" 
        type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/your_database?user=postgres" username="database_username"/>
      </GlobalNamingResources>
    In your web application you need a link to that resource (ResourceLink):

    <?xml version="1.0" encoding="UTF-8"?>
    <Context reloadable="true" >
    		 <ResourceLink name="jdbc/your_jndi"
    	 				global="jdbc/your_jndi"
    	 				auth="Container"
    	 				type="javax.sql.DataSource" />
    </Context>

    So if you're using Hiberte with spring you can tell to him to use the JNDI in your persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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" xmlns="http://java.sun.com/xml/ns/persistence">
    	<persistence-unit name="UNIT_NAME" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    
    		<properties>
    			<property name="javax.persistence.jdbc.driver" 		value="org.postgresql.Driver" />
    			<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
    			
    			<!--  <property name="hibernate.jdbc.time_zone" value="UTC"/>-->
    			<property name="hibernate.hbm2ddl.auto" value="update" />
    			<property name="hibernate.show_sql" value="false" />
    			<property name="hibernate.format_sql" value="true"/>     
    		</properties>
    	</persistence-unit>
    </persistence>

    So in your spring.xml you can do that:

    <bean id="postGresDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    			<property name="jndiName" value="java:comp/env/jdbc/your_jndi" />
    		</bean>
       		
            <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                  <property name="persistenceUnitName" value="UNIT_NAME" />
                  <property name="dataSource" ref="postGresDataSource" />
                  <property name="jpaVendorAdapter"> 
                  	 	<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
                  </property>
            </bean>
    Look above that entityManagerFactory bean refers to your UNIT_NAME configured at persistence xml and the bean postGresDataSource has a property that points to your JNDI resource in Tomcat.

    <property name="jndiName" value="java:comp/env/jdbc/your_jndi" />

    In this example I used spring with xml but you can do this programmaticaly if you prefer.

    That's it, I hope helped.

    0 讨论(0)
提交回复
热议问题