我无法在Heroku上连接我的数据库

时间:2021-09-23 03:01:31

I have craeted a Web Application that can be deployed on Heroku by maven eclipse.

我曾经发过一个可以通过maven eclipse在Heroku上部署的Web应用程序。

Group Id: org.glassfish.jersey.archetypes

Group Id:org.glassfish.jersey.archetypes

Artifact Id: jersey-heroku-webapp

工件ID:jersey-heroku-webapp

version: 2.17

then I followed this guide(1.5) to push it to Heroku.

然后我按照本指南(1.5)将其推送到Heroku。

https://jersey.java.net/documentation/latest/getting-started.html#heroku-webapp

I can not access my apple class from the http link- like this:

我无法从http链接访问我的apple类 - 如下所示:

https://salty-refuge-2027.herokuapp.com/apple

and I am getting the error 500

我收到错误500

I have tested it before without the JDBC procedure and I got the output Hello, from apple class therefore I guess it depends on the my implementation for the Heroku Postgres https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java

我之前没有使用JDBC过程测试过它,我从apple类得到了输出Hello,因此我想这取决于我对Heroku Postgres的实现https://devcenter.heroku.com/articles/heroku-postgresql#connecting-在Java的

    import java.net.URI;
    import java.net.URISyntaxException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("apple")
    public class Apple {

            @GET
            @Produces(MediaType.TEXT_PLAIN)
            public String getIt() {
                try {
                    getConnection();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return "Hello, from apple class";
            }

            private static Connection getConnection() throws URISyntaxException, SQLException {
        //I have modified my orginal ingredients.
                URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@
ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

                String username = dbUri.getUserInfo().split(":")[0];
                String password = dbUri.getUserInfo().split(":")[1];
                String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
                        + dbUri.getPort() + dbUri.getPath();

                Connection con = DriverManager.getConnection(dbUrl, username, password);
                System.out.println("It works");
                return con;
            }
        }

Heroku log:

D:\maven\heroku_maven_by_eclipse\serverSide>heroku logs
2015-05-13T11:19:11.364893+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:401)
2015-05-13T11:19:11.364825+00:00 app[web.1]:    at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:11.364826+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:11.364828+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:11.364895+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:11.364896+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)
2015-05-13T11:19:11.364846+00:00 app[web.1]: java.lang.NullPointerException
2015-05-13T11:19:11.364901+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:11.364898+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:11.364900+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:11.364902+00:00 app[web.1]:    at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:11.364904+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:11.364908+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:11.364906+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:11.364909+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:11.364805+00:00 app[web.1]: 2015-05-13 11:19:11.364:WARN:oejs.S
ervletHandler:qtp688970022-21:
2015-05-13T11:19:11.364910+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:11.364911+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:11.364913+00:00 app[web.1]:    at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:11.364916+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:11.364914+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:11.364917+00:00 app[web.1]:    at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:11.364905+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:11.364938+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:11.364941+00:00 app[web.1]:    at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:11.364940+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:11.366434+00:00 heroku[router]: at=info method=GET path="/apple
" host=salty-refuge-2027.herokuapp.com request_id=77099e48-00b0-4ac2-8c41-c631b9
619a63 fwd="31.17.41.138" dyno=web.1 connect=1ms service=5ms status=500 bytes=50
5
2015-05-13T11:19:12.339340+00:00 heroku[router]: at=info method=GET path="/apple
" host=salty-refuge-2027.herokuapp.com request_id=cb3396d8-474c-436c-9f13-a6d7a3
ac102e fwd="31.17.41.138" dyno=web.1 connect=1ms service=6ms status=500 bytes=50
5
2015-05-13T11:19:12.337223+00:00 app[web.1]: 2015-05-13 11:19:12.336:WARN:oejs.S
ervletHandler:qtp688970022-22:
2015-05-13T11:19:12.337244+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:222)
2015-05-13T11:19:12.337246+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:12.337248+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:12.337235+00:00 app[web.1]: javax.servlet.ServletException: jav
a.lang.NullPointerException
2015-05-13T11:19:12.337237+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:421)
2015-05-13T11:19:12.337240+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:12.337242+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)
2015-05-13T11:19:12.337259+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:12.337250+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:12.337252+00:00 app[web.1]:    at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:12.337254+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:12.337256+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:12.337257+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:12.337261+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:12.337263+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:12.337265+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:12.337271+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:12.337272+00:00 app[web.1]:    at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:12.337274+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:12.337275+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:12.337277+00:00 app[web.1]:    at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:12.337278+00:00 app[web.1]: Caused by:
2015-05-13T11:19:12.337269+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:12.337267+00:00 app[web.1]:    at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:12.337280+00:00 app[web.1]: java.lang.NullPointerException
2015-05-13T11:19:12.337281+00:00 app[web.1]:    at java.net.URI$Parser.parse(URI
.java:3023)
2015-05-13T11:19:12.337293+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvo
cationHandlerFactory.java:81)
2015-05-13T11:19:12.337283+00:00 app[web.1]:    at java.net.URI.<init>(URI.java:
595)
2015-05-13T11:19:12.337284+00:00 app[web.1]:    at busTracker.serverSide.Apple.g
etConnection(Apple.java:34)
2015-05-13T11:19:12.337285+00:00 app[web.1]:    at busTracker.serverSide.Apple.g
etIt(Apple.java:21)
2015-05-13T11:19:12.337290+00:00 app[web.1]:    at sun.reflect.DelegatingMethodA
ccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2015-05-13T11:19:12.337295+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMet
hodDispatcher.java:164)
2015-05-13T11:19:12.337296+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMe
thodDispatcher.java:181)
2015-05-13T11:19:12.337287+00:00 app[web.1]:    at sun.reflect.NativeMethodAcces
sorImpl.invoke0(Native Method)
2015-05-13T11:19:12.337288+00:00 app[web.1]:    at sun.reflect.NativeMethodAcces
sorImpl.invoke(NativeMethodAccessorImpl.java:57)
2015-05-13T11:19:12.337291+00:00 app[web.1]:    at java.lang.reflect.Method.invo
ke(Method.java:606)
2015-05-13T11:19:12.337300+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResource
MethodDispatcher.java:101)
2015-05-13T11:19:12.337308+00:00 app[web.1]:    at org.glassfish.jersey.internal
.Errors$1.call(Errors.java:267)
2015-05-13T11:19:12.337310+00:00 app[web.1]:    at org.glassfish.jersey.internal
.Errors.process(Errors.java:315)
2015-05-13T11:19:12.337311+00:00 app[web.1]:    at org.glassfish.jersey.internal
.Errors.process(Errors.java:297)
2015-05-13T11:19:12.337301+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
2015-05-13T11:19:12.337303+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
2015-05-13T11:19:12.337304+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
2015-05-13T11:19:12.337306+00:00 app[web.1]:    at org.glassfish.jersey.server.S
erverRuntime$2.run(ServerRuntime.java:305)
2015-05-13T11:19:12.337307+00:00 app[web.1]:    at org.glassfish.jersey.internal
.Errors$1.call(Errors.java:271)
2015-05-13T11:19:12.337298+00:00 app[web.1]:    at org.glassfish.jersey.server.m
odel.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(Jav
aResourceMethodDispatcherProvider.java:203)
2015-05-13T11:19:12.337330+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHolder.handle(ServletHolder.java:698)
2015-05-13T11:19:12.337332+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doHandle(ServletHandler.java:519)
2015-05-13T11:19:12.337326+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:386)
2015-05-13T11:19:12.337312+00:00 app[web.1]:    at org.glassfish.jersey.internal
.Errors.process(Errors.java:267)
2015-05-13T11:19:12.337314+00:00 app[web.1]:    at org.glassfish.jersey.process.
internal.RequestScope.runInScope(RequestScope.java:317)
2015-05-13T11:19:12.337337+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doHandle(ContextHandler.java:1097)
2015-05-13T11:19:12.337333+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:138)
2015-05-13T11:19:12.337334+00:00 app[web.1]:    at org.eclipse.jetty.security.Se
curityHandler.handle(SecurityHandler.java:564)
2015-05-13T11:19:12.337336+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doHandle(SessionHandler.java:213)
2015-05-13T11:19:12.337315+00:00 app[web.1]:    at org.glassfish.jersey.server.S
erverRuntime.process(ServerRuntime.java:288)
2015-05-13T11:19:12.337325+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
WebComponent.service(WebComponent.java:401)
2015-05-13T11:19:12.337339+00:00 app[web.1]:    at org.eclipse.jetty.server.sess
ion.SessionHandler.doScope(SessionHandler.java:175)
2015-05-13T11:19:12.337316+00:00 app[web.1]:    at org.glassfish.jersey.server.A
pplicationHandler.handle(ApplicationHandler.java:1110)
2015-05-13T11:19:12.337329+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:222)
2015-05-13T11:19:12.337341+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ContextHandler.doScope(ContextHandler.java:1031)
2015-05-13T11:19:12.337338+00:00 app[web.1]:    at org.eclipse.jetty.servlet.Ser
vletHandler.doScope(ServletHandler.java:446)
2015-05-13T11:19:12.337342+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.ScopedHandler.handle(ScopedHandler.java:136)
2015-05-13T11:19:12.337344+00:00 app[web.1]:    at org.eclipse.jetty.server.Serv
er.handle(Server.java:445)
2015-05-13T11:19:12.337343+00:00 app[web.1]:    at org.eclipse.jetty.server.hand
ler.HandlerWrapper.handle(HandlerWrapper.java:97)
2015-05-13T11:19:12.337346+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Channel.handle(HttpChannel.java:269)
2015-05-13T11:19:12.337350+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
2015-05-13T11:19:12.337353+00:00 app[web.1]:    at java.lang.Thread.run(Thread.j
ava:745)
2015-05-13T11:19:12.337349+00:00 app[web.1]:    at org.eclipse.jetty.io.Abstract
Connection$ReadCallback.run(AbstractConnection.java:358)
2015-05-13T11:19:12.337347+00:00 app[web.1]:    at org.eclipse.jetty.server.Http
Connection.onFillable(HttpConnection.java:229)
2015-05-13T11:19:12.337351+00:00 app[web.1]:    at org.eclipse.jetty.util.thread
.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
2015-05-13T11:19:12.337328+00:00 app[web.1]:    at org.glassfish.jersey.servlet.
ServletContainer.service(ServletContainer.java:335)

