0

Could not load database properties file.

I have my database.properties file in class path resources folder and I have configured activejdbc.properties at root of classpathenv.connections.file=MyProject/src/main/resources/database.properties

I am using mvn to run my app:

mvn process-classes

mvn activejdbc-instrumentation:instrument

mvn package

mvn compile exec:java

When I run the app with Base.open(), I get error:

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)

update:

Running the sample from javalite github is working fine, however, changing this with spark routing, its throwing error:

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;
});

Error:

[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)

So seems like a thread issue where javalite Base.open() creates a thread named "default" and spark is not able comprehend the thread with name "default"

kittu
  • 6,662
  • 21
  • 91
  • 185

2 Answers2

1

You need to understand how Maven life cycles work.

  1. No need to call mvn activejdbc-instrumentation:instrument
  2. Do not execute compile before exec.java

Just do this:

mvn package
mvn exec:java

Since you did not provide any additional info on your pom.xml, I assume it is typical for ActiveJDBC Maven configuration.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Best place for you to start is this sample project: https://github.com/javalite/simple-example – ipolevoy Apr 06 '17 at 00:34
  • Using `mvn package mvn exec:java` and running the app still gives the same error: `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?` – kittu Apr 06 '17 at 02:55
  • Tried with: `github.com/javalite/simple-example` which gives the same error: `Caused by: 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?` – kittu Apr 06 '17 at 03:01
  • A sample working project would help everyone. Check out github.com/javalite/simple-example and see it has same issues. – kittu Apr 06 '17 at 03:09
  • this sample project works as expected: https://github.com/javalite/simple-example. Please, follow directions on the README page. – ipolevoy Apr 06 '17 at 03:26
  • Running `java -classpath target/dependency:target/classes activejdbc.examples.simple.Main` throws `Error: Could not find or load main class activejdbc.examples.simple.Main` So it is NOT WORKING AS EXPECTED – kittu Apr 06 '17 at 03:34
  • @kittu, I just tested, it is absolutely working as expected. Please, follow the README file instruction. – ipolevoy Apr 06 '17 at 04:09
  • I am sorry to say that I have tried so many times with line by line instructions in `readme` file but I am getting error: `could not find main class`. I don't know why it is not working for me. I am using command line to run commands :( – kittu Apr 06 '17 at 04:19
  • I try to record my screen while running commands and send you the link so you can see – kittu Apr 06 '17 at 04:20
  • @kittu, here is the output from running this example step by step: https://gist.github.com/ipolevoy/5fcd18d2a1b3ca85573fe194c6cad285. It just works, please see if you miss a step. – ipolevoy Apr 06 '17 at 14:28
  • It is working on your system, but it is not working for others. I have tried it in a new system I purchased the other day. Same error that `main method is not found`. May be some thing is missing in the documentation. Here is the youtube link I have recorded with all the steps: https://www.youtube.com/watch?v=MmDkbadeDWw&feature=youtu.be – kittu Apr 13 '17 at 06:56
  • @kittu, the errors `Error: Could not find or load main class activejdbc.examples.simple.Main` and `Caused by: org.javalite.activejdbc.DBException: Could not find configuration in a property file for environment` are different. Surely there is something is wrong in the way you execute. Unfortunately the video is not working. Can you please fix the video? – ipolevoy Apr 13 '17 at 12:29
  • Have you seen the video? Waiting for your reply? – kittu Apr 13 '17 at 14:38
  • So what do you think? I have executed all the steps properly in the video. What could be the issue? – kittu Apr 14 '17 at 03:42
  • @kittu, the problem you are having is because you are using Linux path format on Windows. The instructions are for classpath using a Linux format: `target/dependency:target/classes`. Please, note a colon `:`. On Windows your path separator is a semi-colon: `java -classpath target/dependency;target/classes activejdbc.examples.simple.Main`. – ipolevoy Apr 14 '17 at 04:24
  • You are right. Finally it worked but the main problem is `Base.open()` which creates a thread named `Default` and javaspark is not able to understand it if I am not wrong. Updated the question. Any ideas how to fix it? – kittu Apr 14 '17 at 06:55
1

This is a second answer to the second part of your question you added last.

The reason you do not have a connection is because ActiveJDBC is lazy by default: http://javalite.io/lazy_and_eager. This means that at the time you execute your Spark get() method, you open and close the connection to the database. The data object you pass to a view did not have a chance to load data from the database. There are three ways to solve this:

  1. Cause direct trip to the database:

    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. Use Spark filters top open and close DB connections and do not pollute code in your get() methods: http://sparkjava.com/documentation.html#filters

  3. Use ActiveWeb which is nicely integrated with ActiveJDBC: http://javalite.io/activeweb

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • ok I went through the docs `In order to pre-populate the list before passing it off, you need to call any method that will force loading (hydrating) of the list from the database i.e. list.size()` and I have used spark `before()` and `after()` filter but it still throws the same error: `Could not find configuration in a property file for environment: development. Are you sure you have a database.properties file configured?` – kittu Apr 15 '17 at 20:16
  • @kittu, if this is happening, you need to look into why the file is not there. See how you package and deploy your WAR file. The file `database.properties` should be on the classpath of your project when running on the web container. This must be simple! – ipolevoy Apr 15 '17 at 21:44