我向你解释我的问题。我尝试使用netbeans和tomcat创建一个web应用程序。我已经创建了应用程序的后端,为前端提供服务。后端连接到postgresql数据库,并通过使用eclipseLink (JPA 2.1)的持久单元管理实体。我使用每个组件的最新版本。
现在我想在前端使用这些服务,所以我在我的项目中包含了后端。jar。问题出在tomcat和db之间的连接上。经过几个小时的研究,我找到了这些配置文件:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>controleurMaintenance</servlet-name>
<servlet-class>controleur.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controleurMaintenance</servlet-name>
<url-pattern>/controleurMaintenance</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>connection.html</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>java:jdbc/maintenanceDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="jdbc/Maintenance_webInterface">
<Resource auth="Container" name="java:comp/env/jdbc/maintenanceDB" type="javax.sql.DataSource" user="paul" password="1234" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/Maintenance_db" maxActive="20" schema="public" maxIdle="2" maxAwaits="-1" validationQuery="select true;"/>
</Context>
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="MaintenancePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/MaintenanceDB</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
简而言之,我试着把它看作一个数据源。我没有碰过server.xml。我得到了javax.naming.NameNotFoundException:名称jdbc/MaintenanceDB没有链接到这个上下文中。例外情况取决于我所做的改变。有时似乎找到了数据库,但查询失败…
任何帮助都将非常感激。非常感谢,如果您需要更多的信息,请随时询问。
请在您的context.xml文件中尝试以下内容
<Resource auth="Container" driverClassName="org.postgresql.Driver" maxActive="20" maxIdle="2" maxWait="-1"
name="/maintenanceDB" password="1234" removeAbandoned="true" removeAbandonedTimeout="20"
type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/Maintenance_db" username="paul"
validationQuery="select true;"/>
干杯Anant
除此之外还有另一种绑定JNDI数据源的方法
try {
// Create initial context
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES,
"org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:comp");
ic.createSubcontext("java:comp/env");
// Construct DataSource for staging
// Construct DataSource for wfms
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setUrl("jdbc:postgresql://localhost:5432/maintenanceDB");//you might get this from context
ds.setUser("paul");//you might get this from context
ds.setPassword("1234");//you might get this from context
ic.bind("java:comp/env/maintenanceDB", wfmsds);
} catch (NamingException ex) {
ex.printStackTrace();
}
可能需要在servlet init()中添加这个函数;
谢谢Anant