1 个解决方案

#1


Well, from the logs. it seems the error is this part

好吧,从日志。似乎错误就是这一部分

URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

the dbUri variable is null because the app cannot found the system environment variable. maybe this is what you mean?

dbUri变量为null,因为应用程序找不到系统环境变量。也许这就是你的意思?

URI dbUri = new URI("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9");

if not then, you might want to check your system environment variable for this key postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9 though I think this is just ridiculous to have an environment variable with key like that :p

如果没有,你可能想检查你的系统环境变量这个关键postgres:// mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432 / d5kal1r7jtavr9虽然我认为这是用一个像这样的键的环境变量是荒谬的:p

EDIT

from the documentation. it seems your code should be written like this

来自文档。看来你的代码应该是这样编写的

 URI dbUri = new URI(System.getenv("DATABASE_URL"));

the "DATABASE_URL" is an variable that has been set into your environment so it should retrieve the right value. you might want to check it like this on linux system

“DATABASE_URL”是一个已设置到您的环境中的变量,因此它应该检索正确的值。你可能想在linux系统上这样检查它

echo $DATABASE_URL

or in windows

或在窗户中

echo %DATABASE_URL%

#1


Well, from the logs. it seems the error is this part

好吧,从日志。似乎错误就是这一部分

URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));

the dbUri variable is null because the app cannot found the system environment variable. maybe this is what you mean?

dbUri变量为null,因为应用程序找不到系统环境变量。也许这就是你的意思?

URI dbUri = new URI("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9");

if not then, you might want to check your system environment variable for this key postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9 though I think this is just ridiculous to have an environment variable with key like that :p

如果没有,你可能想检查你的系统环境变量这个关键postgres:// mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@ec2-60-16-433-222.compute-1.amazonaws.com:5432 / d5kal1r7jtavr9虽然我认为这是用一个像这样的键的环境变量是荒谬的:p

EDIT

from the documentation. it seems your code should be written like this

来自文档。看来你的代码应该是这样编写的

 URI dbUri = new URI(System.getenv("DATABASE_URL"));

the "DATABASE_URL" is an variable that has been set into your environment so it should retrieve the right value. you might want to check it like this on linux system

“DATABASE_URL”是一个已设置到您的环境中的变量,因此它应该检索正确的值。你可能想在linux系统上这样检查它

echo $DATABASE_URL

or in windows

或在窗户中

echo %DATABASE_URL%