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 [flexeraDS].
when a sister one from which I'm copying the configuration quietly runs.
I have:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/PetLogin">
<ResourceLink global="ds/flexeraDS" name="ds/flexeraDS" type="javax.sql.DataSource"/>
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<web-app ....>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>ds/flexeraDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd ">
<persistence-unit name="flexerajpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>java:/comp/env/ds/flexeraDS</non-jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level" value="WARNING" />
</properties>
</persistence-unit>
</persistence>
Now my syster project some-how manages to create its database folder inside apache-tomcat-7.0.40/bin/exampleDB on startup while mine doesn't create it and throws an error as above.
The code where the error is thrown is the first time I connect to the database:
EntityManager entityManager = PersistenceProvider.createEntityManager();
where the PersistenceProvider class is:
public final class PersistenceProvider
{
private static Map<String, Object> lOptions = new HashMap<String, Object>();
static
{
lOptions.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "false");
}
private static EntityManagerFactory factory = Persistence
.createEntityManagerFactory("flexerajpa", lOptions);
private PersistenceProvider()
{
}
/**
* @return
*/
public static EntityManager createEntityManager()
{
return factory.createEntityManager();
}
/**
* @return
*/
public static Metamodel getMetamodel()
{
return factory.getMetamodel();
}
}
I'm out of possible reasons for this.. if anyone has any suggestion. Thank you
This question is related to
java
jakarta-ee
jpa
orm
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
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).
In Tomcat 8.0.44 I did this: create the JNDI on Tomcat's server.xml between the tag "GlobalNamingResources" For example:
<GlobalNamingResources>_x000D_
<!-- Editable user database that can also be used by_x000D_
UserDatabaseRealm to authenticate users_x000D_
-->_x000D_
<!-- Other previus resouces -->_x000D_
<Resource auth="Container" driverClassName="org.postgresql.Driver" global="jdbc/your_jndi" _x000D_
maxActive="100" maxIdle="20" maxWait="1000" minIdle="5" name="jdbc/your_jndi" password="your_password" _x000D_
type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/your_database?user=postgres" username="database_username"/>_x000D_
</GlobalNamingResources>
_x000D_
<?xml version="1.0" encoding="UTF-8"?>_x000D_
<Context reloadable="true" >_x000D_
<ResourceLink name="jdbc/your_jndi"_x000D_
global="jdbc/your_jndi"_x000D_
auth="Container"_x000D_
type="javax.sql.DataSource" />_x000D_
</Context>
_x000D_
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"?>_x000D_
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"_x000D_
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"_x000D_
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">_x000D_
<persistence-unit name="UNIT_NAME" transaction-type="RESOURCE_LOCAL">_x000D_
<provider>org.hibernate.ejb.HibernatePersistence</provider>_x000D_
_x000D_
<properties>_x000D_
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />_x000D_
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />_x000D_
_x000D_
<!-- <property name="hibernate.jdbc.time_zone" value="UTC"/>-->_x000D_
<property name="hibernate.hbm2ddl.auto" value="update" />_x000D_
<property name="hibernate.show_sql" value="false" />_x000D_
<property name="hibernate.format_sql" value="true"/> _x000D_
</properties>_x000D_
</persistence-unit>_x000D_
</persistence>
_x000D_
So in your spring.xml you can do that:
<bean id="postGresDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">_x000D_
<property name="jndiName" value="java:comp/env/jdbc/your_jndi" />_x000D_
</bean>_x000D_
_x000D_
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">_x000D_
<property name="persistenceUnitName" value="UNIT_NAME" />_x000D_
<property name="dataSource" ref="postGresDataSource" />_x000D_
<property name="jpaVendorAdapter"> _x000D_
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />_x000D_
</property>_x000D_
</bean>
_x000D_
<property name="jndiName" value="java:comp/env/jdbc/your_jndi" />
_x000D_
In this example I used spring with xml but you can do this programmaticaly if you prefer.
That's it, I hope helped.
Source: Stackoverflow.com