0

I have created a Jersey project from the Maven archetype jersey-quickstart-webapp on NetBeans. And I'm using ActiveJDBC for data persistence. In its documentation it's possible to reference an external file for database connection properties: http://javalite.io/database_connection_management#location-of-property-file

I have followed the instructions and I have this activejdbc.properties (located at project-ws/src/main/) file content:

env.connections.file=C:\dev\database.properties

And the database.properties file:

development.driver=com.mysql.jdbc.Driver
development.username=user_db
development.password=*****
development.url=jdbc:mysql://192.168.0.19/customersdb

test.driver=com.mysql.jdbc.Driver
test.username=user_db
test.password=*****
test.url=jdbc:mysql://192.168.0.19/customersdb

production.jndi=java:comp/env/jdbc/acme

Also, I have this web service:

@Path("telephone")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TelephoneResource {
    @GET
    public Response get() {
        try {
            Base.open();

            LazyList<Telephone> tels= Telephone.findAll();

            String telsJson = tels.toJson(true);

            Base.close();

            return Response.ok().entity(telsJson).build();
        } catch (Exception ex) {
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
        }
    }
}

But when I try to execute that GET resource, I receive this error message:

{
    "error": "Could not find configuration in a property file for environment: development. Are you sure you have a database.properties file configured?"
}

I also have tried this other configurations:

By adding them to the pom.xml file using the maven-surefire-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.13</version>
    <configuration>
        <systemPropertyVariables>
            <activejdbc.url>jdbc:mysql://192.168.0.19/customersdb</activejdbc.url>
            <activejdbc.user>user_db</activejdbc.user>
            <activejdbc.password>*****</activejdbc.password>
            <activejdbc.driver>com.mysql.jdbc.Driver</activejdbc.driver>
        </systemPropertyVariables>
        <environmentVariables>
            <ACTIVEJDBC.URL>jdbc:mysql://192.168.0.19/customersdb</ACTIVEJDBC.URL>
            <ACTIVEJDBC.USER>user_db</ACTIVEJDBC.USER>
            <ACTIVEJDBC.PASSWORD>*****</ACTIVEJDBC.PASSWORD>
            <ACTIVEJDBC.DRIVER>com.mysql.jdbc.Driver</ACTIVEJDBC.DRIVER>
        </environmentVariables>
    </configuration>
</plugin>

But they also did not work.

InfZero
  • 2,944
  • 4
  • 24
  • 36

1 Answers1

1

This page: http://javalite.io/database_connection_management#location-of-property-file states:

Add a file activejdbc.properties to your project at root of classpath and configure a property in it:

This means that the file needs to be at the root of your classpath. Since you use Maven, this means:

src/main/resources/database.properties

take a look at this app, complete with tests: https://github.com/javalite/simple-example

Also, opening a connection inside your controller is a pretty terrible way to do this. Since you are not using ActiveWeb which manages connections automatically, you can write a simple Servlet filter similar to: ActiveJdbcFilter, and get the Base.open()/Base.close() code out of your resource:

public class TelephoneResource {
    @GET
    public Response get() {
        try {
            return Response.ok().entity(Telephone.findAll().toJson(true)).build();
        } catch (Exception ex) {
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("{\"error\": \"" + ex.getMessage() + "\"}").build();
        }
    }
}

If you care to explore ActiveWeb, here is a simple example of a Rest Webapp: https://github.com/javalite/activeweb-rest

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Hi @ipolevoy. I've put both the `database.properties` and `activejdbc.properties` files in `src/main/resources/`, and the last file with this content: `env.connections.file=D:\proyect-ws\src\main\resources\activejdbc.properties` In your example (https://github.com/javalite/simple-example) I don't find the `activejdbc.properties` file. – InfZero Feb 09 '18 at 15:03
  • @InfZero, the example does not have a file `activejdbc.properties`, you usually do not have that file in your project. The file you need is `database.properties`. – ipolevoy Feb 12 '18 at 19:09
  • @InfZero, you do not need to mess with `env.connections.file` or environment variables. Just place the file `database.properties` on your classpath, this is all you need. – ipolevoy Feb 12 '18 at 19:11
  • 1
    @InfZero, new feature was added recently: http://javalite.io/database_connection_management#using-system-property since 2.1-SNAPSHOT – ipolevoy Mar 01 '18 at 20:59