[英]NamingException: Cannot create resource instance with Jackrabbit in Tomcat
[英]Cannot connect to postgresql using jdbc and tomcat. NamingException is thrown
我想創建一個 Jersey ReST API 並訪問 PostgreSQL 數據庫。 我設置了 Tomcat 8 服務器並嘗試訪問本地 PostgreSQL 服務器,但它不起作用。
我的 context.xml (在文件夾“project_folder/main/webapp/META_INF/”中)看起來像
<Context>
<Resource name="jdbc/mpmttest"
auth="Container"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/mpm"
user="postgres"
password="pw"
maxActive="15"
maxIdle="3"/>
</Context>
我將以下內容添加到 web.xml
<resource-ref>
<description>mpmt-database</description>
<res-ref-name>jdbc/mpmttest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
但是當我嘗試查找數據庫時,我得到了一個 NamingException。
我的源代碼:
public class DatabaseHandler {
private DataSource standardDatabase = null;
public DatabaseHandler() {
try {
InitialContext initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
standardDatabase = (DataSource) envCtx.lookup("jdbc/mpmttest");
} catch (NamingException e) {
e.printStackTrace();
throw new RuntimeException("Failed to create datasource for standard binding", e);
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException
{
return standardDatabase.getConnection();
}
}
瀏覽器中顯示的堆棧跟蹤顯示:
Root Cause
javax.naming.NamingException: Unexpected exception resolving reference [Root exception is java.lang.NullPointerException]
org.apache.naming.NamingContext.lookup(NamingContext.java:856)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.NamingContext.lookup(NamingContext.java:827)
org.apache.naming.NamingContext.lookup(NamingContext.java:173)
com.sap.ida.ae.mpmtservices.persistence.db.DatabaseHandler.<init>(DatabaseHandler.java:21)
com.sap.ida.ae.mpmtservices.persistence.db.StdDatabaseProvider.getDatabaseHandler(StdDatabaseProvider.java:8)
com.sap.ida.ae.mpmtservices.api.TestCaseAPI.createTable(TestCaseAPI.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:244)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NullPointerException
org.apache.naming.factory.ResourceFactory.getDefaultFactory(ResourceFactory.java:45)
org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:92)
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
org.apache.naming.NamingContext.lookup(NamingContext.java:839)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.NamingContext.lookup(NamingContext.java:827)
org.apache.naming.NamingContext.lookup(NamingContext.java:173)
com.sap.ida.ae.mpmtservices.persistence.db.DatabaseHandler.<init>(DatabaseHandler.java:21)
com.sap.ida.ae.mpmtservices.persistence.db.StdDatabaseProvider.getDatabaseHandler(StdDatabaseProvider.java:8)
com.sap.ida.ae.mpmtservices.api.TestCaseAPI.createTable(TestCaseAPI.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.process(Errors.java:244)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我玩了很多配置和編碼,但沒有解決問題。 似乎無法訪問數據庫。 我可以關閉數據庫服務器並遇到同樣的問題。 我不知道我能做些什么來解決這個問題。
您可以像這樣在 Databasehandler 中嘗試一下嗎:
//Context envCtx = (Context) initCtx.lookup("java:comp/env");
standardDatabase = (DataSource) initCtx.lookup("java:comp/env/jdbc/mpmpttest");
我對所有運行 tomcat 8 和 9 的 Web 應用程序都這樣做
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.