0

无法加载数据库属性文件。

database.properties在类路径资源文件夹中有我的文件,并且我activejdbc.properties在根目录下进行了配置classpathenv.connections.file=MyProject/src/main/resources/database.properties

我正在使用 mvn 运行我的应用程序:

mvn process-classes

mvn activejdbc-instrumentation:instrument

mvn package

mvn compile exec:java

当我使用 运行应用程序时Base.open(),出现错误:

org.javalite.activejdbc.DBException: Could not find configuration in a property file for environment: development. Are you sure you have a database.properties file configured?
        at org.javalite.activejdbc.DB.open(DB.java:151)
        at org.javalite.activejdbc.Base.open(Base.java:52)
        at com.soul.seeker.Application.lambda$main$0(Application.java:52)
        at spark.FilterImpl$1.handle(FilterImpl.java:62)
        at spark.http.matching.BeforeFilters.execute(BeforeFilters.java:48)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:129)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
        at org.eclipse.jetty.server.Server.handle(Server.java:517)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
        at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)

更新:

从 javalite github 运行示例工作正常,但是,使用spark routing更改它,它的抛出错误:

get("/role_on_login", (req, res) -> {
    Base.open();

    Person director = new Person("Stephen Spielberg");
    director.saveIt();

    director.add(new Movie("Saving private Ryan", 1998));
    director.add(new Movie("Jaws", 1982));
    List data = director.getAll(Movie.class);
    Base.close();

    return data;
});

错误:

[qtp1213754379-16] WARN org.eclipse.jetty.server.HttpChannel - //localhost:4567/role_on_login
org.javalite.activejdbc.DBException: there is no connection 'default' on this thread, are you sure you opened it?
        at org.javalite.activejdbc.DB.connection(DB.java:754)
        at org.javalite.activejdbc.DB.createStreamingPreparedStatement(DB.java:521)
        at org.javalite.activejdbc.DB.find(DB.java:512)
        at org.javalite.activejdbc.LazyList.hydrate(LazyList.java:329)
        at org.javalite.activejdbc.AbstractLazyList.toString(AbstractLazyList.java:190)
        at spark.serialization.DefaultSerializer.process(DefaultSerializer.java:38)
        at spark.serialization.Serializer.processElement(Serializer.java:49)
        at spark.serialization.Serializer.processElement(Serializer.java:52)
        at spark.serialization.Serializer.processElement(Serializer.java:52)
        at spark.serialization.SerializerChain.process(SerializerChain.java:53)
        at spark.http.matching.Body.serializeTo(Body.java:72)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:189)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
        at org.eclipse.jetty.server.Server.handle(Server.java:517)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
        at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
        at java.lang.Thread.run(Unknown Source)

所以似乎是一个线程问题,其中 javaliteBase.open()创建了一个名为“default”的线程,而 spark 无法理解名为“default”的线程

4

2 回答 2

1

您需要了解 Maven 生命周期是如何工作的。

  1. 无需致电mvn activejdbc-instrumentation:instrument
  2. compile之前不要执行exec.java

只需这样做:

mvn package
mvn exec:java

由于您没有在 pom.xml 上提供任何其他信息,我认为它是 ActiveJDBC Maven 配置的典型配置。

于 2017-04-05T23:56:49.797 回答
1

这是您上次添加的问题第二部分的第二个答案。

您没有连接的原因是 ActiveJDBC 默认是惰性的:http://javalite.io/lazy_and_eager。这意味着在您执行 Sparkget()方法时,您会打开和关闭与数据库的连接。您传递给视图的data对象没有机会从数据库加载数据。有三种方法可以解决这个问题:

  1. 导致直接访问数据库:

    Base.open();

    Person director = new Person("Stephen Spielberg");
    director.saveIt();
    
    director.add(new Movie("Saving private Ryan", 1998));
    director.add(new Movie("Jaws", 1982));
    List data = director.getAll(Movie.class);
    data.size(); // <<<-- force trip to database before closing connection
    Base.close();
    return data;
    
  2. 使用 Spark 过滤器打开和关闭数据库连接,并且不要污染您的get()方法中的代码:http: //sparkjava.com/documentation.html#filters

  3. 使用与 ActiveJDBC 完美集成的 ActiveWeb:http: //javalite.io/activeweb

于 2017-04-14T19:53:19.853 回答