我正在试图找出我的网络应用程序抛出的原因
javax.naming.NameNotFoundException: Name [flexeraDS] is not bound in this Context. Unable to find [flexeraDS].
当我正在复制配置的姐妹悄然运行时。
我有:
<?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>
现在我的syster项目有些如何设法在启动时在apache-tomcat-7.0.40 / bin / exampleDB中创建其数据库文件夹,而我的不创建它并抛出上述错误。
引发错误的代码是我第一次连接到数据库时:
EntityManager entityManager = PersistenceProvider.createEntityManager();
PersistenceProvider类是:
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();
}
}
我出于可能的原因......如果有人有任何建议。 谢谢
答案 0 :(得分:12)
Ok发现必须配置Tomcat文件server.xml以使数据源正常工作。所以只需添加:
<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"
/>
答案 1 :(得分:5)
您也可以添加
<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"
/>
META-INF/context.xml
文件下的(这只是在应用程序级别)。
答案 2 :(得分:0)
嗯,只是迭代我自己的情况,它给出了大致相同的错误 - 在资源声明(server.xml)中确保不要省略driverClassName,例如对于Oracle,它是&#34; oracle.jdbc.OracleDriver&#34;,并且%CATALINA_HOME%/ lib
中存在正确的JAR文件(例如ojdbc14.jar)答案 3 :(得分:0)
在Tomcat 8.0.44中,我这样做是:在Tomcat的server.xml的标签“ GlobalNamingResources”之间创建JNDI。 例如:
<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>
<?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>
因此,如果您在春季使用Hiberte,可以告诉他在persistence.xml中使用JNDI
<?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>
因此,在您的spring.xml中,您可以这样做:
<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>
<property name="jndiName" value="java:comp/env/jdbc/your_jndi" />
在此示例中,我将spring与xml结合使用,但是您可以根据需要以编程方式进行此操作。
就是这样,希望对您有所帮助。